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?
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:
random.uniform
, you could pass them as parameters inside the function.
random.uniform(low=0.0, high=1.0, size=None)
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