I am trying to write a for loop to calculate error bars by using the derivative method. The formula is relatively simple, however I seem to be running into errors in my code with respect to vector/array sizes. There are a lot of defined vectors in my code, and I have checked the length of all of them. All of the inputs into the for-loop are 1x25 sized arrays.

I've tried changing the indices in the for loop from range(1,25) to range(0,24) but that doesn't seem to work.

# Creating vectors
dfdvg = np.zeros(25)
dfdxi0 = np.zeros(25)
sigsquare = np.zeros(25)
vgerr = vrs
xi0err = xi0s
Asq = np.zeros(25)
Bsq= np.zeros(25)
sig = np.zeros(25)

# calculating derivatives and error vectors
for i in range(0,24):
    dfdvg[i] = (np.multiply(rms[:,i],delta[:,i]))**-1
    dfdxi0[i] = -vr[:,i]/(vr[:,i]*(np.power(delta[:,i],2)))
    Asq[i] = np.power(np.multiply(dfdvg[i],vgerr[i]),2)
    Bsq[i] = np.power(np.multiply(dfdxi0[i],xi0err[i]),2)
    sigsquare[i] = Asq[i] + Bsq[i]
    sig[i] = np.power(sigsquare[i],0.5)

q = np.power(np.multiply(rms,delta),-1)
left = np.multiply(vg,q)
right = -(beta*H)/(3*(1+zeff))

What I want is the "sig" vector, representing the propagated error for each index.

1 Answers

0
luca.vercelli On Best Solutions

The problem is not in the dimensions of the array, the problem is in the shape. Unluckily you didn't write all your arrays. The point is, if you could just use arrays (25) instead of (1, 25), everything works fine:

vrs = np.random.rand(25)
vr = np.random.rand(25)
xi0s = np.random.rand(25)
rms = np.random.rand(25)
delta = np.random.rand(25)
vg = np.random.rand(25)

# Creating vectors
dfdvg = np.zeros(25)
dfdxi0 = np.zeros(25)
sigsquare = np.zeros(25)
vgerr = vrs
xi0err = xi0s
Asq = np.zeros(25)
Bsq= np.zeros(25)
sig = np.zeros(25)

# calculating derivatives and error vectors
for i in range(0,24):
    dfdvg[i] = (np.multiply(rms[i],delta[i]))**-1
    dfdxi0[i] = -vr[i]/(vr[i]*(np.power(delta[i],2)))
    Asq[i] = np.power(np.multiply(dfdvg[i],vgerr[i]),2)
    Bsq[i] = np.power(np.multiply(dfdxi0[i],xi0err[i]),2)
    sigsquare[i] = Asq[i] + Bsq[i]
    sig[i] = np.power(sigsquare[i],0.5)

q = np.power(np.multiply(rms,delta),-1)
left = np.multiply(vg,q)

(your last line of code seems unrelated)

So, in my opinion, your best option is to reshape your arrays:

vrs=vrs.reshape(25)