三角分布サンプリング; Triangular Distribution Sampling
概要
三角分布とは以下の式で表現される確率分布。なお定義域を\([x_{min}, x_{max}]\)、最頻値を\(x_{mode}\)とする。
$$f(x)=\begin{cases} \frac{2(x-x_{min})}{(x_{max}-x_{min})(x_{mode}-x_{min})} & \quad x_{min} \leq x \lt x_{mode} \\ \frac{2}{(x_{max}-x_{min})} & \quad x = x_{mode} \\ \frac{2(x_{max}-x)}{(x_{max}-x_{min})(x_{max}-x_{mode})} & \quad x_{mode} \lt x \leq x_{max} \end{cases} $$
ソースコード
namespace ExRandom.Continuous {
public class TriangularRandom : Random{
readonly MT19937 mt;
readonly double min, mode, max, thr, s0, s1;
public TriangularRandom(MT19937 mt, double min = -1, double mode = 0, double max = 1){
if(mt == null) {
throw new ArgumentNullException();
}
if(!(min < mode) || !(mode < max)) {
throw new ArgumentException();
}
this.mt = mt;
this.min = min;
this.mode = mode;
this.max = max;
this.thr = (mode - min) / (max - min);
this.s0 = (max - min) * (mode - min);
this.s1 = (max - min) * (max - mode);
}
public override double Next() {
double r = mt.NextDouble();
if(r < thr) {
return min + Math.Sqrt(r * s0);
}
else {
return max - Math.Sqrt((1.0 - r) * s1);
}
}
}
public class UnitTriangularRandom : Random{
readonly MT19937 mt;
public UnitTriangularRandom(MT19937 mt){
if(mt == null) {
throw new ArgumentNullException();
}
this.mt = mt;
}
public override double Next() {
return mt.NextDouble() + mt.NextDouble() - 1;
}
}
}
関連項目
メルセンヌ・ツイスタ
各種確率分布サンプリング基本クラス