简体   繁体   English

在 python 中按顺序生成均匀分布的随机数 (X1,X2,X3)

[英]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:

  1. Sample X1 ~ U(0, 1)样本 X1 ~ U(0, 1)
  2. Sample X2 ~ U(0, 1-X1)样本 X2 ~ U(0, 1-X1)
  3. Set X3 = 1- X1- X2.设置 X3 = 1- X1- X2。

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.

相关问题 Gurobi:如何添加约束x1 * x2 * x3 - Gurobi: how to add a constraint x1*x2*x3 torch.cat ((x1, x2, x3, x4), 1) 的大小不匹配 - The size of torch.cat ((x1, x2, x3, x4), 1) does not match Python合并数据集X1(t),X2(t)-&gt; X1(X2) - Python merge datasets X1(t), X2(t) -> X1(X2) 如何将 numpy 数组 [(x1,y1),(x2,y2),(x3,y3),...(xn,yn)] 转换为 [(x1,y1,x1^2,y1^2),(x2 ,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)] - How to convert a numpy array [(x1,y1),(x2,y2),(x3,y3),…(xn,yn)] to [(x1,y1,x1^2,y1^2),(x2,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)] I have a long list of dataframe and want to convert each to numpy array X1,X2,X3 given pandas dataframes df1,df2,df3 in python using for loop - I have a long list of dataframe and want to convert each to numpy array X1,X2,X3 given pandas dataframes df1,df2,df3 in python using for loop 解决大型方程组,具有复杂系数的N个equations_N未知数(如numbered_symbols [x1,x2,x3,..]) - Solving the large system of equations , N equations_N unknowns (as numbered_symbols [x1,x2,x3,..] ) with complex coefficients 如何使用or-tools在我们的MIP问题中设置y = max(x1,x2,x3)等式约束? - How to set an equality constraint like y = max(x1,x2,x3) in our MIP problem with or-tools? 假设x属于[x1,x2],则获取x1,x2 - Get x1,x2 provided that x belongs to [x1,x2] scipy.stats kstest 用于泊松分布/与 (x2) 边一起工作但不与 (x1) 边一起工作? - scipy.stats kstest for Poisson distribution / works with (x2) sided but not with (x1) sided? 加载标题为0,1,x1,x2,x3 ..的列 - Load columns x1,x2,x3.. with headers: 0,1,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM