8#include "StatCumProbDistDynCalc.h"
14#include <QXmlStreamWriter>
20#include "IException.h"
23#include "XmlStackedHandlerReader.h"
49 xmlReader->pushContentHandler(
new XmlHandler(
this, project));
55 : m_numberCells(other.m_numberCells),
56 m_numberQuantiles(other.m_numberQuantiles),
57 m_numberObservations(other.m_numberObservations),
58 m_quantiles(other.m_quantiles),
59 m_observationValues(other.m_observationValues),
60 m_idealNumObsBelowQuantile(other.m_idealNumObsBelowQuantile),
61 m_numObsBelowQuantile(other.m_numObsBelowQuantile) {
107 void StatCumProbDistDynCalc::setQuantiles(
unsigned int nodes) {
187 if (cumProb < 0.0 || cumProb > 1.0) {
189 "StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].";
206 unsigned int index = 0;
212 if ( dist < minDistance ) {
253 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
259 double interp = (
cumProb-x[1]) * (
cumProb-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
260 + (
cumProb-x[0]) * (
cumProb-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
261 + (
cumProb-x[0]) * (
cumProb-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
267 for (i = 0; i < 3; i++) {
274 if (y[i] <= interp && y[i+1] >= interp) {
279 return ( (x[i] -
cumProb) * y[i+1] + (x[i+1] -
cumProb) * y[i] ) / (x[i] - x[i+1]);
314 unsigned int index = 0;
317 if ( dist < minDistance) {
358 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
364 double interp = (
value-x[1]) * (
value-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
365 + (
value-x[0]) * (
value-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
366 + (
value-x[0]) * (
value-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
371 for (i = 0; i < 3; i++) {
376 if (y[i] <= interp && y[i+1] >= interp)
380 return ((x[i] -
value) * y[i+1] + (x[i+1] -
value) * y[i]) / (x[i] - x[i+1]);
442 if (fabs(temp)>1 && temp > 0.0) {
445 else if (fabs(temp)>1 && temp < 0.0) {
492 void StatCumProbDistDynCalc::save(QXmlStreamWriter &stream,
const Project *project)
const {
494 stream.writeStartElement(
"statCumProbDistDynCalc");
499 stream.writeStartElement(
"distributionData");
501 stream.writeStartElement(
"quantileInfo");
505 stream.writeAttribute(
"idealNumObsBelowQuantile",
508 stream.writeEndElement();
510 stream.writeEndElement();
511 stream.writeEndElement();
518 StatCumProbDistDynCalc::XmlHandler::XmlHandler(StatCumProbDistDynCalc *probabilityCalc,
520 m_xmlHandlerCumProbCalc = probabilityCalc;
521 m_xmlHandlerProject = project;
522 m_xmlHandlerCharacters =
"";
527 StatCumProbDistDynCalc::XmlHandler::~XmlHandler() {
530 m_xmlHandlerProject = NULL;
535 bool StatCumProbDistDynCalc::XmlHandler::startElement(
const QString &namespaceURI,
536 const QString &localName,
537 const QString &qName,
538 const QXmlAttributes &atts) {
540 m_xmlHandlerCharacters =
"";
541 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
542 if (qName ==
"quantileInfo") {
544 QString quantile = atts.value(
"quantile");
545 QString obsValue = atts.value(
"dataValue");
546 QString idealObs = atts.value(
"idealNumObsBelowQuantile");
547 QString actualObs = atts.value(
"actualNumObsBelowQuantile");
549 if (!quantile.isEmpty() && !obsValue.isEmpty()
550 && !idealObs.isEmpty() && !actualObs.isEmpty()) {
551 m_xmlHandlerCumProbCalc->m_quantiles.append(
toDouble(quantile));
552 m_xmlHandlerCumProbCalc->m_observationValues.append(
toDouble(obsValue));
553 m_xmlHandlerCumProbCalc->m_idealNumObsBelowQuantile.append(
toDouble(idealObs));
554 m_xmlHandlerCumProbCalc->m_numObsBelowQuantile.append(
toDouble(actualObs));
564 bool StatCumProbDistDynCalc::XmlHandler::characters(
const QString &ch) {
565 m_xmlHandlerCharacters += ch;
566 return XmlStackedHandler::characters(ch);
571 bool StatCumProbDistDynCalc::XmlHandler::endElement(
const QString &namespaceURI,
572 const QString &localName,
573 const QString &qName) {
574 if (!m_xmlHandlerCharacters.isEmpty()) {
575 if (qName ==
"numberCells") {
576 m_xmlHandlerCumProbCalc->m_numberCells =
toInt(m_xmlHandlerCharacters);
578 else if (qName ==
"numberQuantiles") {
579 m_xmlHandlerCumProbCalc->m_numberQuantiles =
toInt(m_xmlHandlerCharacters);
581 else if (qName ==
"numberObservations") {
582 m_xmlHandlerCumProbCalc->m_numberObservations =
toInt(m_xmlHandlerCharacters);
585 m_xmlHandlerCharacters =
"";
587 return XmlStackedHandler::endElement(namespaceURI, localName, qName);
592 QDataStream &StatCumProbDistDynCalc::write(QDataStream &stream)
const {
605 QDataStream &StatCumProbDistDynCalc::read(QDataStream &stream) {
607 qint32 numCells, numQuantiles, numObservations;
625 return scpddc.write(stream);
631 return scpddc.read(stream);
634 void StatCumProbDistDynCalc::validate() {
637 QString msg =
"StatCumProbDistDynCalc will return no data until the quantiles have been set. "
644 QString msg =
"StatCumProbDistDynCalc will return no data until the number of observations "
647 +
"] (i.e. number of nodes) selected.";
@ Programmer
This error is for when a programmer made an API call that was illegal.
Adds specific functionality to C++ strings.
The main project for ipce.
This class is used to approximate cumulative probibility distributions of a stream of observations wi...
QList< int > m_numObsBelowQuantile
The actual number of observations that are less than or equal to the value of the corresponding quant...
unsigned int m_numberObservations
The number of observations, note this is dynamically changing as observations are added.
double min()
Returns the maximum observation so far included in the dynamic calculation.
QList< double > m_idealNumObsBelowQuantile
The ideal number of observations that should be less than or equal to the value of the corresponding ...
QList< double > m_observationValues
The calculated values of the quantiles, note this is dynamically changing as observations are added.
StatCumProbDistDynCalc(unsigned int nodes=20, QObject *parent=0)
Construtor sets up the class to start recieving data.
void initialize()
Inializer, resets the class to start its dynamic calculation anew.
unsigned int m_numberCells
The number of cells or histogram bins that are being used to model the probility density function.
~StatCumProbDistDynCalc()
Destroys StatCumProbDistDynCalc object.
unsigned int m_numberQuantiles
The number of quantiles being used to model the probility density function.
QList< double > m_quantiles
The target quantiles being modeled, between 0 and 1.
double value(double cumProb)
Provides the value of the variable that has the given cumulative probility (according the current est...
void addObs(double obs)
Values for the estimated quantile positions are update as observations are added.
double cumProb(double value)
Provides the cumulative probility, that is, the proportion of the distribution that is less than or e...
double max()
Returns the maximum observation so far included in the dynamic calculation.
Manage a stack of content handlers for reading XML files.
This is free and unencumbered software released into the public domain.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
int toInt(const QString &string)
Global function to convert from a string to an integer.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
double toDouble(const QString &string)
Global function to convert from a string to a double.
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.