Simple Dynamical Model in PyMC3 - python

I'm trying to put together a model of a dynamical system in PyMC3, to infer two parameters. The model is the basic SIR, commonly used in epidemiology :
dS/dt = - r0 * g * S * I
dI/dt = g * I ( r * S - 1 )
where r0 and g are parameters to be inferred. So far, I'm unable to get very far at all. The only examples I've seen of putting together a Markov chain like this yields errors about recursion being too deep. Here's my example code.
# Time
t = np.linspace(0, 8, 200)
# Simulated observation
def SIR(y, t, r0, gamma) :
S = - r0 * gamma * y[0] * y[1]
I = r0 * gamma * y[0] * y[1] - gamma * y[1]
return [S, I]
# Currently no noise, we just want to infer params r0 = 16 and g = 0.5
solution = odeint(SIR, [0.99, 0.01, 0], t, args=(16., 0.5))
with pymc.Model() as model :
r0 = pymc.Normal("r0", 15, sd=10)
gamma = pymc.Uniform("gamma", 0.3, 1.)
# Use forward Euler to solve
dt = t[1] - t[0]
# Initial conditions
S = [0.99]
I = [0.01]
for i in range(1, len(t)) :
S.append(pymc.Normal("S%i" % i, \
mu = S[-1] + dt * (-r0 * gamma * S[-1] * I[-1]), \
sd = solution[:, 0].std()))
I.append(pymc.Normal("I%i" % i, \
mu = I[-1] + dt * ( r0 * gamma * S[-1] * I[-1] - gamma * I[-1]), \
sd = solution[:, 1].std()))
Imcmc = pymc.Normal("Imcmc", mu = I, sd = solution[:, 1].std(), observed = solution[:, 1])
#start = pymc.find_MAP()
trace = pymc.sample(2000, pymc.NUTS())
Any help would be much appreciated. Thanks !

I would try defining a new distribution. Something like the following. However, this is not quite working, and I'm not quite sure what I did wrong.
class SIR(Distribution):
def __init__(self, gamma, r0,dt, std):
self.gamma = gamma
self.r0 = r0
self.std = std
self.dt = dt
def logp(self, SI):
r0 = self.r0
std = self.std
gamma = self.gamma
dt = self.dt
S=SI[:,0]
I=SI[:,1]
Si = S[1:]
Si_m1 = S[:-1]
Ii = I[1:]
Ii_m1 = I[:-1]
Sdelta = (Si - Si_m1)
Idelta = (Ii - Ii_m1)
Sexpected_delta = dt* (-r0 * gamma * Si_m1 * Ii_m1)
Iexpected_delta = dt * gamma * Ii_m1 *( r0 * Si_m1 - 1 )
return (Normal.dist(Sexpected_delta, sd=std).logp(Sdelta) +
Normal.dist(Iexpected_delta, sd=std).logp(Idelta))
with Model() as model:
r0 = pymc.Normal("r0", 15, sd=10)
gamma = pymc.Normal("gamma", 0.3, 1.)
std = .5
dt = t[1]-t[0]
SI = SIR('SI', gamma, r0, std,dt, observed=solution[:,:2])
#start = pymc.find_MAP(start={'gamma' : .45, 'r0' : 17})
trace = pymc.sample(2000, pymc.NUTS())

Related

Plotting Monte Carlo Simulations for option pricing in Python

I am trying to show the monte carlo barrier prices for different number of simultations in the x axis. This is what i tried so far but i'm getting the error -> ValueError: x and y must have same first dimension, but have shapes (10,) and (5,).
I am new to python and as hard as i try i cannot find the error
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
def mc_single_barrier_do(S0, K, T, H, r, vol, N, M):
# Constants
dt = T / N # change in time
nudt = (r - 0.5 * vol ** 2) * dt # deterministic component
volsdt = vol * np.sqrt(dt) # diffusion coefficient
erdt = np.exp(r * dt) # discount factor
# Standard Error Placeholders
sum_CT = 0
sum_CT2 = 0
# Monte Carlo Method
for i in range(M):
# Barrier Crossed Flag
BARRIER = False
St = S0
for j in range(N):
epsilon = np.random.normal()
Stn = St * np.exp(nudt + volsdt * epsilon)
St = Stn
Ptn = np.exp(-2. * (H - St) * (H - Stn) / (St ** 2. * volsdt ** 2.))
Pt = Ptn
if Pt >= npr.uniform():
BARRIER = True
if np.amin(St) > H and BARRIER == False:
CT = np.maximum(St - K, 0)
else:
CT = 0.
sum_CT = sum_CT + CT
sum_CT2 = sum_CT2 + CT * CT
C0_MC = np.exp(-r * T) * sum_CT / M
return C0_MC
def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):
assert (method in ['MC', 'AV', 'CV'])
mean_payoffs = np.zeros(int(np.ceil(max_sample / 10)))
if method == 'MC':
for n_sample in range(10, max_sample + 1, 10):
payoffs = mc_single_barrier_do(n_sample, S0, K, T, H, r, vol, N)
mean_payoffs[int(n_sample / 10 - 1)] = np.mean(payoffs)
return mean_payoffs
r = 0.1
vol = 0.2
T = 2
N = 20
dt = T / N
S0 = 50
K = 50
H = 45
max_sample = 100
MC_price_estimates = sim_iterator(S0, T, r, vol, K, H, max_sample, N, method='MC')
x_axis1 = range(10, max_sample + 1, 10)
plt.plot(x_axis1, MC_price_estimates)
plt.xlabel("No. of Simulations")
plt.ylabel("Estimated option price")
plt.title("Ordinary Monte Carlo Method")
plt.legend()
plt.show()
in your function definition you used:
def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):
while when using the function you used:
MC_price_estimates = sim_iterator(S0, T, r, vol, K, H, max_sample, N, method='MC')
python has positional arguments, which means the arguments are mapped according to their position, not their name, so in the first position is mapped to the first argument, which means S0 in the second line was mapped to max_sample in the first line, just fix the arguments arrangement, or use keyword arguments S0=S0.
MC_price_estimates = sim_iterator(S0=S0, T=T, r=r, vol=vol, K=K, H=H, max_sample=max_sample, N=N, method='MC')
this is what your code will look like when you fix all arguments to be keyword arguments.
def mc_single_barrier_do(S0, K, T, H, r, vol, N, M):
# Constants
dt = T / N # change in time
nudt = (r - 0.5 * vol ** 2) * dt # deterministic component
volsdt = vol * np.sqrt(dt) # diffusion coefficient
erdt = np.exp(r * dt) # discount factor
# Standard Error Placeholders
sum_CT = 0
sum_CT2 = 0
# Monte Carlo Method
for i in range(M):
# Barrier Crossed Flag
BARRIER = False
St = S0
for j in range(N):
epsilon = np.random.normal()
Stn = St * np.exp(nudt + volsdt * epsilon)
St = Stn
Ptn = np.exp(-2. * (H - St) * (H - Stn) / (St ** 2. * volsdt ** 2.))
Pt = Ptn
if Pt >= npr.uniform():
BARRIER = True
if np.amin(St) > H and BARRIER == False:
CT = np.maximum(St - K, 0)
else:
CT = 0.
sum_CT = sum_CT + CT
sum_CT2 = sum_CT2 + CT * CT
C0_MC = np.exp(-r * T) * sum_CT / M
return C0_MC
def sim_iterator(max_sample, N, S0, T, r, vol, K, H, method):
assert (method in ['MC', 'AV', 'CV'])
mean_payoffs = np.zeros(int(np.ceil(max_sample / 10)))
if method == 'MC':
for n_sample in range(10, max_sample + 1, 10):
payoffs = mc_single_barrier_do(M=n_sample,S0= S0, K=K, T=T, H=H, r=r, vol=vol, N=N)
mean_payoffs[int(n_sample / 10 - 1)] = np.mean(payoffs)
return mean_payoffs
r = 0.1
vol = 0.2
T = 2
N = 20
dt = T / N
S0 = 50
K = 50
H = 45
max_sample = 100
MC_price_estimates = sim_iterator(S0=S0, T=T, r=r, vol=vol, K=K, H=H, max_sample=max_sample, N=N, method='MC')
x_axis1 = range(10, max_sample + 1, 10)
plt.plot(x_axis1, MC_price_estimates)
plt.xlabel("No. of Simulations")
plt.ylabel("Estimated option price")
plt.title("Ordinary Monte Carlo Method")
plt.legend()
plt.show()

Not geting right solution with solve_ivp

I am trying to solve the differential equation using solve_ivp, but I am not getting the right solution. However, I obtained the right solution using ideint. Do I have some problems with the solve_ipv program?
ODEINT program :
# Arhenius Function
def Arhenius(a, T):
dadT = np.exp(lnA)/v * np.exp(- E / (8.3144 * T)) * c * np.abs(a) ** m * np.abs((1 - np.abs(a))) ** n
return dadT
# Initial data
pt = 100000
T0 = 273
Tf = 1500
a0 = 0.0000000001
T = np.linspace(T0, Tf, pt)
a_sol = np.zeros(pt)
dadt = np.zeros(pt)
# ODE solve
a_t = odeint(Arhenius, a0, T)
# For removing errored values and have maximum at 1
search1 = np.where(np.isclose(a_t, 1))
try:
ia_1 = search1[0][0]
a_sol[0,:] = a_t[:,0]
a_sol[0,ia_1+1:pt] = 1
except:
a_sol = a_t[:,0]
# Calculate the new derivative
dadt = np.exp(lnA) * np.exp(- E / (8.3144 * T)) * c * np.abs(a_sol) ** m * np.abs((1 - a_sol)) ** n
PROGRAM with solve_ivp :
# Arhenius Function
def Arhenius(a, T):
dadT = np.exp(lnA) * np.exp(- E / (8.3144 * T)) * c * np.abs(a) ** m * np.abs((1 - np.abs(a))) ** n
return dadT
# Initial data
pt = 100000
T0 = 273
Tf = 1500
a0 = 0.0000000001
T = np.linspace(T0, Tf, pt)
a_sol = np.zeros(pt)
dadt = np.zeros(pt)
# ODE solve
a_t = solve_ivp(Arhenius, t_span = (T0, Tf), y0 = (a0,), t_eval = T, method = 'RK45')
a_sol= a_t.y
# Calculate the new derivative
dadt = np.exp(lnA) * np.exp(- E / (8.3144 * T)) * c * np.abs(a_sol) ** m * np.abs((1 - a_sol)) ** n

Octave's fzero() and Scipy's root() functions not producing the same result

I have to find the zero of the following equation:
This is an equation of state, and it doesn't matter a whole lot if you don't know exactly what an EoS is. With the root of the above equation I compute (among other things) the compressibility factors of a gaseous substance, Z, for different pressures and temperatures. With those solutions I can plot families of curves having pressures as abscissas, Zs as ordinates and temperatures as parameters. Beta, delta, eta and phi are constants, as well as pr and Tr.
After banging my head unsuccessfully against the Newton-Raphson method (which works fine with several other EoSs) I decided to try Scipy's root() function. To my discontent, I obtained this chart:
As one can easily perceive, this saw-toothed chart is totally flawed. I should've gotten smooth curves instead. Also, Z typically ranges between 0.25 and 2.0. Thus, Zs equal to, say, 3 or above are completely off the mark. Yet the curves with Z < 2 look OK, although highly compressed because of the scale.
Then I tried Octave's fzero() solver, and got this:
Which is exactly what I should've gotten, as those are curves with the correct/expected shape!
Here comes my question. Apparently Scipy's root() and Octave's fzero() are based on the same algorithm hybrid from MINPACK. Still, the results clearly aren't the same. Do any of you know why?
I plotted a curve of the Zs obtained by Octave (abscissas) against the ones obtained with Scipy and got this:
The points at the bottom hinting a straight line represent y = x, i.e., the points for which Octave and Scipy agreed in the solutions they presented. The other points are in total disagreement and, unfortunately, they're too many to be simply ignored.
I might always use Octave from now on since it works, but I want to keep using Python.
What's your take on this? Any suggestion?
PS: Here's the original Python code. It produces the first chart shown here.
import numpy
from scipy.optimize import root
import matplotlib.pyplot as plt
def fx(x, beta, delta, eta, phi, pr_, Tr_):
tmp = phi*x**2
etmp = numpy.exp(-tmp)
f = x*(1.0 + beta*x + delta*x**4 + eta*x**2*(1.0 + tmp)*etmp) - pr_/Tr_
return f
def zsbwr(pr_, Tr_, pc_, Tc_, zc_, w_, MW_, phase=0):
d1 = 0.4912 + 0.6478*w_
d2 = 0.3000 + 0.3619*w_
e1 = 0.0841 + 0.1318*w_ + 0.0018*w_**2
e2 = 0.075 + 0.2408*w_ - 0.014*w_**2
e3 = -0.0065 + 0.1798*w_ - 0.0078*w_**2
f = 0.770
ee = (2.0 - 5.0*zc_)*numpy.exp(f)/(1.0 + f + 3.0*f**2 - 2*f**3)
d = (1.0 - 2.0*zc_ - ee*(1.0 + f - 2.0*f**2)*numpy.exp(-f))/3.0
b = zc_ - 1.0 - d - ee*(1.0 + f)*numpy.exp(-f)
bc = b*zc_
dc = d*zc_**4
ec = ee*zc_**2
phi = f*zc_**2
beta = bc + 0.422*(1.0 - 1.0/Tr_**1.6) + 0.234*w_*(1.0- 1.0/Tr_**3)
delta = dc*(1.0+ d1*(1.0/Tr_ - 1.0) + d2*(1.0/Tr_ - 1.0)**2)
eta = ec + e1*(1.0/Tr_ - 1.0) + e2*(1.0/Tr_ - 1.0)**2 \
+ e3*(1.0/Tr_ - 1.0)**3
if Tr_ > 1:
y0 = pr_/Tr_/(1.0 + beta*pr_/Tr_)
else:
if phase == 0:
y0 = pr_/Tr_/(1.0 + beta*pr_/Tr_)
else:
y0 = 1.0/zc_**(1.0 + (1.0 - Tr_)**(2.0/7.0))
raiz = root(fx,y0,args=(beta, delta, eta, phi, pr_, Tr_),method='hybr',tol=1.0e-06)
return pr_/raiz.x[0]/Tr_
if __name__ == "__main__":
Tc = 304.13
pc = 73.773
omega = 0.22394
zc = 0.2746
MW = 44.01
Tr = numpy.array([0.8, 0.93793103])
pr = numpy.linspace(0.5, 14.5, 25)
zfactor = numpy.zeros((2, 25))
for redT in Tr:
j = numpy.where(Tr == redT)[0][0]
for redp in pr:
indp = numpy.where(pr == redp)[0][0]
zfactor[j][indp] = zsbwr(redp, redT, pc, Tc, zc, omega, MW, 0)
for key, value in enumerate(zfactor):
plt.plot(pr, value, '.-', linewidth=1, color='#ef082a')
plt.figure(1, figsize=(7, 6))
plt.xlabel('$p_R$', fontsize=16)
plt.ylabel('$Z$', fontsize=16)
plt.grid(color='#aaaaaa', linestyle='--', linewidth=1)
plt.show()
And now the Octave script:
function SoaveBenedictWebbRubin
format long;
nTr = 11;
npr = 43;
ic = 1;
nome = {"CO2"; "N2"; "H2O"; "CH4"; "C2H6"; "C3H8"};
comp = [304.13, 73.773, 0.22394, 0.2746, 44.0100; ...
126.19, 33.958, 0.03700, 0.2894, 28.0134; ...
647.14, 220.640, 0.34430, 0.2294, 18.0153; ...
190.56, 45.992, 0.01100, 0.2863, 16.0430; ...
305.33, 48.718, 0.09930, 0.2776, 30.0700; ...
369.83, 42.477, 0.15240, 0.2769, 44.0970];
Tc = comp(ic,1);
pc = comp(ic,2);
w = comp(ic,3);
zc = comp(ic,4);
MW = comp(ic,5);
Tr = linspace(0.8, 2.8, nTr);
pr = linspace(0.2, 7.2, npr);
figure(1, 'position',[300,150,600,500])
for i=1:size(Tr, 2)
icont = 1;
zval = zeros(1, npr);
for j=1:size(pr, 2)
[Z, phi, density] = SBWR(Tr(i), pr(j), Tc, pc, zc, w, MW, 0);
zval(icont) = Z;
icont = icont + 1;
endfor
plot(pr,zval,'o','markerfacecolor','white','linestyle','-','markersize',3);
hold on;
endfor
str = strcat("Soave-Benedict-Webb-Rubin para","\t",nome(ic));
xlabel("p_r",'fontsize',15);
ylabel("Z",'fontsize',15);
title(str,'fontsize',12);
end
function [Z,phi,density] = SBWR(Tr, pr, Tc, pc, Zc, w, MW, phase)
R = 8.3144E-5; % universal gas constant (bar·m3/(mol·K))
% Definition of parameters
d1 = 0.4912 + 0.6478*w;
d2 = 0.3 + 0.3619*w;
e1 = 0.0841 + 0.1318*w + 0.0018*w**2;
e2 = 0.075 + 0.2408*w - 0.014*w**2;
e3 = -0.0065 + 0.1798*w - 0.0078*w**2;
f = 0.77;
ee = (2.0 - 5.0*Zc)*exp(f)/(1.0 + f + 3.0*f**2 - 2.0*f**3);
d = (1.0 - 2.0*Zc - ee*(1.0 + f - 2.0*f**2)*exp(-f))/3.0;
b = Zc - 1.0 - d - ee*(1.0 + f)*exp(-f);
bc = b*Zc;
dc = d*Zc**4;
ec = ee*Zc**2;
ff = f*Zc**2;
beta = bc + 0.422*(1.0 - 1.0/Tr**1.6) + 0.234*w*(1.0 - 1.0/Tr**3);
delta = dc*(1.0 + d1*(1.0/Tr - 1.0) + d2*(1.0/Tr - 1.0)**2);
eta = ec + e1*(1.0/Tr - 1.0) + e2*(1.0/Tr - 1.0)**2 + e3*(1.0/Tr - 1.0)**3;
if Tr > 1
y0 = pr/Tr/(1.0 + beta*pr/Tr);
else
if phase == 0
y0 = pr/Tr/(1.0 + beta*pr/Tr);
else
y0 = 1.0/Zc**(1.0 + (1.0 - Tr)**(2.0/7.0));
end
end
fun = #(y)y*(1.0 + beta*y + delta*y**4 + eta*y**2*(1.0 + ff*y**2)*exp(-ff*y**2)) - pr/Tr;
options = optimset('TolX',1.0e-06);
yi = fzero(fun,y0,options);
Z = pr/yi/Tr;
density = yi*pc*MW/(1000.0*R*Tc);
phi = exp(Z - 1.0 - log(Z) + beta*yi + 0.25*delta*yi**4 - eta/ff*(exp(-ff*yi**2)*(1.0 + 0.5*ff*yi**2) - 1.0));
end
First things first. Your two files weren't equivalent, therefore a direct comparison of the underlying algorithms was difficult. I attach here an octave and a python version that are directly comparable line-for-line that can be compared side-by-side.
%%% File: SoaveBenedictWebbRubin.m:
% No package imports necessary
function SoaveBenedictWebbRubin()
nome = {"CO2"; "N2"; "H2O"; "CH4"; "C2H6"; "C3H8"};
comp = [ 304.13, 73.773, 0.22394, 0.2746, 44.0100;
126.19, 33.958, 0.03700, 0.2894, 28.0134;
647.14, 220.640, 0.34430, 0.2294, 18.0153;
190.56, 45.992, 0.01100, 0.2863, 16.0430;
305.33, 48.718, 0.09930, 0.2776, 30.0700;
369.83, 42.477, 0.15240, 0.2769, 44.0970 ];
nTr = 11; Tr = linspace( 0.8, 2.8, nTr );
npr = 43; pr = linspace( 0.2, 7.2, npr );
ic = 1;
Tc = comp(ic, 1);
pc = comp(ic, 2);
w = comp(ic, 3);
zc = comp(ic, 4);
MW = comp(ic, 5);
figure(1, 'position',[300,150,600,500])
zvalues = zeros( nTr, npr );
for i = 1 : nTr
for j = 1 : npr
zvalues(i,j) = zSBWR( Tr(i), pr(j), Tc, pc, zc, w, MW, 0 );
endfor
endfor
hold on
for i = 1 : nTr
plot( pr, zvalues(i,:), 'o-', 'markerfacecolor', 'white', 'markersize', 3);
endfor
hold off
xlabel( "p_r", 'fontsize', 15 );
ylabel( "Z" , 'fontsize', 15 );
title( ["Soave-Benedict-Webb-Rubin para\t", nome(ic)], 'fontsize', 12 );
endfunction % main
function Z = zSBWR( Tr, pr, Tc, pc, Zc, w, MW, phase )
% Definition of parameters
d1 = 0.4912 + 0.6478 * w;
d2 = 0.3 + 0.3619 * w;
e1 = 0.0841 + 0.1318 * w + 0.0018 * w ** 2;
e2 = 0.075 + 0.2408 * w - 0.014 * w ** 2;
e3 = -0.0065 + 0.1798 * w - 0.0078 * w ** 2;
f = 0.77;
ee = (2.0 - 5.0 * Zc) * exp( f ) / (1.0 + f + 3.0 * f ** 2 - 2.0 * f ** 3 );
d = (1.0 - 2.0 * Zc - ee * (1.0 + f - 2.0 * f ** 2) * exp( -f ) ) / 3.0;
b = Zc - 1.0 - d - ee * (1.0 + f) * exp( -f );
bc = b * Zc;
dc = d * Zc ** 4;
ec = ee * Zc ** 2;
phi = f * Zc ** 2;
beta = bc + 0.422 * (1.0 - 1.0 / Tr ** 1.6) + 0.234 * w * (1.0 - 1.0 / Tr ** 3);
delta = dc * (1.0 + d1 * (1.0 / Tr - 1.0) + d2 * (1.0 / Tr - 1.0) ** 2);
eta = ec + e1 * (1.0 / Tr - 1.0) + e2 * (1.0 / Tr - 1.0) ** 2 + e3 * (1.0 / Tr - 1.0) ** 3;
if Tr > 1
y0 = pr / Tr / (1.0 + beta * pr / Tr);
else
if phase == 0
y0 = pr / Tr / (1.0 + beta * pr / Tr);
else
y0 = 1.0 / Zc ** (1.0 + (1.0 - Tr) ** (2.0 / 7.0) );
endif
endif
yi = fzero( #(y) fx(y, beta, delta, eta, phi, pr, Tr), y0, optimset( 'TolX', 1.0e-06 ) );
Z = pr / yi / Tr;
endfunction % zSBWR
function Out = fx( y, beta, delta, eta, phi, pr, Tr )
Out = y * (1.0 + beta * y + delta * y ** 4 + eta * y ** 2 * (1.0 + phi * y ** 2) * exp( -phi * y ** 2 ) ) - pr / Tr;
endfunction
### File: SoaveBenedictWebbRubin.py
import numpy; from scipy.optimize import root; import matplotlib.pyplot as plt
def SoaveBenedictWebbRubin():
nome = ["CO2", "N2", "H2O", "CH4", "C2H6", "C3H8"]
comp = numpy.array( [ [ 304.13, 73.773, 0.22394, 0.2746, 44.0100 ],
[ 126.19, 33.958, 0.03700, 0.2894, 28.0134 ],
[ 647.14, 220.640, 0.34430, 0.2294, 18.0153 ],
[ 190.56, 45.992, 0.01100, 0.2863, 16.0430 ],
[ 305.33, 48.718, 0.09930, 0.2776, 30.0700 ],
[ 369.83, 42.477, 0.15240, 0.2769, 44.0970 ] ] )
nTr = 11; Tr = numpy.linspace( 0.8, 2.8, nTr )
npr = 43; pr = numpy.linspace( 0.2, 7.2, npr )
ic = 0
Tc = comp[ic, 0]
pc = comp[ic, 1]
w = comp[ic, 2]
zc = comp[ic, 3]
MW = comp[ic, 4]
plt.figure(1, figsize=(7, 6))
zvalues = numpy.zeros( (nTr, npr) )
for i in range( nTr ):
for j in range( npr ):
zvalues[i,j] = zsbwr( Tr[i], pr[j], pc, Tc, zc, w, MW, 0)
# endfor
# endfor
for i in range(nTr):
plt.plot(pr, zvalues[i, :], 'o-', markerfacecolor='white', markersize=3 )
plt.xlabel( '$p_r$', fontsize = 15 )
plt.ylabel( '$Z$' , fontsize = 15 )
plt.title( "Soave-Benedict-Webb-Rubin para\t" + nome[ic], fontsize = 12 );
plt.show()
# end function main
def zsbwr( Tr, pr, pc, Tc, zc, w, MW, phase=0):
# Definition of parameters
d1 = 0.4912 + 0.6478 * w
d2 = 0.3000 + 0.3619 * w
e1 = 0.0841 + 0.1318 * w + 0.0018 * w ** 2
e2 = 0.075 + 0.2408 * w - 0.014 * w ** 2
e3 = -0.0065 + 0.1798 * w - 0.0078 * w ** 2
f = 0.770
ee = (2.0 - 5.0 * zc) * numpy.exp( f ) / (1.0 + f + 3.0 * f ** 2 - 2 * f ** 3)
d = (1.0 - 2.0 * zc - ee * (1.0 + f - 2.0 * f ** 2) * numpy.exp( -f )) / 3.0
b = zc - 1.0 - d - ee * (1.0 + f) * numpy.exp( -f )
bc = b * zc
dc = d * zc ** 4
ec = ee * zc ** 2
phi = f * zc ** 2
beta = bc + 0.422 * (1.0 - 1.0 / Tr ** 1.6) + 0.234 * w * (1.0 - 1.0 / Tr ** 3)
delta = dc * (1.0 + d1 * (1.0 / Tr - 1.0) + d2 * (1.0 / Tr - 1.0) ** 2)
eta = ec + e1 * (1.0 / Tr - 1.0) + e2 * (1.0 / Tr - 1.0) ** 2 + e3 * (1.0 / Tr - 1.0) ** 3
if Tr > 1:
y0 = pr / Tr / (1.0 + beta * pr / Tr)
else:
if phase == 0:
y0 = pr / Tr / (1.0 + beta * pr / Tr)
else:
y0 = 1.0 / zc ** (1.0 + (1.0 - Tr) ** (2.0 / 7.0))
# endif
# endif
yi = root( fx, y0, args = (beta, delta, eta, phi, pr, Tr), method = 'hybr', tol = 1.0e-06 ).x
return pr / yi / Tr
# endfunction zsbwr
def fx(y, beta, delta, eta, phi, pr, Tr):
return y*(1.0 + beta*y + delta*y**4 + eta*y**2*(1.0 + phi*y**2)*numpy.exp(-phi*y**2)) - pr/Tr
# endfunction fx
if __name__ == "__main__": SoaveBenedictWebbRubin()
This confirms that the outputs from the two systems do in fact differ partly due to the outputs of the underlying algorithms used, rather than because the programs weren't the effectively the same. However, the comparison is not as bad now:
As for "the algorithms are the same", they are not. Octave typically hides more technical implementation details in the source code, so this is always worth checking. In particular, in file fzero.m, right after the docstring, it mentions the following:
This is essentially the ACM "Algorithm 748: Enclosing Zeros of Continuous Functions" due to Alefeld, Potra and Shi, ACM Transactions on Mathematical Software, Vol. 21, No. 3, September 1995.
Although the workflow should be the same, the structure of the algorithm has been transformed non-trivially; instead of the authors' approach of sequentially calling building blocks subprograms we implement here a FSM version using one interior point determination and one bracketing per iteration, thus reducing the number of temporary variables and simplifying the algorithm structure. Further, this approach reduces the need for external functions and error handling. The algorithm has also been slightly modified.
Whereas according to help(root):
Notes
This section describes the available solvers that can be selected by the 'method' parameter. The default method is hybr.
Method hybr uses a modification of the Powell hybrid method as
implemented in MINPACK [1].
References
[1] More, Jorge J., Burton S. Garbow, and Kenneth E. Hillstrom. 1980. User Guide for MINPACK-1.
I tried a couple of the alternatives mentioned in help(root). The df-sane one seems to be optimised for 'scalar' values (i.e. like 'fzero'). Indeed, while not as good as octave's implementation, this does give a slightly 'saner' (pun intended) result:
Having said all that, the hybrid method doesn't dump any warnings, but if you use some of the other alternatives, many of them will inform you that you have a lot of effective divisions by zero, nans, and infs, in places were you shouldn't, which is presumably why you get such weird results. So, perhaps it's not that octave's algorithm is "better" per se, but that it handles "division by zero" instances in this problem slightly more gracefully.
I don't know the exact nature of your problem, but it may be that the algorithms on python's side simply expect you to feed it well-conditioned problems instead. Perhaps some of your computations in zsbwr() result in division by zero occasions or unrealistic zeros etc, which you could detect and treat as special cases?
(Please trim the code to a minimum example which only show the root-finding part and parameters where it finds an unwanted root.)
Then the procedure is to manually inspect the equation to find the localization interval for the root you want and use it. I typically use brentq.

Solving ODE with python

I've tried to solve a 1-st order ode equation using scipy.integrate.ode.
dh/dx = - s0 * (1 - (hn/h)^3)/(1 - (hc/h)^3)
The initial condition is x = 0 and h = 10.
s0 = 0.001 when x < 15000,
s0 = 0.0005 when x >= 15000.
hn = (f*q^2/8*g*s0)^(1/3)
hc = (q^2/g)^(1/3)
f, q, g are constant.
The method I used is node bdf, but the result I got is different than the answer solved by matlab.
The answer should be like this:
https://dl.dropboxusercontent.com/u/18438495/result.png
Can anyone see the problem?
import numpy as np
from scipy.integrate import ode
import matplotlib.pyplot as plt
def waterdepth(t, y):
if t < 15000:
s0 = 0.001
elif t >= 15000:
s0 = 0.0005
q = 3.72
f = 0.03
g = 9.81
hn = (f*q*q/8*g*s0)**(1.0/3.0)
hc = (q*q/g)**(1.0/3.0)
return -s0 * (1.0 - (hn/y)**3)/(1.0 - (hc/y)**3)
y0 = 10.0
t0 = 0.0
solver = ode(waterdepth).set_integrator('node', method = 'bdf')
solver.set_initial_value(y0, t0)
dt = 100.0
t1 = 25000
x = []
h = []
while solver.successful() and solver.t < t1:
x.append(solver.t)
solver.integrate(solver.t + dt)
h.append(solver.y)
plt.plot (x, h)
return -s0 * (1.0 - (hn/y)**3)/(1.0 - (hc/y)**3)
is different from the equation on top.
dh/dx = - s0 * (1 - (hn-h)^3)/(1 - (hc-h)^3)

Chaotic billiards simulation

I came to ask for some help with maths and programming.
What am I trying to do? I'm trying to implement a simulation of a chaotic billiard system, following the algorithm in this excerpt.
How am I trying it? Using numpy and matplotlib, I implemented the following code
def boundaryFunction(parameter):
return 1 + 0.1 * np.cos(parameter)
def boundaryDerivative(parameter):
return -0.1 * np.sin(parameter)
def trajectoryFunction(parameter):
aux = np.sin(beta - phi) / np.sin(beta - parameter)
return boundaryFunction(phi) * aux
def difference(parameter):
return trajectoryFunction(parameter) - boundaryFunction(parameter)
def integrand(parameter):
rr = boundaryFunction(parameter)
dd = boundaryDerivative (parameter)
return np.sqrt(rr ** 2 + dd ** 2)
##### Main #####
length_vals = np.array([], dtype=np.float64)
alpha_vals = np.array([], dtype=np.float64)
# nof initial phi angles, alpha angles, and nof collisions for each.
n_phi, n_alpha, n_cols, count = 10, 10, 10, 0
# Length of the boundary
total_length, err = integrate.quad(integrand, 0, 2 * np.pi)
for phi in np.linspace(0, 2 * np.pi, n_phi):
for alpha in np.linspace(0, 2 * np.pi, n_alpha):
for n in np.arange(1, n_cols):
nu = np.arctan(boundaryFunction(phi) / boundaryDerivative(phi))
beta = np.pi + phi + alpha - nu
# Determines next impact coordinate.
bnds = (0, 2 * np.pi)
phi_new = optimize.minimize_scalar(difference, bounds=bnds, method='bounded').x
nu_new = np.arctan(boundaryFunction(phi_new) / boundaryDerivative(phi_new))
# Reflection angle with relation to tangent.
alpha_new = phi_new - phi + nu - nu_new - alpha
# Arc length for current phi value.
arc_length, err = integrate.quad(integrand, 0, phi_new)
# Append values to list
length_vals = np.append(length_vals, arc_length / total_length)
alpha_vals = np.append(alpha_vals, alpha)
count += 1
print "{}%" .format(100 * count / (n_phi * n_alpha))
What is the problem? When calculating phi_new, the equation has two solutions (assuming the boundary is convex, which is.) I must enforce that phi_new is the solution which is different from phi, but I don't know how to do that. Are there more issues with the code?
What should the output be? A phase space diagram of S x Alpha, looking like this.
Any help is very appreciated! Thanks in advance.
One way you could try would be (given there really are only two solutions) would be
epsilon = 1e-7 # tune this
delta = 1e-4 # tune this
# ...
bnds = (0, 2 * np.pi)
phi_new = optimize.minimize_scalar(difference, bounds=bnds, method='bounded').x
if abs(phi_new - phi) < epsilon:
bnds_1 = (0, phi - delta)
phi_new_1 = optimize.minimize_scalar(difference, bounds=bnds_1, method='bounded').x
bnds_2 = (phi + delta, 2 * np.pi)
phi_new_2 = optimize.minimize_scalar(difference, bounds=bnds_2, method='bounded').x
if difference(phi_new_1) < difference(phi_new_2):
phi_new = phi_new_1
else:
phi_new = phi_new_2
Alternatively, you could introduce a penalty-term, e.g. delta*exp(eps/(x-phi)^2) with appropriate choices of epsilon and delta.

Categories

Resources