Python中的指数分布随机生成器(对数函数)? [英] Exponentially distributed random generator (log function) in python?

查看:971
本文介绍了Python中的指数分布随机生成器(对数函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很需要帮助,因为我陷入了代码的开头。

I really need help as I am stuck at the begining of the code.

我被要求创建一个函数来研究直方图的指数分布。该函数为x = −log(1-y)/λ。 λ是一个常数,我在代码中将其称为lamdr并简单地给定了10。我给N(随机数的数量)10并运行了代码,但结果和生成的随机数却给了我完全不同的结果。您可以在下面找到代码,我不知道出了什么问题,希望大家能帮我! (我使用python 2)

I am asked to create a function to investigate the exponential distribution on histogram. The function is x = −log(1−y)/λ. λ is a constant and I referred to that as lamdr in the code and simply gave it 10. I gave N (the number of random numbers) 10 and ran the code yet the results and the generated random numbers gave me totally different results; below you can find the code, I don't know what went wrong, hope you guys can help me!! (I use python 2)

import random
import math

N = raw_input('How many random numbers you request?: ')
N = int(N)
lamdr = raw_input('Enter a value:')
lamdr = int(lamdr)

def exprand(lamdr):
    y = []
    for i in range(N):
        y.append(random.uniform(0,1))
    return y
y = exprand(lamdr)
print 'Randomly generated numbers:', (y)

x = []
for w in y:
    x.append((math.log((1 - w) / lamdr)) * -1)
print 'Results:', x


推荐答案

在查看您提供的代码后,您似乎拥有了所需的组件,但没有将它们放在一起。

After viewing the code you provided, it looks like you have the pieces you need but you're not putting them together.

要求您使用指定的公式编写函数 exprand(lambdr)。 Python已经提供了一个名为 random.expovariate(lambd)的函数来生成指数,但是到底,我们还是可以自己制作。您的公式需要 y 的随机值,该值在0和1之间均匀分布。 随机模块的文档告诉我们 random.random()将为我们提供统一的(0,1)分布。因此,我们要做的就是用该函数调用替换公式中的 y ,我们开始了业务:

You were asked to write function exprand(lambdr) using the specified formula. Python already provides a function called random.expovariate(lambd) for generating exponentials, but what the heck, we can still make our own. Your formula requires a "random" value for y which has a uniform distribution between zero and one. The documentation for the random module tells us that random.random() will give us a uniform(0,1) distribution. So all we have to do is replace y in the formula with that function call, and we're in business:

def exprand(lambdr):
    return -math.log(1.0 - random.random()) / lambdr

历史记录:数学上,如果 y 具有制服(0, 1)分配, 1-y 也是如此。该算法的实现可以追溯到1950年代,通常会利用这一事实将计算简化为 -math.log(random.random())/ lambdr 。从数学上讲,这给出了分布正确的结果,因为对于任何连续随机变量X和常数c,P {X = c} = 0,但是在计算中,它将在Python中针对2 64 出现的1发生爆炸 random.random()中的零。这样做的一个历史依据是,当计算机比现在慢许多个数量级时,放弃一个额外的算术运算就被认为是微不足道的风险。另一个是当时流行的Prime Modulus乘性PRNG永远不会产生零。如今,它主要具有历史意义,并且是数学和计算有时会出现差异的一个有趣示例。

An historical note: Mathematically, if y has a uniform(0,1) distribution, then so does 1-y. Implementations of the algorithm dating back to the 1950's would often leverage this fact to simplify the calculation to -math.log(random.random()) / lambdr. Mathematically this gives distributionally correct results since P{X = c} = 0 for any continuous random variable X and constant c, but computationally it will blow up in Python for the 1 in 264 occurrence where you get a zero from random.random(). One historical basis for doing this was that when computers were many orders of magnitude slower than now, ditching the one additional arithmetic operation was considered worth the minuscule risk. Another was that Prime Modulus Multiplicative PRNGs, which were popular at the time, never yield a zero. These days it's primarily of historical interest, and an interesting example of where math and computing sometimes diverge.

回到当前的问题。现在,您只需调用该函数 N 次,并将结果存储在某个位置即可。循环或列表推导可能是这样做的候选人。这是后者的一个示例:

Back to the problem at hand. Now you just have to call that function N times and store the results somewhere. Likely candidates to do so are loops or list comprehensions. Here's an example of the latter:

abuncha_exponentials = [exprand(0.2) for _ in range(5)]

这将创建一个λ = 0.2的5个指数的列表。将0.2和5替换为用户提供的适当值,即可开展业务。打印列表,制作直方图,将其用作其他内容的输入...

That will create a list of 5 exponentials with λ=0.2. Replace 0.2 and 5 with suitable values provided by the user, and you're in business. Print the list, make a histogram, use it as input to something else...

exporand 替换为<列表理解中的code> expovariate 应该使用Python的内置指数生成器产生等效的结果。这就是抽象的功能之美,一旦有人编写了它们,您就可以将它们用于您的内心。

Replacing exporand with expovariate in the list comprehension should produce equivalent results using Python's built-in exponential generator. That's the beauty of functions as an abstraction, once somebody writes them you can just use them to your heart's content.

请注意,由于使用了随机性,这将使除非您每次将随机生成器种子到相同的值,否则每次运行它都会得到不同的结果。

Note that because of the use of randomness, this will give different results every time you run it unless you "seed" the random generator to the same value each time.

这篇关于Python中的指数分布随机生成器(对数函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆