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]);
441 if (fabs(temp)>1 && temp > 0.0) {
444 else if (fabs(temp)>1 && temp < 0.0) {
491 void StatCumProbDistDynCalc::save(QXmlStreamWriter &stream,
const Project *project)
const {
493 stream.writeStartElement(
"statCumProbDistDynCalc");
498 stream.writeStartElement(
"distributionData");
500 stream.writeStartElement(
"quantileInfo");
504 stream.writeAttribute(
"idealNumObsBelowQuantile",
507 stream.writeEndElement();
509 stream.writeEndElement();
510 stream.writeEndElement();
517 StatCumProbDistDynCalc::XmlHandler::XmlHandler(StatCumProbDistDynCalc *probabilityCalc,
519 m_xmlHandlerCumProbCalc = probabilityCalc;
520 m_xmlHandlerProject = project;
521 m_xmlHandlerCharacters =
"";
526 StatCumProbDistDynCalc::XmlHandler::~XmlHandler() {
529 m_xmlHandlerProject = NULL;
534 bool StatCumProbDistDynCalc::XmlHandler::startElement(
const QString &namespaceURI,
535 const QString &localName,
536 const QString &qName,
537 const QXmlAttributes &atts) {
539 m_xmlHandlerCharacters =
"";
540 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
541 if (qName ==
"quantileInfo") {
543 QString quantile = atts.value(
"quantile");
544 QString obsValue = atts.value(
"dataValue");
545 QString idealObs = atts.value(
"idealNumObsBelowQuantile");
546 QString actualObs = atts.value(
"actualNumObsBelowQuantile");
548 if (!quantile.isEmpty() && !obsValue.isEmpty()
549 && !idealObs.isEmpty() && !actualObs.isEmpty()) {
550 m_xmlHandlerCumProbCalc->m_quantiles.append(
toDouble(quantile));
551 m_xmlHandlerCumProbCalc->m_observationValues.append(
toDouble(obsValue));
552 m_xmlHandlerCumProbCalc->m_idealNumObsBelowQuantile.append(
toDouble(idealObs));
553 m_xmlHandlerCumProbCalc->m_numObsBelowQuantile.append(
toDouble(actualObs));
563 bool StatCumProbDistDynCalc::XmlHandler::characters(
const QString &ch) {
564 m_xmlHandlerCharacters += ch;
565 return XmlStackedHandler::characters(ch);
570 bool StatCumProbDistDynCalc::XmlHandler::endElement(
const QString &namespaceURI,
571 const QString &localName,
572 const QString &qName) {
573 if (!m_xmlHandlerCharacters.isEmpty()) {
574 if (qName ==
"numberCells") {
575 m_xmlHandlerCumProbCalc->m_numberCells =
toInt(m_xmlHandlerCharacters);
577 else if (qName ==
"numberQuantiles") {
578 m_xmlHandlerCumProbCalc->m_numberQuantiles =
toInt(m_xmlHandlerCharacters);
580 else if (qName ==
"numberObservations") {
581 m_xmlHandlerCumProbCalc->m_numberObservations =
toInt(m_xmlHandlerCharacters);
584 m_xmlHandlerCharacters =
"";
586 return XmlStackedHandler::endElement(namespaceURI, localName, qName);
591 QDataStream &StatCumProbDistDynCalc::write(QDataStream &stream)
const {
604 QDataStream &StatCumProbDistDynCalc::read(QDataStream &stream) {
606 qint32 numCells, numQuantiles, numObservations;
624 return scpddc.write(stream);
630 return scpddc.read(stream);
633 void StatCumProbDistDynCalc::validate() {
636 QString msg =
"StatCumProbDistDynCalc will return no data until the quantiles have been set. "
643 QString msg =
"StatCumProbDistDynCalc will return no data until the number of observations "
646 +
"] (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.