[英]Generating random number sequentially (X1,X2,X3) with uniform distribution in python
I need to generate X1,X2 and X3 as follows:我需要按如下方式生成 X1、X2 和 X3:
I am trying to code as:我正在尝试编码为:
N = 100
X1 = np.random.rand(N)
X2 = X1 - np.random.rand(N)
X3 = 1-X1-X2
But it's returning -(ve) value for X2.但它返回 X2 的 -(ve) 值。 How can I get (+)ve values along according to the condition.如何根据条件获得 (+)ve 值。
A couple of options:几个选项:
Use the random.uniform()
function:使用random.uniform()
function:
X1 = random.uniform(0, 1)
X2 = random.uniform(0, 1 - X1)
X3 = 1 - X1 - X2
Note: If this is for anything where you may have a motivated adversary, use the secrets
module rather than random
.注意:如果这是针对您可能有动机的对手的任何事情,请使用secrets
模块而不是random
。
Edit: Now that I read more carefully, you want an array of 100 samples;编辑:现在我更仔细地阅读了,你想要一个包含 100 个样本的数组; to get that, you'll have to scale a U(0, 1) variable to U(0, 1-X1) manually:为此,您必须手动将 U(0, 1) 变量缩放为 U(0, 1-X1) :
X1 = np.random.rand(N)
X2 = np.random.rand(N) * (1 - X1)
X3 = 1 - X1 - X2
np.random.rand(N)
generates from U(0,1)
np.random.rand(N)
从U(0,1)
生成
When you do X1 - np.random.rand(N)
, you generate from X1 - U(0,1)
instead of generating from U(0,1-X1)
, which is why you get negative values for X2
当您执行X1 - np.random.rand(N)
时,您从X1 - U(0,1)
生成而不是从U(0,1-X1)
生成,这就是为什么您会得到X2
的负值
Solution: Rescale the X2 answer: X2 = np.random.rand(N) * (1. - X1)
解决方案:重新调整 X2 答案: X2 = np.random.rand(N) * (1. - X1)
If you're actually seeking order statistics for three independent U(0,1) random variables, your proposed 3-step generating approach will not have the correct distributional properties.如果您实际上是在寻找三个独立 U(0,1) 随机变量的顺序统计信息,那么您提出的 3 步生成方法将不具有正确的分布属性。 With your proposed algorithm, the expected values of X1, X2, and X3 are 0.5, 0.25, and 0.25, respectively.使用您提出的算法,X1、X2 和 X3 的期望值分别为 0.5、0.25 和 0.25。 It's a toss-up as to which of the three is the max, and which is the min.至于这三个中哪个是最大值,哪个是最小值,这是一个折腾。
If you're interested in legitimate order statistics, the max of k independent uniforms can be generated by taking the k th root of a generated uniform value.如果您对合法订单统计感兴趣,可以通过取生成的统一值的第k个根来生成k个独立统一的最大值。 The next one can be taking the ( k - 1) th root scaled between 0 and the max, and so on.下一个可以取第 ( k - 1)个根,在 0 和最大值之间缩放,依此类推。 That would make the algorithm for k = 3 as follows:这将使k = 3 的算法如下:
import numpy as np
N = 100
X3 = np.random.uniform(size = N) ** (1/3)
X2 = np.sqrt(np.random.uniform(size = N)) * X3
X1 = np.random.uniform(size = N) * X2
Note that I've renamed the X's to use standard order statistics notation, where X1 denotes the min, X2 is the median (for k = 3), and X3 is the max.请注意,我已将 X 重命名为使用标准顺序统计符号,其中 X1 表示最小值,X2 是中位数(对于k = 3),X3 是最大值。 The algorithm guarantees correct ordering, and the expected values are 0.25, 0.5, and 0.75 for X1, X2, and X3, respectively, so they jointly partition the U(0,1) interval correctly.该算法保证了正确的排序,X1、X2和X3的期望值分别为0.25、0.5和0.75,因此它们共同正确划分了U(0,1)区间。 The preservation of ordering can easily be confirmed with:可以通过以下方式轻松确认订单的保留:
print(all(X1 <= X2))
print(all(X2 <= X3))
which prints True
for both checks regardless of N
.无论N
是什么,它都会为两次检查打印True
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.