0

I am trying to generate 500 numbers each from a uniform distribution over the intervals [-Kmin^N,Kmin] and [Kmax,Kmax^N].

Following is the code that I used:

Kmin=min(K1[0],K1[1],K1[2],K1[3],K1[4])  
Kmax=max(K1[0],K1[1],K1[2],K1[3],K1[4]) 
K3=np.zeros(500)
K4=np.zeros(500)
d=0
#the new weights
W_tilde=np.zeros(5)
while d<5:
    c=0
    total=0
    while c<500:
       #generating uniform random numbers from the sets [-Kmin^N,Kmin] and [Kmax,Kmax^N]
        K3[c]=Kmax+(Kmax**N-Kmax)*random.uniform(0,1)
        K4[c]=(-Kmin**N)+(Kmin+Kmin**N)*random.uniform(0,1)
        total=total+(K3[c]+K4[c])*W2[d]
        c=c+1
    W_tilde[d]=total/N
    d=d+1

However, I am getting the following error in the output.

<ipython-input-4-257ed5a3bfee>:70: RuntimeWarning: overflow encountered in double_scalars
  K3[c]=Kmax +((Kmax**N) -Kmax)*random.uniform(0,1)
<ipython-input-4-257ed5a3bfee>:71: RuntimeWarning: overflow encountered in double_scalars
  K4[c]=-(Kmin**N) +(Kmin+(Kmin**N))*random.uniform(0,1)
<ipython-input-4-257ed5a3bfee>:71: RuntimeWarning: invalid value encountered in double_scalars
  K4[c]=-(Kmin**N) +(Kmin+(Kmin**N))*random.uniform(0,1)

Can someone please tell me where I went wrong?

  • what is Kmax and N? If Kmax is bigger than 1 you probably run into the limits of what a double can take – Eumel Apr 07 '22 at 06:29
  • @Eumel Kmax is 681 and N is 1000. Thank you so much for the input! I changed it to [Kmax,12*Kmax] and [-12*Kmin, Kmin] – Purba Banerjee Apr 07 '22 at 06:34
  • Does this answer your question? [Python RuntimeWarning: overflow encountered in long scalars](https://stackoverflow.com/questions/7559595/python-runtimewarning-overflow-encountered-in-long-scalars) – Fran Arenas Apr 07 '22 at 06:49

1 Answers1

0

681^1000 is equal to 1.4031752403968567759489378274383e+2833 (using windows calculator), which is way too large and numpy cannot store this number in a single variable.

So I think the only two solutions are to break it into multiple variables or use smaller numbers.

Also some optimization for your code:

  1. Instead of multiplying and adding numbers to the result of random.uniform, you could pass them as parameters inside the function.
    • From numpy docs: random.uniform(low=0.0, high=1.0, size=None)
  2. Use numpy vectorized operations, it is much faster than looping over a list
  3. You could use for in place of while to make the code more compact

Your code after some rewriting (using the smaller values from your comment above)

W_tilde=np.zeros(5)
for d in range(5):
    K3 = random.uniform(Kmax,12*Kmax, 500)
    K4 = random.uniform(-12*Kmin,Kmin, 500)
    total = np.sum((K3+K4)*W2[d]) # vectorized operations
    W_tilde[d]=total/N