Numworks Epsilon  1.4.1
Graphing Calculator Operating System
binomial_law.cpp
Go to the documentation of this file.
1 #include "binomial_law.h"
2 #include <assert.h>
3 #include <cmath>
4 
5 namespace Probability {
6 
8  TwoParameterLaw(20.0, 0.5)
9 {
10 }
11 
12 I18n::Message BinomialLaw::title() {
13  return I18n::Message::BinomialLaw;
14 }
15 
17  return Type::Binomial;
18 }
19 
21  return false;
22 }
23 
24 I18n::Message BinomialLaw::parameterNameAtIndex(int index) {
25  assert(index >= 0 && index < 2);
26  if (index == 0) {
27  return I18n::Message::N;
28  } else {
29  return I18n::Message::P;
30  }
31 }
32 
33 I18n::Message BinomialLaw::parameterDefinitionAtIndex(int index) {
34  assert(index >= 0 && index < 2);
35  if (index == 0) {
36  return I18n::Message::RepetitionNumber;
37  } else {
38  return I18n::Message::SuccessProbability;
39  }
40 }
41 
43  float min = 0.0f;
44  float max = m_parameter1 > 0.0f ? m_parameter1 : 1.0f;
45  return min - k_displayLeftMarginRatio * (max - min);
46 }
47 
49  float min = 0.0f;
50  float max = m_parameter1;
51  if (max <= min) {
52  max = min + 1.0f;
53  }
54  return max + k_displayRightMarginRatio*(max - min);
55 }
56 
59 }
60 
62  int maxAbscissa = m_parameter2 < 1.0f ? (m_parameter1+1)*m_parameter2 : m_parameter1;
63  float result = evaluateAtAbscissa(maxAbscissa);
64  if (result <= 0.0f || std::isnan(result)) {
65  result = 1.0f;
66  }
67  return result*(1.0f+ k_displayTopMarginRatio);
68 }
69 
70 
71 bool BinomialLaw::authorizedValueAtIndex(float x, int index) const {
72  if (index == 0) {
73  if (x != (int)x || x < 0.0f || x > 999.0f) {
74  return false;
75  }
76  return true;
77  }
78  if (x < 0.0f || x > 1.0f) {
79  return false;
80  }
81  return true;
82 }
83 
85  if (m_parameter1 == 0.0 && (m_parameter2 == 0.0 || m_parameter2 == 1.0)) {
86  return NAN;
87  }
88  if (*probability >= 1.0) {
89  return m_parameter1;
90  }
92 }
93 
95  if (m_parameter1 == 0.0 && (m_parameter2 == 0.0 || m_parameter2 == 1.0)) {
96  return NAN;
97  }
98  if (*probability <= 0.0) {
99  return m_parameter1;
100  }
101  return Law::rightIntegralInverseForProbability(probability);
102 }
103 
104 template<typename T>
106  if (m_parameter1 == 0) {
107  if (m_parameter2 == 0 || m_parameter2 == 1) {
108  return NAN;
109  }
110  if (floor(x) == 0) {
111  return 1;
112  }
113  return 0;
114  }
115  if (m_parameter2 == 1) {
116  if (floor(x) == m_parameter1) {
117  return 1;
118  }
119  return 0;
120  }
121  if (m_parameter2 == 0) {
122  if (floor(x) == 0) {
123  return 1;
124  }
125  return 0;
126  }
127  if (x > m_parameter1) {
128  return 0;
129  }
132  return std::exp(lResult);
133 }
134 
135 }
136 
138 template double Probability::BinomialLaw::templatedApproximateAtAbscissa(double x) const;
#define exp(x)
Definition: math.h:176
bool authorizedValueAtIndex(float x, int index) const override
#define NAN
Definition: math.h:30
#define assert(e)
Definition: assert.h:9
I18n::Message parameterNameAtIndex(int index) override
#define lgamma(x)
Definition: math.h:182
#define T(x)
Definition: events.cpp:26
static constexpr float k_displayBottomMarginRatio
Definition: law.h:45
I18n::Message parameterDefinitionAtIndex(int index) override
I18n::Message title() override
virtual double rightIntegralInverseForProbability(double *probability)
Definition: law.cpp:101
float xMin() override
virtual double cumulativeDistributiveInverseForProbability(double *probability)
Definition: law.cpp:68
#define log(x)
Definition: math.h:184
static constexpr float k_displayTopMarginRatio
Definition: law.h:44
#define N
Definition: b_log__D.c:69
float xMax() override
#define isnan(x)
Definition: math.h:43
double rightIntegralInverseForProbability(double *probability) override
double cumulativeDistributiveInverseForProbability(double *probability) override
Type type() const override
float yMin() override
T templatedApproximateAtAbscissa(T x) const
float yMax() override
static constexpr float k_displayRightMarginRatio
Definition: law.h:47
static constexpr float k_displayLeftMarginRatio
Definition: law.h:46
#define floor(x)
Definition: math.h:179
bool isContinuous() const override
float evaluateAtAbscissa(float x) const override
Definition: binomial_law.h:20