Files
Upsilon/apps/probability/calculation/right_integral_calculation.cpp
Léa Saviot c248363b34 [apps/proba] Better handling of NAN values
For instance, chi squared law woth 1E8 degrees of freedom, comppute
P(X<?) = 0.4 will give P(X<undef) = undef
2019-08-21 14:47:17 +02:00

62 lines
1.4 KiB
C++

#include "right_integral_calculation.h"
#include <assert.h>
#include <ion.h>
#include <cmath>
namespace Probability {
RightIntegralCalculation::RightIntegralCalculation() :
Calculation(),
m_lowerBound(0.0),
m_result(0.0)
{
compute(0);
}
I18n::Message RightIntegralCalculation::legendForParameterAtIndex(int index) {
assert(index >= 0 && index < 2);
if (index == 0) {
return I18n::Message::RightIntegralFirstLegend;
}
return I18n::Message::RightIntegralSecondLegend;
}
void RightIntegralCalculation::setParameterAtIndex(double f, int index) {
assert(index >= 0 && index < 2);
if (index == 0) {
m_lowerBound = f;
}
if (index == 1) {
m_result = f;
}
compute(index);
}
double RightIntegralCalculation::parameterAtIndex(int index) {
assert(index >= 0 && index < 2);
if (index == 0) {
return m_lowerBound;
}
return m_result;
}
void RightIntegralCalculation::compute(int indexKnownElement) {
if (m_distribution == nullptr) {
return;
}
if (indexKnownElement == 0) {
m_result = m_distribution->rightIntegralFromAbscissa(m_lowerBound);
} else {
double currentResult = m_distribution->rightIntegralFromAbscissa(m_lowerBound);
if (std::fabs(currentResult - m_result) < std::pow(10.0, - Constant::LargeNumberOfSignificantDigits)) {
return;
}
m_lowerBound = m_distribution->rightIntegralInverseForProbability(&m_result);
if (std::isnan(m_lowerBound)) {
m_result = NAN;
}
}
}
}