Numworks Epsilon  1.4.1
Graphing Calculator Operating System
normal_law.cpp
Go to the documentation of this file.
1 #include "normal_law.h"
2 #include "erf_inv.h"
3 #include <assert.h>
4 #include <cmath>
5 #include <float.h>
6 #include <ion.h>
7 
8 namespace Probability {
9 
11  TwoParameterLaw(0.0f, 1.0f)
12 {
13 }
14 
15 I18n::Message NormalLaw::title() {
16  return I18n::Message::NormalLaw;
17 }
18 
20  return Type::Normal;
21 }
22 
24  return true;
25 }
26 
27 I18n::Message NormalLaw::parameterNameAtIndex(int index) {
28  assert(index >= 0 && index < 2);
29  if (index == 0) {
30  return I18n::Message::Mu;
31  } else {
32  return I18n::Message::Sigma;
33  }
34 }
35 
36 I18n::Message NormalLaw::parameterDefinitionAtIndex(int index) {
37  assert(index >= 0 && index < 2);
38  if (index == 0) {
39  return I18n::Message::MeanDefinition;
40  } else {
41  return I18n::Message::DeviationDefinition;
42  }
43 }
44 
45 float NormalLaw::xMin() {
46  if (m_parameter2 == 0.0f) {
47  return m_parameter1 - 1.0f;
48  }
49  return m_parameter1 - 5.0f*std::fabs(m_parameter2);
50 }
51 
52 float NormalLaw::xMax() {
53  if (m_parameter2 == 0.0f) {
54  return m_parameter1 + 1.0f;
55  }
56  return m_parameter1 + 5.0f*std::fabs(m_parameter2);
57 }
58 
59 float NormalLaw::yMin() {
61 }
62 
63 float NormalLaw::yMax() {
64  float maxAbscissa = m_parameter1;
65  float result = evaluateAtAbscissa(maxAbscissa);
66  if (std::isnan(result) || result <= 0.0f) {
67  result = 1.0f;
68  }
69  return result*(1.0f+ k_displayTopMarginRatio);
70 }
71 
72 float NormalLaw::evaluateAtAbscissa(float x) const {
73  if (m_parameter2 == 0.0f) {
74  return NAN;
75  }
76  return (1.0f/(std::fabs(m_parameter2)*std::sqrt(2.0f*M_PI)))*std::exp(-0.5f*std::pow((x-m_parameter1)/m_parameter2,2));
77 }
78 
79 bool NormalLaw::authorizedValueAtIndex(float x, int index) const {
80  if (index == 0) {
81  return true;
82  }
83  if (x <= FLT_MIN || std::fabs(m_parameter1/x) > k_maxRatioMuSigma) {
84  return false;
85  }
86  return true;
87 }
88 
89 void NormalLaw::setParameterAtIndex(float f, int index) {
91  if (index == 0 && std::fabs(m_parameter1/m_parameter2) > k_maxRatioMuSigma) {
92  m_parameter2 = m_parameter1/k_maxRatioMuSigma;
93  }
94 }
95 
97  if (m_parameter2 == 0.0f) {
98  return NAN;
99  }
100  return standardNormalCumulativeDistributiveFunctionAtAbscissa((x-m_parameter1)/std::fabs(m_parameter2));
101 }
102 
104  if (m_parameter2 == 0.0f) {
105  return NAN;
106  }
107  return standardNormalCumulativeDistributiveInverseForProbability(*probability)*std::fabs(m_parameter2) + m_parameter1;
108 }
109 
110 double NormalLaw::standardNormalCumulativeDistributiveFunctionAtAbscissa(double abscissa) const {
111  if (abscissa == 0.0) {
112  return 0.5;
113  }
114  if (abscissa < 0.0) {
115  return 1.0 - standardNormalCumulativeDistributiveFunctionAtAbscissa(-abscissa);
116  }
117  if (abscissa > k_boundStandardNormalDistribution) {
118  return 1.0;
119  }
120  return 0.5+0.5*std::erf(abscissa/std::sqrt(2.0));
121 }
122 
123 double NormalLaw::standardNormalCumulativeDistributiveInverseForProbability(double probability) {
124  if (probability >= 1.0) {
125  return INFINITY;
126  }
127  if (probability <= 0.0) {
128  return -INFINITY;
129  }
130  if (probability < 0.5) {
131  return -standardNormalCumulativeDistributiveInverseForProbability(1-probability);
132  }
133  return std::sqrt(2.0)*erfInv(2.0*probability-1.0);
134 }
135 
136 }
bool authorizedValueAtIndex(float x, int index) const override
Definition: normal_law.cpp:79
void setParameterAtIndex(float f, int index) override
#define exp(x)
Definition: math.h:176
double cumulativeDistributiveInverseForProbability(double *probability) override
Definition: normal_law.cpp:103
#define NAN
Definition: math.h:30
float yMin() override
Definition: normal_law.cpp:59
void setParameterAtIndex(float f, int index) override
Definition: normal_law.cpp:89
#define assert(e)
Definition: assert.h:9
float xMin() override
Definition: normal_law.cpp:45
#define M_PI
Definition: math.h:17
double cumulativeDistributiveFunctionAtAbscissa(double x) const override
Definition: normal_law.cpp:96
static constexpr float k_displayBottomMarginRatio
Definition: law.h:45
#define FLT_MIN
Definition: float.h:7
Type type() const override
Definition: normal_law.cpp:19
#define fabs(x)
Definition: math.h:178
double erfInv(double x)
Definition: erf_inv.cpp:31
I18n::Message title() override
Definition: normal_law.cpp:15
float evaluateAtAbscissa(float x) const override
Definition: normal_law.cpp:72
#define pow(x, y)
Definition: math.h:190
bool isContinuous() const override
Definition: normal_law.cpp:23
static constexpr float k_displayTopMarginRatio
Definition: law.h:44
I18n::Message parameterNameAtIndex(int index) override
Definition: normal_law.cpp:27
#define isnan(x)
Definition: math.h:43
#define INFINITY
Definition: math.h:29
#define erf(x)
Definition: math.h:174
I18n::Message parameterDefinitionAtIndex(int index) override
Definition: normal_law.cpp:36
float xMax() override
Definition: normal_law.cpp:52
float yMax() override
Definition: normal_law.cpp:63
#define sqrt(x)
Definition: math.h:196