23 #include <QDataStream>
27 #include <QXmlStreamWriter>
36 #include "XmlStackedHandlerReader.h"
62 xmlReader->pushContentHandler(
new XmlHandler(
this, project));
68 : m_id(new QUuid(other.m_id->
toString())),
69 m_numberCells(other.m_numberCells),
70 m_numberQuantiles(other.m_numberQuantiles),
71 m_numberObservations(other.m_numberObservations),
72 m_quantiles(other.m_quantiles),
73 m_observationValues(other.m_observationValues),
74 m_idealNumObsBelowQuantile(other.m_idealNumObsBelowQuantile),
75 m_numObsBelowQuantile(other.m_numObsBelowQuantile) {
95 m_id =
new QUuid(other.
m_id->toString());
128 void StatCumProbDistDynCalc::setQuantiles(
unsigned int nodes) {
130 m_id =
new QUuid(QUuid::createUuid());
210 if (cumProb < 0.0 || cumProb > 1.0) {
211 IString msg =
"Invalid cumulative probability [" +
toString(cumProb) +
"] passed in to "
212 "StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].";
217 if (cumProb == 0.0) {
222 if (cumProb == 1.0) {
227 double minDistance = fabs(
m_quantiles[0] - cumProb);
229 unsigned int index = 0;
230 for (
int i = 1; i < int(m_numberQuantiles); i++) {
235 if ( dist < minDistance ) {
276 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
282 double interp = (cumProb-x[1]) * (cumProb-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
283 + (cumProb-x[0]) * (cumProb-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
284 + (cumProb-x[0]) * (cumProb-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
290 for (i = 0; i < 3; i++) {
291 if ( x[i] <= cumProb && x[i+1] >= cumProb) {
297 if (y[i] <= interp && y[i+1] >= interp) {
302 return ( (x[i] - cumProb) * y[i+1] + (x[i+1] -
cumProb) * y[i] ) / (x[i] - x[i+1]);
337 unsigned int index = 0;
340 if ( dist < minDistance) {
362 else if (index == m_numberCells) {
381 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
387 double interp = (value-x[1]) * (value-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
388 + (value-x[0]) * (value-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
389 + (value-x[0]) * (value-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
394 for (i = 0; i < 3; i++) {
395 if ( x[i] <= value && x[i+1] >= value)
399 if (y[i] <= interp && y[i+1] >= interp)
403 return ((x[i] - value) * y[i+1] + (x[i+1] -
value) * y[i]) / (x[i] - x[i+1]);
446 for (
int i = 1; i < (int)m_numberQuantiles-1; i++) {
448 for (; i < (int)m_numberQuantiles-1; i++) {
454 for (
int i = 1; i < (int)m_numberQuantiles; i++) {
464 if (fabs(temp)>1 && temp > 0.0) {
467 else if (fabs(temp)>1 && temp < 0.0) {
476 && (m_numObsBelowQuantile[i+1] - m_numObsBelowQuantile[i] > 1) )
478 && (m_numObsBelowQuantile[i-1] - m_numObsBelowQuantile[i] < -1) ) ) {
481 + double(d) / (m_numObsBelowQuantile[i+1] - m_numObsBelowQuantile[i-1])
482 * ( (m_numObsBelowQuantile[i] - m_numObsBelowQuantile[i-1] + d)
484 / (m_numObsBelowQuantile[i+1] - m_numObsBelowQuantile[i])
485 + (m_numObsBelowQuantile[i+1] - m_numObsBelowQuantile[i] - d)
487 / (m_numObsBelowQuantile[i] - m_numObsBelowQuantile[i-1]) );
501 / (m_numObsBelowQuantile[i+d] - m_numObsBelowQuantile[i]);
506 m_numObsBelowQuantile[i] += d;
514 void StatCumProbDistDynCalc::save(QXmlStreamWriter &stream,
const Project *project)
const {
516 stream.writeStartElement(
"statCumProbDistDynCalc");
517 stream.writeTextElement(
"id",
m_id->toString());
519 stream.writeTextElement(
"numberQuantiles",
toString(m_numberQuantiles));
522 stream.writeStartElement(
"distributionData");
524 stream.writeStartElement(
"quantileInfo");
528 stream.writeAttribute(
"idealNumObsBelowQuantile",
531 stream.writeEndElement();
533 stream.writeEndElement();
534 stream.writeEndElement();
541 StatCumProbDistDynCalc::XmlHandler::XmlHandler(StatCumProbDistDynCalc *probabilityCalc,
543 m_xmlHandlerCumProbCalc = probabilityCalc;
544 m_xmlHandlerProject = project;
545 m_xmlHandlerCharacters =
"";
550 StatCumProbDistDynCalc::XmlHandler::~XmlHandler() {
553 m_xmlHandlerProject = NULL;
558 bool StatCumProbDistDynCalc::XmlHandler::startElement(
const QString &namespaceURI,
559 const QString &localName,
560 const QString &qName,
561 const QXmlAttributes &atts) {
563 m_xmlHandlerCharacters =
"";
564 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
565 if (qName ==
"quantileInfo") {
567 QString quantile = atts.value(
"quantile");
568 QString obsValue = atts.value(
"dataValue");
569 QString idealObs = atts.value(
"idealNumObsBelowQuantile");
570 QString actualObs = atts.value(
"actualNumObsBelowQuantile");
572 if (!quantile.isEmpty() && !obsValue.isEmpty()
573 && !idealObs.isEmpty() && !actualObs.isEmpty()) {
574 m_xmlHandlerCumProbCalc->m_quantiles.append(
toDouble(quantile));
575 m_xmlHandlerCumProbCalc->m_observationValues.append(
toDouble(obsValue));
576 m_xmlHandlerCumProbCalc->m_idealNumObsBelowQuantile.append(
toDouble(idealObs));
577 m_xmlHandlerCumProbCalc->m_numObsBelowQuantile.append(
toDouble(actualObs));
587 bool StatCumProbDistDynCalc::XmlHandler::characters(
const QString &ch) {
588 m_xmlHandlerCharacters += ch;
589 return XmlStackedHandler::characters(ch);
594 bool StatCumProbDistDynCalc::XmlHandler::endElement(
const QString &namespaceURI,
595 const QString &localName,
596 const QString &qName) {
597 if (!m_xmlHandlerCharacters.isEmpty()) {
599 m_xmlHandlerCumProbCalc->m_id = NULL;
600 m_xmlHandlerCumProbCalc->m_id =
new QUuid(m_xmlHandlerCharacters);
602 else if (qName ==
"numberCells") {
603 m_xmlHandlerCumProbCalc->m_numberCells =
toInt(m_xmlHandlerCharacters);
605 else if (qName ==
"numberQuantiles") {
606 m_xmlHandlerCumProbCalc->m_numberQuantiles =
toInt(m_xmlHandlerCharacters);
608 else if (qName ==
"numberObservations") {
609 m_xmlHandlerCumProbCalc->m_numberObservations =
toInt(m_xmlHandlerCharacters);
612 m_xmlHandlerCharacters =
"";
614 return XmlStackedHandler::endElement(namespaceURI, localName, qName);
619 QDataStream &StatCumProbDistDynCalc::write(QDataStream &stream)
const {
620 stream <<
m_id->toString()
622 << (qint32)m_numberQuantiles
633 QDataStream &StatCumProbDistDynCalc::read(QDataStream &stream) {
635 qint32 numCells, numQuantiles, numObservations;
647 m_id =
new QUuid(
id);
650 m_numberQuantiles = (
unsigned int)numQuantiles;
658 return scpddc.write(stream);
664 return scpddc.read(stream);
667 void StatCumProbDistDynCalc::validate() {
669 if (m_numberQuantiles == 0) {
670 QString msg =
"StatCumProbDistDynCalc will return no data until the quantiles have been set. "
677 QString msg =
"StatCumProbDistDynCalc will return no data until the number of observations "
679 "quantiles [" +
toString(m_numberQuantiles)
680 +
"] (i.e. number of nodes) selected.";
double max()
Returns the maximum observation so far included in the dynamic calculation.
The main project for cnetsuite.
StatCumProbDistDynCalc(unsigned int nodes=20, QObject *parent=0)
Construtor sets up the class to start recieving data.
double value(double cumProb)
Provides the value of the variable that has the given cumulative probility (according the current est...
~StatCumProbDistDynCalc()
Destroys StatCumProbDistDynCalc object.
int toInt(const QString &string)
Global function to convert from a string to an integer.
QList< double > m_observationValues
The calculated values of the quantiles, note this is dynamically changing as observations are added...
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
double toDouble(const QString &string)
Global function to convert from a string to a double.
This error is for when a programmer made an API call that was illegal.
unsigned int m_numberQuantiles
The number of quantiles being used to model the probility density function.
This class is used to approximate cumulative probibility distributions of a stream of observations wi...
unsigned int m_numberCells
The number of cells or histogram bins that are being used to model the probility density function...
#define _FILEINFO_
Macro for the filename and line number.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
QUuid * m_id
A unique ID for this object (useful for others to reference this object when saving to disk)...
QList< double > m_quantiles
The target quantiles being modeled, between 0 and 1.
void initialize()
Inializer, resets the class to start its dynamic calculation anew.
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...
void addObs(double obs)
Values for the estimated quantile positions are update as observations are added. ...
Adds specific functionality to C++ strings.
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 correspond...
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
double cumProb(double value)
Provides the cumulative probility, that is, the proportion of the distribution that is less than or e...
his enables stack-based XML parsing of XML files.