Skip to content

Instantly share code, notes, and snippets.

@jaj42
Created August 12, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaj42/eca83147c11d6ad8e955b1e14b2ba400 to your computer and use it in GitHub Desktop.
Save jaj42/eca83147c11d6ad8e955b1e14b2ba400 to your computer and use it in GitHub Desktop.
Show values of the time parameter provided to ODEs by PYMC
import numpy as np
import pymc as pm
from pymc.ode import DifferentialEquation
from scipy.integrate import odeint
def freefall(y, t, p):
print(t, type(t))
return 2.0 * p[1] - p[0] * y[0]
times = np.arange(0, 10, 0.5)
gamma, g, y0, sigma = 0.4, 9.8, -2, 2
y = odeint(freefall, t=times, y0=y0, args=tuple([[gamma, g]]))
yobs = np.random.normal(y, 2)
ode_model = DifferentialEquation(
func=freefall, times=times, n_states=1, n_theta=2, t0=0
)
with pm.Model() as model:
sigma = pm.HalfCauchy("sigma", 1)
gamma = pm.Lognormal("gamma", 0, 1)
ode_solution = ode_model(y0=[0], theta=[gamma, 9.8])
Y = pm.Normal("Y", mu=ode_solution, sigma=sigma, observed=yobs)
idata = pm.sample(tune=100, draws=100, chains=1, cores=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment