使用(负)指数分布生成介于0和1之间的随机数(Generate random number between 0 and 1 with (negative)exponential distribution)
Heyho,
我试图用指数/负指数分布生成介于0.0和1.0之间的随机数。 有一篇文章告诉你必须得到“分位数函数” 。 但结果仍然大于1.0。 所以我需要以某种方式扩展我的等式。
我的目标是在一个范围内生成一个随机数,例如,更高/更低的值具有更高的概率。 (分发应该是可扩展的)
相关问题(不要将结果截断为[0,1]):
Heyho,
I'm trying to generate a random number between 0.0 and 1.0 with exponential/negative exponential distribution. There is an article, which tells that you have to get the "quantile function". But the results are still greater than 1.0. So I would need to scale my equation somehow.
My goal is to generate a random number in a range, where for example higher/lower values have a higher probability. (The distribution should be scalable)
Related questions(don't truncate the result to [0,1]):
原文:https://stackoverflow.com/questions/20385964
更新时间:2020-02-04 01:15
最满意答案
负指数分布支持范围[0,∞),因此我将您的问题解释为截断负指数的请求。 这种野兽的累积分布函数的下限l >= 0 ,上限h > l ,速率λ为
F(x) = (exp(-λl) - exp(-λx)) / (exp(-λl) - exp(-λh))
我们可以通过设置等于U ,一个统一的(0,1)随机数,并求解x来找到反演:
X = -ln(exp(-λl) - (exp(-λl) - exp(-λh)) * U) / λ
由于您分别指定了0和1的下限和上限,因此减少为
X = -ln(1 - (1 - exp(-λ)) * U) / λ
将U替换为您最喜欢的U(0,1)生成器的调用,这是您生成具有所需分布的X的算法。
这是用λ = 5生成的10,000个值的直方图。 较小的λ值给出较平坦的分布,较大的值表示更快的指数下降。
The negative exponential distribution has support on the range [0, ∞), so I'm interpreting your question as a request for a truncated negative exponential. The Cumulative Distribution Function for such a beast with lower limit l >= 0, upper limit h > l, and rate λ is
F(x) = (exp(-λl) - exp(-λx)) / (exp(-λl) - exp(-λh))
We can find the inversion by setting this equal to U, a uniform(0,1) random number, and solving for x:
X = -ln(exp(-λl) - (exp(-λl) - exp(-λh)) * U) / λ
Since you specified lower and upper limits of 0 and 1, respectively, this reduces to
X = -ln(1 - (1 - exp(-λ)) * U) / λ
Replace U with a call to your favorite U(0,1) generator and that's your algorithm for generating X's with the desired distribution.
Here's a histogram of 10,000 values generated with λ = 5. Smaller values of λ give a flatter distribution, larger values show a more rapid exponential drop off.
2013-12-05
相关问答
你可能需要一个 beta = 4 * (rand - 0.5)^3 + 0.5
功能或类似形状的东西 图形 分发结果: http : //jsfiddle.net/4hBqz/ You probably need a beta = 4 * (rand - 0.5)^3 + 0.5
function or something with a similar shape Graph Distribution results: http://jsfiddle.net/4hBqz/
您提到了对数分布,但它看起来像代码旨在生成截断的几何分布,而不是它的缺陷。 有多于一种称为对数分布的分布,它们都不常见。 请澄清你是否真的意味着其中之一。 您计算floor [log_2 U],其中U从1到(2 ^ max)+1均匀分布。 这有一个1/2 ^最大的机会产生最大值,但你把它夹到最大-1。 所以,你有1/2 ^ max的机会产生0,2 / 2 ^ max的机会产生1,4 / 2 ^ max的机会产生2,...达到1/2 + 1/2 ^ max产生最大-1的机会。 出现在你的代码中,但在
...
由于您可以访问统一的随机数生成器,因此生成与其他分布分布的随机数,通过反演方法,您可以轻松识别CDF。 所以,在[0,1)生成一个均匀的随机数u ,然后通过以下方式计算x : x = log(1-u)/( λ ) , 其中λ是指数分布的速率参数。 现在, x是具有指数分布的随机数。 注意上面的log是ln ,是自然对数。 Since you have access to a uniform random number generator, generating a random number di
...
好的,我看到了这个bug 你会产生size的事件数量。 你真的需要更多的事件来填充直方图 PS 填充箱#n(n在[0 ...大小)范围内)的概率由表达式给出 prob = exp(-gamma*n) - exp(-gamma*(n+1))
对于gamma等于0.01,例如, n约为1000,将给出大约4*10^-7概率。 因此,要在此垃圾箱中获得一个事件,您需要抽样约250万次 PPS 并且使用库指数采样虽然一般都很好,但不会给你买任何东西,因为据我所知你采样是可以的 Ok, I see the
...
这有帮助吗? (或者我误解了这个问题?) %#Set the parameters
T = 2000; %#Number of observations to simulate
Mu = 0.5; %#Exponential distribution parameter
LB = 0; %#Lower bound on exponential distribution
UB = 1; %#Upper bound on exponential distribution
%#Validate the
...
负指数分布支持范围[0,∞),因此我将您的问题解释为截断负指数的请求。 这种野兽的累积分布函数的下限l >= 0 ,上限h > l ,速率λ为 F(x) = (exp(-λl) - exp(-λx)) / (exp(-λl) - exp(-λh))
我们可以通过设置等于U ,一个统一的(0,1)随机数,并求解x来找到反演: X = -ln(exp(-λl) - (exp(-λl) - exp(-λh)) * U) / λ
由于您分别指定了0和1的下限和上限,因此减少为 X = -ln(1 -
...
这是因为您没有声明lambda的类型,更正了结果在您寻找的范围内。 如果未声明,旧的编译器会认为它是int 。 #include
#include
#include
#include
double ran_expo(double lambda){
double u;
u = rand() / (RAND_MAX + 1.0);
return -log(1- u) / lambda;
}
int
...
这取决于解释,但我要说你需要分别模拟从X到Y和从Y到X的时间,尽管速率相同。 如果列车双向行驶且平均速度相同,则并不意味着从X和Y出发的两列火车将同时到达另一点。 你没有使用 AYX
AXY
所以他们是多余的。 编写minAXB
...
为了在每次试验中生成概率为p的几何,给定一个返回均匀(0,1)结果的函数rand ,伪代码为: define geometric(p)
return ceiling(ln(1-rand) / ln(1-p))
这产生了多少试验,直到第一次成功。 如果您想要几何的替代定义(在第一次成功之前有多少次失败)减去1或使用floor而不是ceiling 。 To generate a geometric with probability p of success on each trial, give
...
我会收回它,没有对F呼唤。 所以,如果你想使用Distribution gem,我建议使用具有4个自由度的Chi 2 。 具有k自由度的Chi 2的模式等于k-2 ,因此对于4 df,您将获得2的模式,请参见此处 。 我的红宝石生锈,忍受着我 require 'distribution'
normal = Distribution::Normal.rng(0)
g1 = normal.call
g2 = normal.call
g3 = normal.call
g4 = normal.call
...