Skip to content

Instantly share code, notes, and snippets.

@springcoil
Created September 26, 2016 14:45
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 springcoil/2b46700e4322339a475b86645c33ddc7 to your computer and use it in GitHub Desktop.
Save springcoil/2b46700e4322339a475b86645c33ddc7 to your computer and use it in GitHub Desktop.
Testing the Euler_sde
Display the source blob
Display the rendered blob
Raw
In [1]: import pymc3 as pm
In [2]: pm.distributions.timeseries.EulerMaruyama?
Init signature: pm.distributions.timeseries.EulerMaruyama(name, *args, **kwargs)
Docstring:
Stochastic differential equation discretized with the Euler-Maruyama method.
Parameters
----------
dt : float
time step of discretization
sde_fn : callable
function returning the drift and diffusion coefficients of SDE
sde_pars : tuple
parameters of the SDE, passed as *args to sde_fn
File: ~/Code/pymc3/pymc3/distributions/timeseries.py
Type: type
In [3]: from pm.distributions.timeseries import EulerMaruyama
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-6d6ca1e840c0> in <module>()
----> 1 from pm.distributions.timeseries import EulerMaruyama
ImportError: No module named 'pm'
In [4]: pm
Out[4]: <module 'pymc3' from '/Users/peadarcoyle/Code/pymc3/pymc3/__init__.py'>
In [5]: import pymc3.distributions.timeseries as pm.distributions.timeseries
File "<ipython-input-5-6d1bed62fd5e>", line 1
import pymc3.distributions.timeseries as pm.distributions.timeseries
^
SyntaxError: invalid syntax
In [6]: from pymc3.distributions.timeseries import EulerMaruyama
In [7]: help(EulerMaruyama)
In [8]:
In [8]: EulerMaruyama?
Init signature: EulerMaruyama(name, *args, **kwargs)
Docstring:
Stochastic differential equation discretized with the Euler-Maruyama method.
Parameters
----------
dt : float
time step of discretization
sde_fn : callable
function returning the drift and diffusion coefficients of SDE
sde_pars : tuple
parameters of the SDE, passed as *args to sde_fn
File: ~/Code/pymc3/pymc3/distributions/timeseries.py
Type: type
In [9]: def drift(x,t):
...: return t*x
...:
In [10]: EulerMaruyama(2.0, drift(1,2))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/Users/peadarcoyle/Code/pymc3/pymc3/model.py in get_context(cls)
115 try:
--> 116 return cls.get_contexts()[-1]
117 except IndexError:
IndexError: list index out of range
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/Users/peadarcoyle/Code/pymc3/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
17 try:
---> 18 model = Model.get_context()
19 except TypeError:
/Users/peadarcoyle/Code/pymc3/pymc3/model.py in get_context(cls)
117 except IndexError:
--> 118 raise TypeError("No context on context stack")
119
TypeError: No context on context stack
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-10-a94d2721635e> in <module>()
----> 1 EulerMaruyama(2.0, drift(1,2))
/Users/peadarcoyle/Code/pymc3/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
18 model = Model.get_context()
19 except TypeError:
---> 20 raise TypeError("No model on context stack, which is needed to "
21 "use the Normal('x', 0,1) syntax. "
22 "Add a 'with model:' block")
TypeError: No model on context stack, which is needed to use the Normal('x', 0,1) syntax. Add a 'with model:' block
In [11]: EulerMaruyama(2.0, drift(1,0))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/Users/peadarcoyle/Code/pymc3/pymc3/model.py in get_context(cls)
115 try:
--> 116 return cls.get_contexts()[-1]
117 except IndexError:
IndexError: list index out of range
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/Users/peadarcoyle/Code/pymc3/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
17 try:
---> 18 model = Model.get_context()
19 except TypeError:
/Users/peadarcoyle/Code/pymc3/pymc3/model.py in get_context(cls)
117 except IndexError:
--> 118 raise TypeError("No context on context stack")
119
TypeError: No context on context stack
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-11-cac5cc2fc6cb> in <module>()
----> 1 EulerMaruyama(2.0, drift(1,0))
/Users/peadarcoyle/Code/pymc3/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
18 model = Model.get_context()
19 except TypeError:
---> 20 raise TypeError("No model on context stack, which is needed to "
21 "use the Normal('x', 0,1) syntax. "
22 "Add a 'with model:' block")
TypeError: No model on context stack, which is needed to use the Normal('x', 0,1) syntax. Add a 'with model:' block
In [12]: dt = 4.0
In [13]: import numpy as np
In [14]: np.linspace(0.0, 2, 12)
Out[14]:
array([ 0. , 0.18181818, 0.36363636, 0.54545455, 0.72727273,
0.90909091, 1.09090909, 1.27272727, 1.45454545, 1.63636364,
1.81818182, 2. ])
In [15]: Dt = np.linspace(0.0, 2, 12)
In [16]: dt = Dt[1]
In [17]: DW = np.random.rand(2, 100) * sqrt(dt)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-17-4dd5da5cbe1d> in <module>()
----> 1 DW = np.random.rand(2, 100) * sqrt(dt)
NameError: name 'sqrt' is not defined
In [18]: DW = np.random.rand(2, 100) * np.sqrt(dt)
In [19]: DW
Out[19]:
array([[ 0.14465351, 0.22520468, 0.39751699, 0.10716463, 0.19360521,
0.31153116, 0.05761513, 0.01412758, 0.07721464, 0.38753596,
0.17978391, 0.25598329, 0.34719351, 0.19724166, 0.21532362,
0.17854584, 0.16407368, 0.02910817, 0.29921227, 0.4240718 ,
0.04475854, 0.25122942, 0.07721465, 0.10763928, 0.29265415,
0.23981001, 0.40763148, 0.32342419, 0.3117144 , 0.3799668 ,
0.05028443, 0.01509876, 0.26878639, 0.09877193, 0.22974507,
0.2049723 , 0.1007016 , 0.17694232, 0.24337021, 0.04606625,
0.16949454, 0.27034794, 0.02942346, 0.02868173, 0.3563495 ,
0.27213763, 0.00701184, 0.20039175, 0.42488034, 0.08420734,
0.11663545, 0.38800166, 0.18279421, 0.07020729, 0.29468917,
0.32019468, 0.10975705, 0.15224128, 0.00955467, 0.08984983,
0.24319534, 0.00721825, 0.27320645, 0.22230949, 0.3359596 ,
0.1608497 , 0.21716382, 0.01464809, 0.04696097, 0.38515856,
0.10669029, 0.1518438 , 0.125818 , 0.3573952 , 0.34789692,
0.15446122, 0.17131119, 0.3685311 , 0.37573802, 0.35911347,
0.29068843, 0.34979726, 0.00201593, 0.22641128, 0.09787641,
0.09385365, 0.34699478, 0.07891091, 0.2032107 , 0.11717648,
0.22989897, 0.29342408, 0.02487022, 0.01402238, 0.24009102,
0.04921841, 0.1648915 , 0.08921225, 0.38329055, 0.10116879],
[ 0.08581443, 0.22437234, 0.0783111 , 0.05926941, 0.15526582,
0.28164058, 0.17989175, 0.05954021, 0.27563292, 0.14647983,
0.27741351, 0.05094232, 0.28784671, 0.05879102, 0.20025116,
0.31581221, 0.33919999, 0.11110361, 0.17369151, 0.19581344,
0.35905025, 0.24666575, 0.37077008, 0.13068126, 0.02217936,
0.12605149, 0.06391398, 0.31639945, 0.21151435, 0.09216551,
0.38309929, 0.19599267, 0.04743272, 0.35927557, 0.30280796,
0.3801529 , 0.18579087, 0.34804023, 0.346656 , 0.08216258,
0.08555963, 0.36491111, 0.28328984, 0.3291515 , 0.27342323,
0.34900384, 0.06476383, 0.00892561, 0.36518935, 0.21104539,
0.30267101, 0.26355325, 0.28733292, 0.20015628, 0.38836872,
0.04537482, 0.27910202, 0.18574873, 0.14567074, 0.39866891,
0.41052628, 0.20863832, 0.34400395, 0.28301699, 0.36086856,
0.33661165, 0.37387007, 0.08084559, 0.23812324, 0.32236612,
0.1832781 , 0.35694783, 0.12996314, 0.1354343 , 0.29830749,
0.2479505 , 0.27289863, 0.14653161, 0.04742004, 0.38453723,
0.28088607, 0.37769498, 0.24474374, 0.0264286 , 0.16646393,
0.2766279 , 0.0777525 , 0.39536645, 0.27922717, 0.03801697,
0.41307015, 0.41553937, 0.39116344, 0.23351107, 0.17003069,
0.353576 , 0.42063941, 0.19522205, 0.16072622, 0.28177761]])
In [20]: type(DW)
Out[20]: numpy.ndarray
In [21]: EulerMaruyama(dt, DW)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment