61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef TRIANGLE_H
|
||
#define TRIANGLE_H
|
||
|
||
#include <cmath>
|
||
#include <random>
|
||
#include "../random/RandomGenerator.h"
|
||
#include "../../Assertions.h"
|
||
#include "Normal.h"
|
||
|
||
namespace Distribution {
|
||
|
||
/**
|
||
* distribution that forms a triangle
|
||
* sigma defines the width (from mu to 0.0, half the width of the triangle's base)
|
||
* all values outside of the triangle are zero
|
||
*/
|
||
template <typename T> class Triangle {
|
||
|
||
private:
|
||
|
||
const T mu;
|
||
const T sigma;
|
||
const T area;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
Triangle(const T mu, const T sigma) : mu(mu), sigma(sigma), area(sigma*sigma) {
|
||
|
||
}
|
||
|
||
/** get probability for the given value */
|
||
T getProbability(const T val) const {
|
||
const T diff = std::abs(val - mu);
|
||
if (diff > sigma) {return 0;} // outside of triangle
|
||
return (sigma - diff) / area; // inside the triangle
|
||
|
||
}
|
||
|
||
/** get the probability for the given value */
|
||
static T getProbability(const T mu, const T sigma, const T val) {
|
||
Triangle<T> dist(mu, sigma);
|
||
return dist.getProbability(val);
|
||
}
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // TRIANGLE_H
|