のようになります。私はポイントにほとんどの@ jwimberleyの考え方を踏襲し、私はここに私の結果を共有するだろうと思いました。
- コンストラクタ引数:
- CDF(正規化または非正規化)、PDFの 積分である私は、次のないクラスを作成しました。
- ディストリビューションの上限と下限
- (オプション)CDFのサンプルポイント数を示す解像度。
- CDF→乱数xからのマッピングを計算します。これは逆CDF関数です。
- によってランダム点を生成します。
- ランダム確率P
std::random
を使用して(0, 1]
間を生成します。
- pに対応するCDF値のマッピングでのバイナリ検索。 CDFと一緒に計算されたxを返します。近くのバケット間のオプションの線形統合が提供されます。そうでなければ、n ==離散ステップを解決します。
コード:ここで
// sampled_distribution.hh
#ifndef SAMPLED_DISTRIBUTION
#define SAMPLED_DISTRIBUTION
#include <algorithm>
#include <vector>
#include <random>
#include <stdexcept>
template <typename T = double, bool Interpolate = true>
class Sampled_distribution
{
public:
using CDFFunc = T (*)(T);
Sampled_distribution(CDFFunc cdfFunc, T low, T high, unsigned resolution = 200)
: mLow(low), mHigh(high), mRes(resolution), mDist(0.0, 1.0)
{
if (mLow >= mHigh) throw InvalidBounds();
mSampledCDF.resize(mRes + 1);
const T cdfLow = cdfFunc(low);
const T cdfHigh = cdfFunc(high);
T last_p = 0;
for (unsigned i = 0; i < mSampledCDF.size(); ++i) {
const T x = i/mRes*(mHigh - mLow) + mLow;
const T p = (cdfFunc(x) - cdfLow)/(cdfHigh - cdfLow); // normalising
if (! (p >= last_p)) throw CDFNotMonotonic();
mSampledCDF[i] = Sample{p, x};
last_p = p;
}
}
template <typename Generator>
T operator()(Generator& g)
{
T cdf = mDist(g);
auto s = std::upper_bound(mSampledCDF.begin(), mSampledCDF.end(), cdf);
auto bs = s - 1;
if (Interpolate && bs >= mSampledCDF.begin()) {
const T r = (cdf - bs->prob)/(s->prob - bs->prob);
return r*bs->value + (1 - r)*s->value;
}
return s->value;
}
private:
struct InvalidBounds : public std::runtime_error { InvalidBounds() : std::runtime_error("") {} };
struct CDFNotMonotonic : public std::runtime_error { CDFNotMonotonic() : std::runtime_error("") {} };
const T mLow, mHigh;
const double mRes;
struct Sample {
T prob, value;
friend bool operator<(T p, const Sample& s) { return p < s.prob; }
};
std::vector<Sample> mSampledCDF;
std::uniform_real_distribution<> mDist;
};
#endif
は、結果のいくつかのプロットです。与えられたPDFに対して、まず積分によってCDFを解析的に計算する必要があります。
対数線形
正弦波
次のデモを自分でこれを試してみることができます:
// main.cc
#include "sampled_distribution.hh"
#include <iostream>
#include <fstream>
int main()
{
auto logFunc = [](double x) {
const double k = -1.0;
const double m = 10;
return x*(k*std::log(x) + m - k); // PDF(x) = k*log(x) + m
};
auto sinFunc = [](double x) { return x + std::cos(x); }; // PDF(x) = 1 - sin(x)
std::mt19937 gen;
//Sampled_distribution<> dist(logFunc, 1.0, 1e4);
Sampled_distribution<> dist(sinFunc, 0.0, 6.28);
std::ofstream file("d.txt");
for (int i = 0; i < 100000; i++) file << dist(gen) << std::endl;
}
データはPythonでプロットされます。
// dist_plot.py
import numpy as np
import matplotlib.pyplot as plt
d = np.loadtxt("d.txt")
fig, ax = plt.subplots()
bins = np.arange(d.min(), d.max(), (d.max() - d.min())/50)
ax.hist(d, edgecolor='white', bins=bins)
plt.show()
実行とデモ:ASが他の場所で指摘
clang++ -std=c++11 -stdlib=libc++ main.cc -o main; ./main; python dist_plot.py
C++と同じくらい数学があります。たとえば、https://en.wikipedia.org/wiki/Inverse_transform_samplingを参照してください。 – jwimberley
ドメインとは何ですか? –
@ YvesDaoust最初の問題については、1 - > 1.4e7の間でした。私はそれをどのように解決したのか答えを付け加えました。 – pingul