1

I am trying to understand the random variables from scipy.stats. I can sample from a uniform random variable:

from scipy.stats import uniform
print(uniform.rvs(size=1000))

But how can I make a random variable that with 0.5 probability samples uniformly from 0..1 and with 0.5 prob samples uniformly from 5..6?

I could write a loop that picks a random number between 0 and 1. If it is < .5, then picks a random number between 0 and 1. If it is >= .5 pick a random number between 0 and 1 and add 5. But I would really like to be able to call it like:

mixed_uniform.rvs(size=1000)

I also need to use the survival function of this mixed distribution.

Simd
  • 19,447
  • 42
  • 136
  • 271
  • Doesn't the distribution need to be continuous to provide the survival function? – rcriii Jul 01 '19 at 17:59
  • @rcriii No. You just need to be able to integrate it. – Simd Jul 01 '19 at 18:40
  • 1
    Aha! As you can see, I did not address the survival function in my answer below, since I don't have any experience with it. – rcriii Jul 02 '19 at 18:50
  • felipa, did you perhaps mean like either of the ff? 1 - Also, a lot of people who illegally download music/movies do it more because of the convenience than the financial advantage. Conversely, a lot of people who legally download do it for convenience despite the financial disadvantage. If you own an iphone or ipad, for example, it is much simpler to download what you want from itunes or subscribe to netflix than to find a way to get it illegally. – BCLC Sep 24 '20 at 03:46
  • 2 - Also, a lot of people who legally download music/movies do it more because of the convenience despite the financial disadvantage. If you own an iphone or ipad, for example, it is much simpler to download what you want from itunes or subscribe to netflix than to find a way to get it illegally. – BCLC Sep 24 '20 at 03:46
  • https://economics.stackexchange.com/a/6330/4483 – BCLC Sep 24 '20 at 03:46

2 Answers2

1

For the distribution, a mix of a custom function to do the transformation, then use vectorize() to apply it will be more efficient than looping.

In [1]: from scipy.stats import uniform

In [2]: r = uniform.rvs(size=1000)

In [3]: r
Out[3]:
array([7.48816182e-02, 4.63880797e-01, 8.75315477e-01, 3.61116729e-01,
       ...
       3.13473322e-01, 3.45434625e-01, 9.49993090e-01, 1.55553018e-01])

In [4]: type(r)
Out[4]: numpy.ndarray

In [8]: def f(a):
   ...:     a *= 2
   ...:     if a > 1: a += 4
   ...:     return a
   ...:


In [10]: import numpy

In [11]: vf = numpy.vectorize(f)

In [12]: r2 = numpy.vectorize(f)(r)

In [13]: r2
Out[13]:
array([1.49763236e-01, 9.27761594e-01, 5.75063095e+00, 7.22233457e-01,
       ...
       6.26946644e-01, 6.90869250e-01, 5.89998618e+00, 3.11106036e-01])

In [14]: max(r2)
Out[14]: 5.999360665646841

In [15]: min(r2)
Out[15]: 0.0004563758727054168

In [17]: len([x for x in r2 if x<=2])
Out[17]: 504

In [18]: len([x for x in r2 if x>=5])
Out[18]: 496
rcriii
  • 687
  • 6
  • 9
0

I generate a random distribution of 1000 numbers between 0 and 1 and randomly chose a element from the list. if the element is greater than .5 then add 5

from scipy.stats import uniform
import random
min_number=0
max_number=1
size=1000

number_pool= uniform.rvs(min_number,max_number,size=size)

plt.hist(number_pool)
plt.show()

def getValue(number_pool):
    val=random.choice(number_pool)
    if val>.5: 
        val+=5
    return val
print(getValue(number_pool))
Golden Lion
  • 3,840
  • 2
  • 26
  • 35