Blame view

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