mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
THe distribution now represents the number of trials neded before a
success, so is defined for k in {1, 2, 3, ...}
49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#include "geometric_distribution.h"
|
|
#include <assert.h>
|
|
#include <cmath>
|
|
#include <ion.h>
|
|
|
|
namespace Probability {
|
|
|
|
float GeometricDistribution::xMin() const {
|
|
return -k_displayLeftMarginRatio * xMax();
|
|
}
|
|
|
|
float GeometricDistribution::xMax() const {
|
|
assert(m_parameter1 != 0.0f);
|
|
return 5/m_parameter1 * (1.0f + k_displayRightMarginRatio);
|
|
}
|
|
|
|
float GeometricDistribution::yMax() const {
|
|
float result = evaluateAtAbscissa(1.0); // Tha probability is max for x == 1
|
|
return result * (1.0f + k_displayTopMarginRatio);
|
|
}
|
|
|
|
bool GeometricDistribution::authorizedValueAtIndex(float x, int index) const {
|
|
assert(index == 0);
|
|
if (x <= 0.0f || x > 1.0f) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template<typename T>
|
|
T GeometricDistribution::templatedApproximateAtAbscissa(T k) const {
|
|
constexpr T castedOne = static_cast<T>(1.0);
|
|
if (k < castedOne) {
|
|
return static_cast<T>(0.0);
|
|
}
|
|
T p = static_cast<T>(m_parameter1);
|
|
if (p == castedOne) {
|
|
return k == castedOne ? castedOne : static_cast<T>(0.0);
|
|
}
|
|
// The result is p * (1-p)^{k-1}
|
|
T lResult = (k - castedOne) * std::log(castedOne - p);
|
|
return p * std::exp(lResult);
|
|
}
|
|
|
|
}
|
|
|
|
template float Probability::GeometricDistribution::templatedApproximateAtAbscissa(float x) const;
|
|
template double Probability::GeometricDistribution::templatedApproximateAtAbscissa(double x) const;
|