I am trying to complete this, where I have to solve five ordinary
differential equations using odeint and reproduce figures given in that task.
Here is my code:
import scipy as sp
import scipy.interpolate as ip
import numpy as np
import matplotlib.pyplot as pl
d = 8.64
Mu1 = 4.95*10**2
Mu2 = 4.95*10**(-2)
vs = 0.12
vd = 1.23
w = 10**(-3)
k1 = 2.19*10**(-4)
k2 = 6.12*10**(-5)
k3 = 0.997148
k4 = 6.79*10**(-2)
p0 = 1.00
sigmas0 = 2.01
sigmad0 = 2.23
alphas0 = 2.20
alphad0 = 2.26
hs = (sigmas0-(sigmas0**(2)-k3*alphas0*(2*sigmas0-alphas0))**(1/2))/k3
cs = (alphas0-hs)/2
ps = k4*(hs**2)/cs
t_points = [ 1000, 1850, 1950, 1980, 2000, 2050, 2080, 2100, 2120, 2150, 2225, 2300, 2500, 5000 ]
y_points = [ 0.0, 0.0, 1.0, 4.0, 5.0, 8.0, 10.0, 10.5, 10.0, 8.0, 3.5, 2.0, 0.0, 0.0 ]
t1 = np.array(t_points)
y1 = np.array(y_points)
new_length = 1000
new_t = np.linspace(t1.min(), t1.max(), new_length)
new_y2 = ip.pchip_interpolate(t1, y1, new_t)
pl.plot(t_points,y_points,'o', new_t,new_y2)
pl.show()
ft = sp.interpolate.interp1d(new_t, new_y2)
def equations(x, t1):
p = x[0]
alphad = x[1]
alphas = x[2]
sigmad = x[3]
sigmas = x[4]
dpdt = (ps-p)/d + ft/Mu1
dalphaddt = (1/vd)*(k2-w*(alphad-alphas))
dalphasdt = (1/vs)*(w*(alphad-alphas)-k2)
dsigmaddt = (1/vd)*(k1-w*(sigmad-sigmas))
dsigmasdt = (1/vs)*(w*(sigmad-sigmas)-k1-(ps-p)/d*Mu2)
return [dpdt, dalphaddt, dalphasdt, dsigmaddt, dsigmasdt]
solve = sp.integrate.odeint(equations, [p0, alphad0, alphas0, sigmad0, sigmas0], t1)
It seems like this part:
dpdt = (ps-p)/d + ft/Mi1
is wrong and I have no idea how to solve it.
The error says:
TypeError: unsupported operand type(s) for /: 'interp1d' and 'float'.
Any ideas and advices are much appreciated.
EDIT: When I apply changes suggested by meowgoesthedog, I get error:
Traceback (most recent call last):
File "<ipython-input-5-324757833872>", line 1, in <module>
runfile('E:/Data/Project 2/project2.py', wdir='E:/Data/Project 2')
File "D:\Programs\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "D:\Programs\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/Data/Project 2/project2.py", line 59, in <module>
solve = odeint(equations, [p0, alphad0, alphas0, sigmad0, sigmas0], t1)
File "D:\Programs\Anaconda3\lib\site-packages\scipy\integrate\odepack.py", line 233, in odeint
int(bool(tfirst)))
File "E:/Data/Project 2/project2.py", line 51, in equations
dpdt = (ps-p)/d + ft(t1)/Mu1
File "D:\Programs\Anaconda3\lib\site-packages\scipy\interpolate\polyint.py", line 79, in __call__
y = self._evaluate(x)
File "D:\Programs\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 664, in _evaluate
below_bounds, above_bounds = self._check_bounds(x_new)
File "D:\Programs\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 696, in _check_bounds
raise ValueError("A value in x_new is above the interpolation "
ValueError: A value in x_new is above the interpolation range.
`
According to interp1d's documentation:
ynew = f(xnew) # use interpolation function returned by interp1d
It returns a function / callable object which takes a value x and returns the interpolated value of f(x). In your case "x" = t:
dpdt = (ps-p)/d + ft(t1)/Mu1 # pass t1 to ft to obtain interpolated value
UPDATE
This new error is due to odeint sampling the function f(t) at values of t beyond the last value of t_points. This is necessary for error correction and there is no option to prevent odeint from doing so. However, we can instead extrapolate f(t) beyond the supplied samples, using InterpolatedUnivariateSpline:
from scipy.interpolate import InterpolatedUnivariateSpline
...
ft = InterpolatedUnivariateSpline(t1, y1, k=1)
As with interp1d, this returns a function with the same signature. However, after applying this fix the result becomes:
Which is of course incorrect.
You have declared hs, cs, ps outside of the function as constants. In-fact they are functions of the alpha* and sigma* variables, so have to be evaluated during each call to equation:
def equations(x, t):
p = x[0]
alphad = x[1]
alphas = x[2]
sigmad = x[3]
sigmas = x[4]
hs = (sigmas-(sigmas**(2)-k3*alphas*(2*sigmas-alphas))**(1/2))/k3
cs = (alphas-hs)/2
ps = k4*(hs**2)/cs
dpdt = (ps-p)/d + ft(t)/Mu1
dalphaddt = (1/vd)*(k2-w*(alphad-alphas))
dalphasdt = (1/vs)*(w*(alphad-alphas)-k2)
dsigmaddt = (1/vd)*(k1-w*(sigmad-sigmas))
dsigmasdt = (1/vs)*(w*(sigmad-sigmas)-k1-(ps-p)/d*Mu2)
return [dpdt, dalphaddt, dalphasdt, dsigmaddt, dsigmasdt]
The result now matches the graph in the exercise... almost.
You passed t1 as the horizontal axis variable to odeint. It only has 14 elements which is too few for a smooth output. Pass new_t instead:
solve = ig.odeint(equations, [p0, alphad0, alphas0, sigmad0, sigmas0], new_t)
The result now exactly matches the expected one!
Related
In my python code, I keep getting the following error:
TypeError: No loop matching the specified signature and casting was found for ufunc svd_n
the code is as follows:
import numpy as np
from numpy.linalg import norm
def sdm_3eqs():
def f_bold(x):
return [15*x[0] + x[1]**2 - 4*x[2] - 15, x[0]**2 + 10*x[1] - x[2] - 10, x[1]**3 - 25*x[2] + 24]
def f(x):
f_n = []
for i in range(len(x)):
f_i = f_bold[i]**2
f_n.append(f_i)
return np.sum(f_n)
def M(x):
m = np.array([[15, 2*x[0], 0], [2*x[1], 10, 3*x[1]**2], [-4, -1, -25]])
return m
def grad_f(x):
return 2*M(x)*f_bold(x)
def d(x):
return -grad_f(x)/norm(grad_f(x), ord=2)
def s_prime(x, alpha, d):
return grad_f(x + alpha*d)*d
x = [0.5, 0.5, 0.5]
iter = 0
err = 100
while err > 0.005:
x_k = x
d_k = d(x_k)
m = 0
sprime = 300
alpha_l = 0
alpha_u = 1.5
alpha = (alpha_l+alpha_u)/2
while abs(sprime) > 0.0005:
alpha = (alpha_l+alpha_u)/2
sprime = s_prime(x_k, alpha, d_k)[0][0]
if abs(sprime) < 0.001:
break
elif sprime > 0:
alpha_u = alpha
else:
alpha_l = alpha
m += 1
iter += 1
x = x_k + alpha*d_k
err = norm(grad_f(x), ord=2)/max(1, norm(f_bold(x), ord=2))
print(f'f_bold: {f_bold(x)}')
sdm_3eqs()
I am unsure why but it says the type error come from line 57 in the code:
err = norm(grad_f(x), ord=2)/max(1, norm(f_bold(x), ord=2))
If anyone can help, that would be great!
EDIT:
Traceback (most recent call last):
File "/Users/aidanpayne/Desktop/Scripts/Python/University of Greenwich/MATH1157/Scripts/Steepest Descent Method.py", line 61, in <module>
sdm_3eqs()
File "/Users/aidanpayne/Desktop/Scripts/Python/University of Greenwich/MATH1157/Scripts/Steepest Descent Method.py", line 57, in sdm_3eqs
err = norm(grad_f(x), ord=2)/max(1, norm(f_bold(x), ord=2))
File "<__array_function__ internals>", line 5, in norm
File "/Users/aidanpayne/opt/anaconda3/lib/python3.8/site-packages/numpy/linalg/linalg.py", line 2579, in norm
ret = _multi_svd_norm(x, row_axis, col_axis, amax)
File "/Users/aidanpayne/opt/anaconda3/lib/python3.8/site-packages/numpy/linalg/linalg.py", line 2355, in _multi_svd_norm
result = op(svd(y, compute_uv=False), axis=-1)
File "<__array_function__ internals>", line 5, in svd
File "/Users/aidanpayne/opt/anaconda3/lib/python3.8/site-packages/numpy/linalg/linalg.py", line 1673, in svd
s = gufunc(a, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting was found for ufunc svd_n
It looks like the M(x) function is returning a 3-dimensional array, of structure
[
[ a [b c d] e ]
[[f g h] i [k l m]]
[ n o p ]
]
and then you're attempting to matrix-multiply that with the result of f_bold(x) and calculate the norm.
I believe the error is related to trying to calculate the norm of this irregular matrix. In particular, check your function definition for M(x) to verify the shape/regularity of its returned array.
line 17: m = np.array([[15, 2*x[0], 0], [2*x[1], 10, 3*x[1]**2], [-4, -1, -25]])
^^^^ ^^^^ ^^^^
I added a couple of prints to your code, and got this run:
In [63]: sdm_3eqs()
<ipython-input-62-2b082fcea817>:14: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
m = np.array([[15, 2*x[0], 0], [2*x[1], 10, 3*x[1]**2], [-4, -1, -25]])
f_bold
[array([ 6.64610017e-05, -8.57391288e+00, -1.40877199e+01]), array([-3.81306816, -3.03705309, -6.81097338]), array([ 15.47764768, 12.41073581, -18.41883603])]
grad_f
array([[0.0019938300511945783,
array([-36.8081748 , -17.89173085, -17.14782575]), -0.0],
[array([ -8.20903822, -10.93449844, -7.0767119 ]),
-60.74106175194817,
array([-11.83793275, -21.00337307, -8.79740011])],
[-123.82118142652486, -24.821471626766595, 920.9418016830625]],
dtype=object)
Traceback (most recent call last):
File "<ipython-input-63-8732d4079184>", line 1, in <module>
sdm_3eqs()
File "<ipython-input-62-2b082fcea817>", line 57, in sdm_3eqs
err = np.linalg.norm(grad_f(x), ord=2)/max(1, np.linalg.norm(f_bold(x), ord=2))
File "<__array_function__ internals>", line 5, in norm
File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 2579, in norm
ret = _multi_svd_norm(x, row_axis, col_axis, amax)
File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 2355, in _multi_svd_norm
result = op(svd(y, compute_uv=False), axis=-1)
File "<__array_function__ internals>", line 5, in svd
File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 1672, in svd
s = gufunc(a, signature=signature, extobj=extobj)
UFuncTypeError: Cannot cast ufunc 'svd_n' input from dtype('O') to dtype('float64') with casting rule 'same_kind'
In new enough numpy versions, the creation of a "ragged array" gets this warning, and tells us that we are not creating a simple numeric array. Instead M is an object dtype array, containing a mix of numbers and arrays.
f_bold is a list of arrays. grad_f is a (3,3) object dtype array with a mix of floats and arrays.
And in the latest numpy version this kind of error specifies the problem dtype - which as I suspected is 'O', object.
norm cannot handle that kind of array. That's why I asked for the dtype of the 2 norm arguments.
I have solved it.
In some of the functions, it was supposed to be the dot product of two matrices.
e.g.:
def grad_f(x):
return 2*np.dot(M(x), f_bold(x))
Here's the updated code:
import numpy as np
from numpy.core.fromnumeric import shape
from numpy.linalg import norm
from numpy import transpose
from numpy import array
from numpy import sum
from matplotlib import pyplot as plt
def sdm_3eqs():
def f_bold(x):
x_1 = x[0]
x_2 = x[1]
x_3 = x[2]
return array([15*x_1 + x_2**2 - 4*x_3 - 15, x_1**2 + 10*x_2 - x_3 - 10, x_2**3 - 25*x_3 + 24], dtype='float32')
def f(x):
f_n = []
for i in range(len(x)):
f_i = f_bold[i]**2
f_n.append(f_i)
return sum(f_n)
def M(x):
x_1 = x[0]
x_2 = x[1]
d1 = 2*x_1
d2 = 2*x_2
d3 = 3*x_2**2
return array([[15, d1, 0], [d2, 10, d3], [-4, -1, -25]], dtype='float32')
def grad_f(x):
return 2*np.dot(M(x), f_bold(x))
def d(x):
return -1*grad_f(x)/norm(grad_f(x), ord=2)
def s_prime(x, alpha, d):
return np.dot(transpose(grad_f(x + alpha*d)), d)
x = array([[0.5], [0.5], [0.5]], dtype='float32')
iter = 0
err = 100
while err > 0.005:
x_k = x
d_k = d(x_k)
m = 0
sprime = 300
alpha_l = 0
alpha_u = 1.5
alpha = (alpha_l+alpha_u)/2
while abs(sprime) > 0.0005:
alpha = (alpha_l+alpha_u)/2
sprime = s_prime(x_k, alpha, d_k)
if abs(sprime) < 0.001:
break
elif sprime > 0:
alpha_u = alpha
else:
alpha_l = alpha
m += 1
iter += 1
x = x_k + alpha*d_k
err = norm(grad_f(x), ord=2)/max(1, norm(f_bold(x), ord=2))
print(f'\nf_bold:\nEquation 1 = {f_bold(x)[0][0]}\nEquation 2 = {f_bold(x)[1][0]}\nEquation 3 = {f_bold(x)[2][0]}\n')
sdm_3eqs()
This is my standalone code to reproduce the problem:
import numpy as np
from scipy.optimize import curve_fit
def find_vector_of_minor_axis_from_chunk(data):
n = 20 # number of points
time = np.linspace(0, 2 * np.pi, n)
guess_center_point = data.mean(1)
guess_center_point = guess_center_point[np.newaxis, :].transpose()
guess_a_phase = 0
guess_b_phase = 0
guess_a = 1
guess_b = 1
guess_a_axis_vector = np.array([[1], [0], [0]])
guess_b_axis_vector = np.array([[0], [1], [0]])
p0 = np.array([guess_center_point,
guess_a, guess_a_axis_vector, guess_a_phase,
guess_b, guess_b_axis_vector, guess_b_phase])
def ellipse_func(t, center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase):
return center_point + a * a_axis_vector * np.sin(t * a_phase) + b * b_axis_vector * np.sin(t + b_phase)
popt, pcov = curve_fit(ellipse_func, time, data, p0=p0)
center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase = popt
print(str(a_axis_vector, b_axis_vector))
shorter_vector = a_axis_vector
if np.abs(a_axis_vector) > np.aps(b_axis_vector):
shorter_vector = b_axis_vector
return shorter_vector
def main():
data = np.array([[-4.62767933, -4.6275775, -4.62735346, -4.62719652, -4.62711625, -4.62717975,
-4.62723845, -4.62722407, -4.62713901, -4.62708749, -4.62703238, -4.62689101,
-4.62687185, -4.62694013, -4.62701082, -4.62700483, -4.62697488, -4.62686825,
-4.62675683, -4.62675204],
[-1.58625998, -1.58625039, -1.58619648, -1.58617611, -1.58620606, -1.5861833,
-1.5861821, -1.58619169, -1.58615814, -1.58616893, -1.58613179, -1.58615934,
-1.58611262, -1.58610782, -1.58613179, -1.58614017, -1.58613059, -1.58612699,
-1.58607428, -1.58610183],
[-0.96714786, -0.96713827, -0.96715984, -0.96715145, -0.96716703, -0.96712869,
-0.96716104, -0.96713228, -0.96719698, -0.9671838, -0.96717062, -0.96717062,
-0.96715744, -0.96707717, -0.96709275, -0.96706519, -0.96715026, -0.96711791,
-0.96713588, -0.96714786]])
print(str(find_vector_of_minor_axis_from_chunk(data)))
if __name__ == '__main__':
main()
That gives me this traceback:
Traceback (most recent call last):
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 52, in <module>
main()
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 49, in main
print(str(find_vector_of_minor_axis_from_chunk(data)))
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 25, in find_vector_of_minor_axis_from_chunk
popt, pcov = curve_fit(ellipse_func, time, data, p0=p0)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\minpack.py", line 763, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\minpack.py", line 392, in leastsq
raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
TypeError: Improper input: N=7 must not exceed M=3
Process finished with exit code 1
My code is an adaption of the second answer here. The problem causing the error message is solved by simple packing of variables here.
Why does the problem not surface in the mentioned second answer? And how can I pack my variables, which consist of several 3d vectors and individual scalars, to solve this problem? How do i pass in my t, which is a constant and should not be optimized?
Apparently python is quite smart regarding the length of the fields of the arguments, depending on the the initial guesses. So i could just pass in ONE variable, and split it up inside the function like so:
import numpy as np
from scipy.optimize import minimize
def find_vector_of_minor_axis_from_chunk(data):
n = 20 # number of points
guess_center_point = data.mean(1)
guess_center_point = guess_center_point[np.newaxis, :].transpose()
guess_a_phase = 0.0
guess_b_phase = 0.0
guess_a = 1.0
guess_b = 1.0
guess_a_axis_vector = np.array([[1.0], [0.0], [0.0]])
guess_b_axis_vector = np.array([[0.0], [1.0], [0.0]])
p0 = np.array([guess_center_point,
guess_a, guess_a_axis_vector, guess_a_phase,
guess_b, guess_b_axis_vector, guess_b_phase])
def ellipse_func(x, data):
center_point = x[0]
a = x[1]
a_axis_vector = x[2]
a_phase = x[3]
b = x[4]
b_axis_vector = x[5]
b_phase = x[6]
t = np.linspace(0, 2 * np.pi, n)
error = center_point + a * a_axis_vector * np.sin(t * a_phase) + b * b_axis_vector * np.sin(t + b_phase) - data
error_sum = np.sum(error**2)
print(str(error_sum))
return error_sum
popt, pcov = minimize(ellipse_func, p0, args=(data))
center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase = popt
print(str(a_axis_vector, b_axis_vector))
shorter_vector = a_axis_vector
if np.abs(a_axis_vector) > np.aps(b_axis_vector):
shorter_vector = b_axis_vector
return shorter_vector
def main():
data = np.array([[-4.62767933, -4.6275775, -4.62735346, -4.62719652, -4.62711625, -4.62717975,
-4.62723845, -4.62722407, -4.62713901, -4.62708749, -4.62703238, -4.62689101,
-4.62687185, -4.62694013, -4.62701082, -4.62700483, -4.62697488, -4.62686825,
-4.62675683, -4.62675204],
[-1.58625998, -1.58625039, -1.58619648, -1.58617611, -1.58620606, -1.5861833,
-1.5861821, -1.58619169, -1.58615814, -1.58616893, -1.58613179, -1.58615934,
-1.58611262, -1.58610782, -1.58613179, -1.58614017, -1.58613059, -1.58612699,
-1.58607428, -1.58610183],
[-0.96714786, -0.96713827, -0.96715984, -0.96715145, -0.96716703, -0.96712869,
-0.96716104, -0.96713228, -0.96719698, -0.9671838, -0.96717062, -0.96717062,
-0.96715744, -0.96707717, -0.96709275, -0.96706519, -0.96715026, -0.96711791,
-0.96713588, -0.96714786]])
print(str(find_vector_of_minor_axis_from_chunk(data)))
if __name__ == '__main__':
main()
Also i fixed some floating point vs integer errors in the vector for the initial values.
However now I get a different error:
Traceback (most recent call last):
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 61, in <module>
main()
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 58, in main
print(str(find_vector_of_minor_axis_from_chunk(data)))
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 34, in find_vector_of_minor_axis_from_chunk
popt, pcov = minimize(ellipse_func, p0, args=(data))
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\_minimize.py", line 604, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\optimize.py", line 1063, in _minimize_bfgs
if isinf(rhok): # this is patch for numpy
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I guess that
The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
is some internal error, stemming from the internal decision matrix how to proceed. I don't know how I caused it and how to fix it. When i figure out how it is done properly, I will come back and edit this answer.
The goal with the following code is to plug the function "ddW" into odeint to find W for a given X (later on). I'm using the print function to make sure functions run).
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import pint
u = pint.UnitRegistry()
R = 8.31446261815324 * u.J/(u.mol*u.K)
Rgas = R.to (u.atm*u.L/(u.mol*u.K))
P0 = 10 * u.atm
T = 400 * u.K
v0 = 2 * u.L/u.min
α = 0.02 * u.kg**-1
k = 1.4 * u.L**2/(u.mol*u.kg*u.min)
FA0 = 0.5*P0*v0/(Rgas*T) #Assuming I.G.
def ddW(param,w): #param: [X,P]
X = param[0]
P = u.Quantity(param[1]).magnitude * u.atm
W = u.Quantity(w).magnitude * u.kg
d = np.zeros(2)
d[0] = k*FA0/(v0**2*P0) * P*W*(1-X) #dX/dW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
return d
param0 = [0,P0]
Wrange = np.linspace(0,100) *u.kg
#PBR = odeint(ddW,param0,Wrange)
#plt.plot(Wrange,PBR)
print(ddW(param0,0))
Traceback (most recent call last):
File "<ipython-input-1-83022bc3b5da>", line 1, in <module>
runfile(REDACTED, wdir=REDACTED)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Spencer\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "REDACTED", line 43, in <module>
print(ddW(param0,0))
File "REDACTED", line 33, in ddW
d[1] = -α/2 *P0**2 * (1-X)/P #dP/dW
ValueError: setting an array element with a sequence.
I previously had the same error message for what is now line 32 (defining d[0]) until I defined W in line 29. What is bothering me is that none of the inputs for d[1] (as far as I can tell) have size >1, so it should fit.
Use the following to create an array. The array which you are creating is of type float and the values which you are getting in d[0] and d[1] are not float.
d = np.array(np.zeros(2), dtype=np.object)
Assuming your fix for line 32 (defining d[0]) was to replace w with W where you took the magnitude of w, you need to do the same for the quantity you are trying to put into d[1].
The right side of = for line 32 is a dimensionless Quantity object, whereas the right side of = for line 33 is a Quantity object with dimension standard_atmosphere / kilogram which can't be put into array d as is because it needs to be magnitude only so it can be converted to type float.
Try this if you want to keep your array d as type float:
val = -a/2 *P0**2 * (1-X)/P #dP/dW
d[1] = val.magnitude
I'm trying to read my text file and extract 3 main parameters and put them in separate list and apply normalizing on lists of parameters which are (Temperature, Speed, Acceleration) after assigning Gaussian distribution function. For getting good result I split up positive and negative numbers of each parameters' list and apply gaussian distribution function and pick mean value of negative numbers as the real Minimum and pick mean value of positive numbers as the real Maximum instead of directly find Min and Max values in main list of these parameters which could repeat few times due to they're not in desired confidence interval. The problem is I faced RunTimeWarning error which I avoided already but still I have below error(s) which I don't have any clue how I can solve them includes ValueError: scale <0 , hope that someone has good idea about solution for errors ot better way to apply normalization by using gaussian distribution function Thanks for your attention:
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 258, in handle_args
debug_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 45, in debug_main
run_file(address, name, *extra, **kwargs)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 79, in run_file
run(argv, addr, **kwargs)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 140, in _run
_pydevd.main()
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1925, in main
debugger.connect(host, port)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\majm\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "p:\Desktop\correctt\news.py", line 142, in <module>
plotgaussianfunction(t_p_mean, t_sigma_Positive)
File "p:\Desktop\correctt\news.py", line 58, in plotgaussianfunction
s = np.random.normal(mu, sigma,1000)
File "mtrand.pyx", line 1656, in mtrand.RandomState.normal
ValueError: scale < 0
So my code is:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy
import warnings
warnings.filterwarnings("ignore",category =RuntimeWarning)
df = pd.read_csv('D:/me.txt', header=None)
id_set = df[df.index % 4 == 0].astype('int').values
speed = df[df.index % 4 == 1].values
acceleration = df[df.index % 4 == 2].values
temperature = df[df.index % 4 == 3].values
m_data={'p_Speed': s_p_results[:,0],'n_Speed': s_n_results[:,0], 'p_Acceleration': a_p_results[:,0],'n_Acceleration': a_n_results[:,0], 'p_Temperature': t_p_results[:,0],'n_Temperature': t_n_results[:,0]}
m_main_data = pd.DataFrame(data, columns=['Speed','Acceleration','Temperature'], index = id_set[:,0])
data = {'Speed': speed[:,0], 'Acceleration': acceleration[:,0], 'Temperature': temperature[:,0]}
main_data = pd.DataFrame(data, columns=['Speed','Acceleration','Temperature'], index = id_set[:,0])
main_data = main_data.replace([np.inf, -np.inf], np.nan)
def normalize(value, min_value, max_value, min_norm, max_norm):
new_value = ((max_norm - min_norm)*((value - min_value)/(max_value - min_value))) + min_norm
return new_value
def createpositiveandnegativelist(listtocreate):
l_negative = []
l_positive = []
for value in listtocreate:
if (value < 0):
l_negative.append(value)
elif (value > 0):
l_positive.append(value)
#print(t_negative)
#print(t_positive)
return l_negative,l_positive
def calculatemean(listtocalculate):
return sum(listtocalculate)/len(listtocalculate)
def plotgaussianfunction(mu,sigma):
s = np.random.normal(mu, sigma,1000)
abs(mu - np.mean(s))<0.01
abs(sigma - np.std(s,ddof=1))<0.01
#count, bins, ignored = plt.hist(s,30,density=True)
#plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(-(bins-mu)**2/(2*sigma**2)),linewidth=2, color= 'r')
#plt.show()
return
def plotboundedCI(s, mu, sigma, lists):
plt.figure()
count, bins, ignored = plt.hist(s,30,density=True)
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(-(bins-mu)**2/(2*sigma**2)),linewidth=2, color= 'r')
#confidential interval calculation
ci = scipy.stats.norm.interval(0.68, loc = mu, scale = sigma)
#confidence interval for left line
one_x12, one_y12 = [ci[0],ci[0]], [0,3]
#confidence interval for right line
two_x12, two_y12 = [ci[1],ci[1]], [0,3]
plt.title("Gaussian 68% Confidence Interval", fontsize=12, color='black', loc='left', style='italic')
plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')
plt.show()
results = []
for value in lists:
if(ci[0]< value <ci[1]):
results.append(value)
else:
#print("NOT WANTED: ",value)
pass
return results
t_negative, t_positive = createpositiveandnegativelist(temperature)
a_negative, a_positive = createpositiveandnegativelist(acceleration)
s_negative, s_positive = createpositiveandnegativelist(speed)
#calculating the mean value
t_p_mean = calculatemean(t_positive)
a_p_mean = calculatemean(a_positive)
s_p_mean = calculatemean(s_positive)
t_n_mean = calculatemean(t_negative)
a_n_mean = calculatemean(a_negative)
s_n_mean = calculatemean(s_negative)
#calculating the sigma value
t_sigma_Negative = np.std(t_negative)
t_sigma_Positive = np.std(t_positive)
a_sigma_Negative = np.std(t_negative)
a_sigma_Positive = np.std(t_positive)
s_sigma_Negative = np.std(t_negative)
s_sigma_Positive = np.std(t_positive)
#plot the gaussian function with histograms
plotgaussianfunction(t_p_mean, t_sigma_Positive)
plotgaussianfunction(t_n_mean, t_sigma_Negative)
plotgaussianfunction(a_p_mean, a_sigma_Positive)
plotgaussianfunction(a_n_mean, a_sigma_Negative)
plotgaussianfunction(s_p_mean, s_sigma_Positive)
plotgaussianfunction(s_n_mean, s_sigma_Negative)
#normalization
t_p_s = np.random.normal(t_p_mean, t_sigma_Positive,1000)
t_n_s = np.random.normal(t_n_mean, t_sigma_Negative,1000)
a_p_s = np.random.normal(a_p_mean, a_sigma_Positive,1000)
a_n_s = np.random.normal(a_n_mean, a_sigma_Negative,1000)
s_p_s = np.random.normal(s_p_mean, s_sigma_Positive,1000)
s_n_s = np.random.normal(s_n_mean, s_sigma_Negative,1000)
#histograms minus the outliers
t_p_results = plotboundedCI(t_p_s, t_p_mean, t_sigma_Positive, t_positive)
t_n_results = plotboundedCI(t_n_s, t_n_mean, t_sigma_Negative, t_negative)
a_p_results = plotboundedCI(a_p_s, a_p_mean, a_sigma_Positive, a_positive)
a_n_results = plotboundedCI(a_n_s, a_n_mean, a_sigma_Negative, a_negative)
s_p_results = plotboundedCI(s_p_s, s_p_mean, s_sigma_Positive, s_positive)
s_n_results = plotboundedCI(s_n_s, s_n_mean, s_sigma_Negative, s_negative)
Note: I have some missing data(nan or inf) in my list of values which are already replaced by zero! but considering that when I have no missing values in my list of parameters , the code works!
from documentation of numpy.random.normal:
Parameters:
loc : float or array_like of floats
Mean (“centre”) of the distribution.
scale : float or array_like of floats
Standard deviation (spread or “width”) of the distribution.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if loc and scale are both scalars. Otherwise, np.broadcast(loc, scale).size samples are drawn.
the scale is the Standard deviation of the distribution hence it can not be negative. Hence the error you get: ValueError: scale < 0
you may want to check the sign of this parameter. give it a try with:
s = np.random.normal(mu, np.abs(sigma),1000)
I want to model a multivariate (bivariate in the example) normal with separate means and a covariance matrix, somehow I just can't get the dimensions to match. The model is relatively straight forward, here is the data generating process:
import numpy as np
import pymc3 as pm
import theano.tensor as tt
np.random.seed(123)
N = 200
# x1 and x2 are independent variables
x1 = np.random.randn(N)
x2 = np.random.randn(N)
α1, β1, α2, β2 = np.random.randn(4)
# the means of the joint distribution
μ1 = α1 + β1 * x1
μ2 = α2 + β2 * x2
# the covariance of the joint distribution
σ = 1, 2
Ω = np.array([[1, 0.5], [0.5, 1]])
Σ = np.diag(σ) # Ω # np.diag(σ)
# the error term
ϵ = np.random.multivariate_normal(mean=(0, 0), cov=Σ, size=N)
# the dependent variable as bivariate normal
y = np.stack((μ1, μ2)).T + ϵ
Here is my model:
with pm.Model() as joint_model:
a1 = pm.Normal('a1', 0, 1)
b1 = pm.Normal('b1', 0, 1)
a2 = pm.Normal('a2', 0, 1)
b2 = pm.Normal('b2', 0, 1)
mu1 = a1 + b1 * x1
mu2 = a2 + b2 * x2
sigma = pm.HalfCauchy('sigma', 1, shape=2)
# build the covariance matrix
C_triu = pm.LKJCorr('omega', n=2, p=2)
C = tt.fill_diagonal(C_triu[np.zeros((2, 2), dtype=np.int64)], 1)
sigma_diag = tt.nlinalg.diag(sigma)
cov = tt.nlinalg.matrix_dot(sigma_diag, C, sigma_diag)
joint_obs = pm.MvNormal('joint', mu=(mu1, mu2), cov=cov, observed=y)
The error message is ValueError: Input dimension mis-match. (input[0].shape[0] = 200, input[1].shape[0] = 2).
It's clear I've made a mistake specifying the Multivariate normal distribution, there is a (maybe more than one) dimension mismatch between mu, cov, and the observed data, I just can't figure out where and how to fix it. Here is the (very long) error message, and the code above should be able to replicate the error:
Traceback (most recent call last):
File "<ipython-input-4-b3658e712eaf>", line 18, in <module>
joint_obs = pm.MvNormal('joint', mu=(mu1, mu2), cov=cov, observed=y)
File "/Users/oma/anaconda/lib/python3.6/site-packages/pymc3/distributions/distribution.py", line 39, in __new__
return model.Var(name, dist, data, total_size)
File "/Users/oma/anaconda/lib/python3.6/site-packages/pymc3/model.py", line 545, in Var
total_size=total_size, model=self)
File "/Users/oma/anaconda/lib/python3.6/site-packages/pymc3/model.py", line 970, in __init__
self.logp_elemwiset = distribution.logp(data)
File "/Users/oma/anaconda/lib/python3.6/site-packages/pymc3/distributions/multivariate.py", line 200, in logp
delta = value - mu
File "/Users/oma/anaconda/lib/python3.6/site-packages/theano/tensor/var.py", line 147, in __sub__
return theano.tensor.basic.sub(self, other)
File "/Users/oma/anaconda/lib/python3.6/site-packages/theano/gof/op.py", line 674, in __call__
required = thunk()
File "/Users/oma/anaconda/lib/python3.6/site-packages/theano/gof/op.py", line 843, in rval
fill_storage()
File "/Users/oma/anaconda/lib/python3.6/site-packages/theano/gof/cc.py", line 1698, in __call__
reraise(exc_type, exc_value, exc_trace)
File "/Users/oma/anaconda/lib/python3.6/site-packages/six.py", line 686, in reraise
raise value
ValueError: Input dimension mis-match. (input[0].shape[0] = 200, input[1].shape[0] = 2)
The question has been answered on discourse.pymc.io.
The specification for mu in pm.MvNormal should be mu = tt.stack([mu1, mu2]).T.