简体   繁体   中英

Generating random number sequentially (X1,X2,X3) with uniform distribution in python

I need to generate X1,X2 and X3 as follows:

  1. Sample X1 ~ U(0, 1)
  2. Sample X2 ~ U(0, 1-X1)
  3. Set 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. How can I get (+)ve values along according to the condition.

A couple of options:

Use the 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 .

Edit: Now that I read more carefully, you want an array of 100 samples; to get that, you'll have to scale a U(0, 1) variable to U(0, 1-X1) manually:

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)

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

Solution: Rescale the X2 answer: 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. With your proposed algorithm, the expected values of X1, X2, and X3 are 0.5, 0.25, and 0.25, respectively. 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. The next one can be taking the ( k - 1) th root scaled between 0 and the max, and so on. That would make the algorithm for k = 3 as follows:

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. 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM