- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这里,我的目标是估计阻尼谐振子的参数( Gamma 和欧米茄),由下式给出
dX^2/dt^2+gamma*dX/dt+(2*pi*omega)^2*X=0. (We can add white gaussian noise to the system.)
import pymc
import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt;
from scipy.integrate import odeint
#import data
xdata = sio.loadmat('T.mat')['T'][0] #time
ydata1 = sio.loadmat('V1.mat')['V1'][0] # V2=dV1/dt, (X=V1),
ydata2 = sio.loadmat('V2.mat')['V2'][0] # dV2/dt=-(2pi*omega)^2*V1-gama*V2
#time span for solving the equations
npts= 500
dt=0.01
Tspan=5.0
time = np.linspace(0,Tspan,npts+1)
#initial condition
V0 = [1.0, 1.0]
# Priors for unknown model parameters
sigma = pymc.Uniform('sigma', 0.0, 100.0)
gama= pymc.Uniform('gama', 0.0, 20.0)
omega=pymc.Uniform('omega',0.0, 20.0)
#Solve the equations
@pymc.deterministic
def DHOS(gama=gama, omega=omega):
V1= np.zeros(npts+1)
V2= np.zeros(npts+1)
V1[0] = V0[0]
V2[0] = V0[1]
for i in range(1,npts+1):
V1[i]= V1[i-1] + dt*V2[i-1];
V2[i] = V2[i-1] + dt*(-((2*np.pi*omega)**2)*V1[i-1]-gama*V2[i-1]);
return [V1, V2]
#or we can use odeint
#@pymc.deterministic
#def DHS( gama=gama, omega=omega):
# def DOS_func(y, time):
# V1, V2 = y[0], y[1]
# dV1dt = V2
# dV2dt = -((2*np.pi*omega)**2)* V1 -gama*V2
# dydt = [dV1dt, dV2dt]
# return dydt
# soln = odeint(DOS_func,V0, time)
# V1, V2 = soln[:,0], soln[:,1]
# return V1, V2
# value of outcome (observations)
V1 = pymc.Lambda('V1', lambda DHOS=DHOS: DHOS[0])
V2 = pymc.Lambda('V2', lambda DHOS=DHOS: DHOS[1])
# liklihood of observations
Yobs1 = pymc.Normal('Yobs1', mu=V1, tau=1.0/sigma**2, value=ydata1, observed=True)
Yobs2 = pymc.Normal('Yobs2', mu=V2, tau=1.0/sigma**2, value=ydata2, observed=True)
import pymc
import DampedOscil_model
MDL = pymc.MCMC(DampedOscil_model, db='pickle')
MDL.sample(iter=1e4, burn=1e2, thin=2)
gama_trace=MDL.trace('gama')[- 1000:]
omega_trace=MDL.trace('omega')[-1000:]
gama=MDL.gama.value
omega=MDL.omega.value
import matplotlib.pyplot as plt
import scipy.io as sio
import pandas as pd
import numpy as np
import pymc3 as pm
import theano.tensor as tt
import theano
from pymc3 import Model, Normal, HalfNormal, Uniform
from pymc3 import NUTS, find_MAP, sample, Slice, traceplot, summary
from pymc3 import Deterministic
from scipy.optimize import fmin_powell
#import data
xdata = sio.loadmat('T.mat')['T'][0] #time
ydata1 = sio.loadmat('V1.mat')['V1'][0] # V2=dV1/dt, (X=V1),
ydata2 = sio.loadmat('V2.mat')['V2'][0] # dV2/dt=-(2pi*omega)^2*V1-gama*V2
#time span for solving the equations
npts= 500
dt=0.01
Tspan=5.0
time = np.linspace(0,Tspan,npts+1)
niter=10000
burn=niter//2;
with pm.Model() as model:
#Priors for unknown model parameters
sigma = pm.HalfNormal('sigma', sd=1)
gama= pm.Uniform('gama', 0.0, 20.0)
omega=pm.Uniform('omega',0.0, 20.0)
#initial condition
V0 = [1.0, 1.0]
#Solve the equations
# do I need to use theano.tensor here?!
@theano.compile.ops.as_op(itypes=[tt.dscalar, tt.dscalar],otypes=[tt.dvector])
def DHOS(gama=gama, omega=omega):
V1= np.zeros(npts+1)
V2= np.zeros(npts+1)
V1[0] = V0[0]
V2[0] = V0[1]
for i in range(1,npts+1):
V1[i]= V1[i-1] + dt*V2[i-1];
V2[i] = V2[i-1] + dt*(-((2*np.pi*1)**2)*V1[i-1]-gama*V2[i-1]);
return V1,V2
V1 = pm.Deterministic('V1', DHOS[0])
V2 = pm.Deterministic('V2', DHOS[1])
start = pm.find_MAP(fmin=fmin_powell, disp=True)
step=pm.NUTS()
trace=pm.sample(niter, step, start=start, progressbar=False)
traceplot(trace);
Summary=pm.df_summary(trace[-1000:])
gama_trace = trace.get_values('gama', burn)
omega_trace = trace.get_values('omega', burn)
TypeError: 'FromFunctionOp' object does not support indexing
@pymc.deterministic
def DOS(gama=gama, omega=omega):
V1= np.zeros(npts+1)
V2= np.zeros(npts+1)
V1[0] = V0[0]
V2[0] = V0[1]
for i in range(1,npts+1):
V1[i]= V1[i-1] + dt*V2[i-1];
V2[i] = V2[i-1] + dt*(-((2*np.pi*omega)**2)*V1[i-1]-gama*V2[i-1]);
return [V1, V2]
V1 = pymc.Lambda('V1', lambda DOS=DOS: DOS[0])
V2 = pymc.Lambda('V2', lambda DOS=DOS: DOS[1])
最佳答案
我建议并已成功实现,使用“黑匣子”方法与 PYMC3 连接。在这种情况下,这意味着自己计算对数似然,然后使用 PYMC3 对其进行采样。这需要以 Theano 和 PYMC3 可以与它们交互的方式编写您的函数。
这是在 notebook 中概述的在 PYMC3 页面上,以 cython 为例。
这是需要完成的工作的简短示例。
首先,您可以加载数据并设置所需的任何参数,例如时间步长等。
import pymc3 as pm
import numpy as np
import theano
import theano.tensor as tt
#import data
xdata = sio.loadmat('T.mat')['T'][0] #time
ydata1 = sio.loadmat('V1.mat')['V1'][0] # V2=dV1/dt, (X=V1),
ydata2 = sio.loadmat('V2.mat')['V2'][0] # dV2/dt=-(2pi*omega)^2*V1-gama*V2
#time span for solving the equations
npts= 500
dt=0.01
Tspan=5.0
time = np.linspace(0,Tspan,npts+1)
#initial condition
V0 = [1.0, 1.0]
def DHOS(theta):
gama,omega=theta
V1= np.zeros(npts+1)
V2= np.zeros(npts+1)
V1[0] = V0[0]
V2[0] = V0[1]
for i in range(1,npts+1):
V1[i]= V1[i-1] + dt*V2[i-1];
V2[i] = V2[i-1] + dt*(-((2*np.pi*omega)**2)*V1[i-1]-gama*V2[i-1]);
return [V1, V2]
def my_loglike(theta,data,sigma):
"""
A Gaussian log-likelihood function for a model with parameters given in theta
"""
model = DHOS(theta) #V1 and V2 from the DHOS function
#Here data = [ydata1,ydata2] to compare with model
#sigma is either the same shape as model or a scalar
#which corresponds to the uncertainty on the data.
return -(0.5)*sum((data - model)**2/sigma**2)
# define a theano Op for our likelihood function
class LogLike(tt.Op):
"""
Specify what type of object will be passed and returned to the Op when it is
called. In our case we will be passing it a vector of values (the parameters
that define our model) and returning a single "scalar" value (the
log-likelihood)
"""
itypes = [tt.dvector] # expects a vector of parameter values when called
otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)
def __init__(self, loglike, data, sigma):
"""
Initialise the Op with various things that our log-likelihood function
requires. Below are the things that are needed in this particular
example.
Parameters
----------
loglike:
The log-likelihood (or whatever) function we've defined
data:
The "observed" data that our log-likelihood function takes in
x:
The dependent variable (aka 'x') that our model requires
sigma:
The noise standard deviation that our function requires.
"""
# add inputs as class attributes
self.likelihood = loglike
self.data = data
self.sigma = sigma
def perform(self, node, inputs, outputs):
# the method that is used when calling the Op
theta, = inputs # this will contain my variables
# call the log-likelihood function
logl = self.likelihood(theta, self.data, self.sigma)
outputs[0][0] = array(logl) # output the log-likelihood
ndraws = 10000 # number of draws from the distribution
nburn = 1000 # number of "burn-in points" (which we'll discard)
# create our Op
logl = LogLike(my_loglike, rdat_sim, 10)
# use PyMC3 to sampler from log-likelihood
with pm.Model():
gama= pm.Uniform('gama', 0.0, 20.0)
omega=pm.Uniform('omega',0.0, 20.0)
# convert m and c to a tensor vector
theta = tt.as_tensor_variable([gama,omega])
# use a DensityDist (use a lamdba function to "call" the Op)
pm.DensityDist('likelihood', lambda v: logl(v), observed={'v': theta})
trace = pm.sample(ndraws, tune=nburn, discard_tuned_samples=True)
_ = pm.traceplot(trace)
关于python - 在 PYMC3 中求解 ODE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109292/
我有以下代码: def multirk4(funcs, x0, y0, step, xmax): n = len(funcs) table = [[x0] + y0] f1,
我写了下面的代码。它是一个 ODE,其中有一个参数作为另一个 ODE。正如我们所见,M(m0,z,b,c) 用于另一个ODE,它本身就是一个ODE 函数。代码非常慢,有人能给我建议如何改进它吗? im
我目前正在尝试使用 SciPy 的 integrate.ode 包来求解一对耦合的一阶 ODE:比如 Lotka-Volterra predator-prey equation .但是,这意味着在集成
我在使用 scipy.integrate.ode 求解两个非常简单的非耦合 ODE 时遇到了问题。例如下面的简单代码: from scipy.integrate import ode def f(
我有以下包含一些颂歌的函数: myfunction 20) { # this is an internal threshold! Y <- 35000
是否使用odeint在这个方程中遇到了问题或 solve_ivp来解决。 import numpy as np from scipy.integrate import solve_ivp import
我有一个函数dφ/dt = γ - F(φ) (其中 F(φ) -- a 是 2π -周期函数)和函数图 F(φ) . 我需要创建一个输出 φ(t) 的 6 个图的程序对于 γ 的不同值(γ = 0.
我正在尝试解决来自 DifferentialEquation 包的典型示例, 根据他们页面上的指南。 这是示例: using DifferentialEquations using Plots fun
我正在尝试复制发布的湖泊食物网络模型 here 。该模型代表两条食物链(沿海与远洋),由顶级捕食者(鱼类)连接。我已经对模型进行了编码,但是当我在 2-3 个时间步骤后运行它时,模型会生成 NaN。我
我正在尝试复制发布的湖泊食物网络模型 here 。该模型代表两条食物链(沿海与远洋),由顶级捕食者(鱼类)连接。我已经对模型进行了编码,但是当我在 2-3 个时间步骤后运行它时,模型会生成 NaN。我
我正在编写一些代码,其中我有以下方程组 here .问题是我非常想解决多个 k 值以及为每个 k 值绘制相平面/颤动图。有人可以帮帮我吗?到目前为止,这是我对求解器的了解: import numpy
我有一些微分方程需要使用 MATLAB 的 ODE 求解器求解。虽然微分方程本身相当简单,但它们取决于很多“常数”。这些常量不是通用的,需要由调用者提供。 这种 ODE 的例子是: dx/dt = -
我有一个看起来像的系统 dn/dt=f(n,v) dh/dt=g(h,v) 我想在流形 F(v,n,h)=0 上求解这个方程,流形是 v 中的非线性函数。我尝试使用类似 v=fzero(@(x) F(
我正在尝试求解具有复杂条目的 ODE 系统。从 GSL 文档可以看出它只接受真实条目。有没有办法传递复杂的(比区分实部和虚部更直接的方法)?如果不可能,您能否为此目的推荐任何其他好的图书馆? 最佳答案
我一直在尝试实现贝叶斯 ODE。在石油工业中,我们使用以下等式来拟合生产数据然后进行预测: ODE 方程描述为: 其中 0 我的初始代码: using DiffEqFlux, OrdinaryDiff
我一直在尝试实现 ODE 模型来模拟胰岛素信号通路,正如 supplementary material 中所介绍的那样的 this paper , 使用 python's GEKKO . 实现的模型变
我们可以使用 deSolve R 中的常微分方程 (ODE) 包,但是,我找不到解决两个嵌套 ODE 方程的方法,假设` b'(t) = beta - k*b(t); a'(t) = alpha -b
我正在尝试找出 Dymola 解决 Modelica 代码所需的步骤。通过阅读一些引用文献和书籍,我了解到 Dymola: 将 Modelica 代码转换为混合 DAE(扁平化)。 操纵 DAE 以将
这是我在这里的第一个问题,所以如果格式被关闭,我很抱歉。 我想将牛顿万有引力定律建模为 Python 中的二阶微分方程,但结果图没有意义。供引用,here's the equation和[这是结果][
如何使用 Sympy 求解矩阵微分方程? 我有一个形式为 y'(t) = A*y(t) + B 的方程,其中 A 是一个 3x3 矩阵,y(t) 是一个 1x3 向量,B 是一个 1x3 向量。 更具
我是一名优秀的程序员,十分优秀!