Pseudo Random Nubmer Sampling
https://en.wikipedia.org/wiki/Inverse\_transform\_samplinghtml
given a distribution's cumulative distribution function (CDF), generate sample numbers for this distribution.数组
typically based on uniform distribution variable X (or several of them), then somehow manipulate it, and get random variable Y which has the required distributiondom
Rejection Sampling if density function is known
one type of Monte-Carlo Method
see some noteside
target: sample from F=f(x)函数
idea: find an alternative G=g(x) which we already know, and that f(x)/g(x) <= c where c is a constant (ideally close to 1)ui
algorithm:this
- sample y from G;
- sample u from U[0,1];
- if u <= f(y)/c*g(y), then accept y; reject otherwise
- input 1: CDF of some distribution; for example, exponential distribution, F(x)=1-exp{\left(1-\lambda x\right)}
- input 2: a uniform distribution U[0,1]; for example, u=0.387;
- F(x) = y => x = F^{-1}\left(y\right) = -\frac{1}{\lambda}\ln{\left(1-y\right)} => x = -\frac{1}{\lambda}\ln\left(y\right)
- draw a value from U[0,1], and use it as CDF() value, then solve for the corresponding x value
- only used for generating Normal Distribution
- input: uniform distribution U[0,1]
- output: 2 independent standard normal distribution numbers
- Suppose U1 and U2 are independent random variables from U[0,1]
- let
and
, then Z0 and Z1 are both N(0,1) random variables
exampleidea
有一个数组,相似于:{{'Canada', 3}, {'USA', 5}, {'UK', 2}, {'Brasil', 3}}, 数组的类型是Country, 有两个变量, Country.name, Country.weight. 每一个国家都有一个权重,而后给一个output()函数,每次调用这个函数的时候就输出一个国家的名字,要使每一个国家被输出的几率相等。我用的方法是平摊weight: {Canada, Canada, USA, USA, USA, USA, UK, UK, Brasil, Brasil, Brasil}, 而后用Random 函数输出。Follow up : 若是这个权重的值很大很大,好比billio级别,应该怎么办。个人方法是相似于线段树,而后再用sum * Random(), 看这个区间坐落在哪里。orm
- target distribution is a discrete distribution, p(x='Canada')=3/13, p(x='USA')=5/13 etc.
- fit it into the Inverse Transform Sampling algorithm
- sample an integer from [1,13], {1,2,3} => Canada, {4,5,6,7,8} => USA, {9,10} => UK, {11,12,13} => Brasil