4. 被積分関数を定義しておき, sampler.Integrateで計算する.
SMCTC: Sequential Monte Carlo template Class
http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic-research/johansen/smctc
マニュアルメモ
http://www.jstatsoft.org/v30/i06/paper
4. The SMCTC Library
4.1. Library and program structure
smc::sampler : ライブラリのアルゴリズム全体に対応するもの
smc::particle : 個別の粒子の値と重みを保持するもの
smc::history : 前回のiterationの後のsamplerの状態を表すもの
smc::historyelement : 前回のsamplerの状態を保持するためにhistoryによって用いられるもの
smc::historyflags : samplerの履歴に関する簡単な情報を保持するためのもの. 現状ではリサンプリングを行ったかどうかを保持することに用
いられている.
smc::moveset : 粒子の初期化, 提案分布, 追加的なMCMCによる変化を扱う
smc:rng : GSL random number facilitiesに対するラッパー. 現状では一部の機能のみ対応. 直接GSLの機能を利用することも可能.
smc:gslrnginfo : GSL 乱数生成機に関する情報を扱う
smc::exception : 例外処理に用いるもの
SMCTCを用いたSequential Monte Carloで実行される手続きの概要.
- 初期化
smc::sampler が利用可能になる前に, samplerが行うこととSMCアルゴリズムのパラメータを指定する必要がある.
- 繰り返し
5. smc::sampler が生成された後は完了もしくは結果が得られるまで繰り返す
- 出力
samplerそのものと粒子セットに基づく何らかの期待値を出力する
4.2. Creating, configuring and running a sampler: smc::sampler
まずは新しいsamplerオブジェクトを作成する. 粒子数を指定する必要がある(粒子数は固定).
smc::samplerのコンストラクタの引数は基本的には2つ. 粒子数と, samplerの履歴(各繰り返しにおいて生成される粒子セット)保持メモリ容
量を指定する値(SMC_HISTORY_RAM or SMC_HISTORY_NONE)である. NONEの方を指定すると直近に生成された粒子セットのみを保
持する.
smc::samplerはテンプレートクラスなので型を指定する必要がある. ここでの型はクラスもしくはC++のデータ型のことであり, 状態空間の
点を表すものである.
状態空間が1次元の実数全体, 1000粒子, 履歴保持なしのsamplerをコンストラクトするなら,
smc::sampler<double> Sampler(1000, SMC_HISTORY_NONE);
同様に, 状態空間がn次元の実数全体, 1000粒子, 履歴有りなら
smc::sampler<std::vector<double> > Sampler(1000, SMC_HISTORY_RAM);
とする.
提案分布と重み(Proposals and importance weights)
粒子の遷移と重み付けを担う関数はsmc::movesetを経由してsmc::samplerに引き渡す.
movesetはsamplerに対してSetMoveSetメンバ関数で提供される. このmovesetは事前に初期化されていなければならない.
Section 4.3参照.
リサンプリング(Resampling)
幾つかのリサンプリング方法が指定できる. 常にリサンプリングする, しない, ESSが指定された閾値を下回るときリサンプリングするのいず
れかである. SetResampleParams(ResampleType, double)によって設定する. ResampleTypeはenumerationであり, 次の表から選ぶ.
2つめのdoubleの値が負の場合はリサンプリングしない.
[0,1]の間の場合は, ESSの全粒子数に対する割合がその値を下回るときに,
1より大きい場合は, ESSがその値より小さい場合に, リサンプリングする. 全粒子数より大きい値を設定すれば, 常にリサンプリングすること
になる.
MCMCによる散らし(MCMC Diversification)
リサンプリングの後にMCMCによる遷移が適用出来る. 適切に設定されたmovesetをsamplerに渡すことで自動的に実行可能である. Section
5.2 参照.
アルゴリズムの実行(Running the algorithm)
粒子セットの初期化を実行するsmc::samplerのInitialize(). これは履歴を消去し, 全粒子に一度ずつmovesetに設定された関数を実行することで
初期化する.
初期化が済めば繰り返し処理を行う. Iterate()とIterateUntil(int)の2通りがある.
出力(Output)
粒子セットに基づく経験測度に関する積分が実行可能である. 粒子セットの位置と重みも取得できる.
- Simple integration
double integrand(const T& , void *)
where T denotes the type of the smc::sampler template class in use.
Sampler.Integrate(integrand, void *(p));
where p is a pointer to auxiliary information that is passed directly to the integrand function
via its second argument { this may be safely set to NULL if no such information is required
by the function.
例5.1に説明有り.
- Path-sampling integration
http://www.cis.upenn.edu/~taskar/courses/cis700-sp08/papers/gelman-meng.pdf
省略しておく.
6. - General output
For more general tasks it is possible to access the locations and weights of the particles directly.
Three low-level member functions provide access to the current generation of particles, each
takes a single integer argument corresponding to a particle index.
GetParticleValue(int n)
GetParticleLogWeight(int n)
GetParticleWeight(int n)
n番目の粒子に対する, 値の参照, 規格化されていない対数重み, 規格化されていない重みをそれぞれ返す.
smc::sampler の GetHistory() も使えるが, 詳しくはmanualをみる.
Finally, a human-readable summary of the state of the particle system can be directed to an
output stream using the usual << operator
4.3. Specifying proposals and importance weights: smc::moveset
- Initializing the particles
samplerが保持する粒子の初期化関数
smc::particle<T> fInitialise(smc::rng *pRng);
T: samplerの型
particleのコンストラクタ
smc::particle<T> (value, dLogWeight)
- Moving and weighting the particles
提案分布
void fMove(long, smc::particle<T> &, smc::rng *)
samplerが呼ぶ時, 1番目のlongは現在の繰り返し番号に設定される
-- Changing the value
GetValue() and SetValue(T)
GetValuePointer()
-- Updating the weight
GetWeight(): 規格化されていない重み
GetLogWeight()
SetWeight(double)
SetLogWeight(double)
AddToLogWeight(double)
MultiplyWeightBy(double)
※粒子の重み更新において, AddToLogWeightを用いた例がRcppSMCにある.
意味的にSetLogWeightでないのか? 要確認
粒子の重みの更新方法について確認する.
規格化はsampler<space>のiterateEssの中でやっている.
- Mixtures of moves
複数のtransition kernelを用いる.
For example, if the sampler is of type T and fMv1 and fMv2 each correspond to a valid move function then the following
code would produce an array of the appropriate type named pfMoves which contains pointers to these two functions:
void (*pfMoves[])(long, smc::particle<T> &,smc::rng*) = {fMv1, fMv2};
long fSelect(long lTime, const smc::particle<T> & p, smc::rng *pRng)
- Additional MCMC moves
If MCMC moves are required then one should simply produce an additional move function with an almost identical prototype to that used for
proposal moves. The one difference is that normal proposals have a void return type, whilst the MCMC move function should return int. The
function should return zero if a move is rejected and a positive value if it is accepted.
- Creating an smc::moveset object
-- Single-proposal movesets
moveset ( particle<T>(*pfInit)(rng *),
void(*pfNewMoves)(long, particle<T> &, rng *),
int(*pfNewMCMC)(long, particle<T> &, rng *))
optionally, a pointer to an MCMC move function (if one is not required, then the argument
specifying this function should be set to NULL)
-- Mixture-proposal movesets
moveset ( particle<T>(*pfInit)(rng *),
long(*pfMoveSelector)(long, const particle<T> &, rng *),
long nMoves,
void(**pfNewMoves)(long, particle<T> &, rng *),
int(*pfNewMCMC)(long, particle<T> &, rng *))
参照:5.2節
-- Using a moveset
samplerのSetMoveSetで設定する.
7. 4.4. Error handling: smc::exception
const char* szFile;
long lLine;
long lCode;
const char* szMessage;
参照:5.1節
4.5. Random number generation
特に気にしなくて良いはずであるが, 必要ならマニュアルを見る.
GSL_RNG_SEED
GSL_RNG_TYPE
参照:5.1節
5.1節の例では適当なディレクトリでBASH promptにおいて
GSL_RNG_TYPE=ranlux GSL_RNG_SEED=36532673278 ./pf
という設定値を用いている.
5. Examples applications
a simple particle fillter in Section 5.1 and
a more involved SMC sampler which estimates rare event probabilities in Section 5.2.
5.1. A simple particle filter
The almost constant velocity model in the tracking literature.
2次元粒子の運動.
位置のみ不完全だが測定可能(=誤差含む), 速度は測定できない, 測定は離散時間