Blame view

build6/epsilon-master/apps/probability/law/law.cpp 3.14 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
137
138
  #include "law.h"
  #include <cmath>
  #include <float.h>
  
  namespace Probability {
  
  Law::Law() :
   Shared::CurveViewRange()
  {
  }
  
  float Law::xGridUnit() {
    return computeGridUnit(Axis::X, xMin(), xMax());
  }
  
  double Law::cumulativeDistributiveFunctionAtAbscissa(double x) const {
    if (!isContinuous()) {
      int end = std::round(x);
      double result = 0.0;
      for (int k = 0; k <=end; k++) {
        result += evaluateAtDiscreteAbscissa(k);
        /* Avoid too long loop */
        if (k > k_maxNumberOfOperations) {
          break;
        }
        if (result >= k_maxProbability) {
          result = 1.0;
          break;
        }
  
      }
      return result;
    }
    return 0.0;
  }
  
  double Law::rightIntegralFromAbscissa(double x) const {
    if (isContinuous()) {
      return 1.0 - cumulativeDistributiveFunctionAtAbscissa(x);
    }
    return 1.0 - cumulativeDistributiveFunctionAtAbscissa(x-1.0);
  }
  
  double Law::finiteIntegralBetweenAbscissas(double a, double b) const {
    if (b < a) {
      return 0.0;
    }
    if (isContinuous()) {
      return cumulativeDistributiveFunctionAtAbscissa(b) - cumulativeDistributiveFunctionAtAbscissa(a);
    }
    int start = std::round(a);
    int end = std::round(b);
    double result = 0.0;
    for (int k = start; k <=end; k++) {
      result += evaluateAtDiscreteAbscissa(k);
      /* Avoid too long loop */
      if (k-start > k_maxNumberOfOperations) {
        break;
      }
      if (result >= k_maxProbability) {
        result = 1.0;
        break;
      }
    }
    return result;
  }
  
  double Law::cumulativeDistributiveInverseForProbability(double * probability) {
    if (*probability >= 1.0) {
      return INFINITY;
    }
    if (isContinuous()) {
      return 0.0;
    }
    if (*probability <= 0.0) {
      return 0.0;
    }
    double p = 0.0;
    int k = 0;
    double delta = 0.0;
    do {
      delta = std::fabs(*probability-p);
      p += evaluateAtDiscreteAbscissa(k++);
      if (p >= k_maxProbability && std::fabs(*probability-1.0) <= delta) {
        *probability = 1.0;
        return k-1.0;
      }
    } while (std::fabs(*probability-p) <= delta && k < k_maxNumberOfOperations && p < 1.0);
    p -= evaluateAtDiscreteAbscissa(--k);
    if (k == k_maxNumberOfOperations) {
      *probability = 1.0;
      return INFINITY;
    }
    *probability = p;
    if (std::isnan(*probability)) {
      return NAN;
    }
    return k-1.0;
  }
  
  double Law::rightIntegralInverseForProbability(double * probability) {
    if (isContinuous()) {
      double f = 1.0 - *probability;
      return cumulativeDistributiveInverseForProbability(&f);
    }
    if (*probability >= 1.0) {
      return 0.0;
    }
    if (*probability <= 0.0) {
      return INFINITY;
    }
    double p = 0.0;
    int k = 0;
    double delta = 0.0;
    do {
      delta = std::fabs(1.0-*probability-p);
      p += evaluateAtDiscreteAbscissa(k++);
      if (p >= k_maxProbability && std::fabs(1.0-*probability-p) <= delta) {
        *probability = 0.0;
        return k;
      }
    } while (std::fabs(1.0-*probability-p) <= delta && k < k_maxNumberOfOperations);
    if (k == k_maxNumberOfOperations) {
      *probability = 1.0;
      return INFINITY;
    }
    *probability = 1.0 - (p - evaluateAtDiscreteAbscissa(k-1));
    if (std::isnan(*probability)) {
      return NAN;
    }
    return k-1.0;
  }
  
  double Law::evaluateAtDiscreteAbscissa(int k) const {
    return 0.0;
  }
  
  }