在python中实现特定分布 [英] Implementing specific distribution in python

查看:51
本文介绍了在python中实现特定分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以概率1/(2^(l-1))

我应该怎么做,而不是:

how i should do this rather then:

    x = random()
    if x < 0.5:
       return 2

等等

谢谢

推荐答案

这会很有趣...我对这些东西有点生疏,所以一个好的matematician可以解决我的推理.

This is going to be fun... I am a bit rusty with these things, so a good matematician could fix my reasoning.

要从公式生成分布,您首先需要进行积分并计算指定区间的累积密度函数.特别是我们需要开始计算归一化常数.

To generate a distribution from a formula you need first to do some integrals and calculate the cumulative density function for the specified interval. In particular we need to start to calculate the normalization constant.

这个积分给出,对于k":

This integral gives, for "k":

累积密度函数的意义"是获得属于我需要的区间的某个数字的概率是多少?".这个问题可以换个角度看:取一个小于或等于 10 的数的概率一定是 1".这导致以下等式有助于找到参数C".请注意,第一个 therm 是 k,第二个 therm 是 2^(1-x) 的一般积分,其中我将 x 替换为 10.

The "meaning" of the cumulative density function is "what's the probability to obtain a certain number that belong to the interval I need?". This question can be seen in another way: "the probability to take a number that is below or equal to 10 must be 1". This lead to the following equation that help to to find the parameter "C". Note that the first therm is the k, the second therm is the general integral of 2^(1-x) where I have replace x with 10.

解决这个问题,我们终于到达了 CDF(同样,找到它的方法可能更容易):

Solving this we finally reach the CDF (again, it is possible that the way to find it is easier):

此时我们需要反转 X 的 CDF.X 现在是我们在 0 和 1 之间的随机数生成器.公式是:

At this point we need to reverse the CDF for X. X is now our random number generator between 0 and 1. The formula is:

在 python 代码中,我尝试了以下操作:

In python code I tried the following:

import numpy as np
import matplotlib.pyplot as plt

a=[ 1-   np.log2(1-(1-2**(-9))*np.random.rand()) for i in range(10000)]

plt.hist(a, normed=True)

有意义吗?

这篇关于在python中实现特定分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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