jkirkby3 / pymle Goto Github PK
View Code? Open in Web Editor NEWMaximum Likelihood estimation and Simulation for Stochastic Differential Equations (Diffusions)
License: MIT License
Maximum Likelihood estimation and Simulation for Stochastic Differential Equations (Diffusions)
License: MIT License
I find that if an SDE with term t in the drift or diffusion terms, the fit task will not work anymore.
So I check the code in TransitionDensity.py, and I found that your code confuses t and dt.
Following Code:
class EulerDensity(TransitionDensity):
def __init__(self, model: Model1D):
"""
Class which represents the Euler approximation transition density for a model
:param model: the SDE model, referenced during calls to the transition density
"""
super().__init__(model=model)
def __call__(self,
x0: Union[float, np.ndarray],
xt: Union[float, np.ndarray],
t: float) -> Union[float, np.ndarray]:
"""
The transition density obtained via Euler expansion
:param x0: float or array, the current value
:param xt: float or array, the value to transition to (must be same dimension as x0)
:param t: float, the time of observing Xt
:return: probability (same dimension as x0 and xt)
"""
sig2t = (self._model.diffusion(x0, t) ** 2) * 2 * t
mut = x0 + self._model.drift(x0, t) * t
return np.exp(-(xt - mut) ** 2 / sig2t) / np.sqrt(np.pi * sig2t)
In the code above, you pass the dt as t, and then used the dt in the self._model.drift and self._model.diffusion, the wrong likelihood leads to the failure in the fitting task finally. So you need to correct the code by passing the t and dt separately. The correct one may be like the following code.
def eulerdensity(self,
x0: Union[float, np.ndarray],
xt: Union[float, np.ndarray],
t: float,
dt: float) -> Union[float, np.ndarray]:
"""
The transition density obtained via Euler expansion
:param x0: float or array, the current value
:param xt: float or array, the value to transition to (must be same dimension as x0)
:param t: float, the time of observing Xt
:param dt: float, the time setps
:return: probability (same dimension as x0 and xt)
"""
sig2t = (self.diffusion(x0, t) ** 2) * 2 * dt
mut = x0 + self.drift(x0, t) * dt
# print(np.exp(-(xt - mut) ** 2 / sig2t) / np.sqrt(np.pi * sig2t))
return np.exp(-(xt - mut) ** 2 / sig2t) / np.sqrt(np.pi * sig2t)
I also suggest that you should check other formulas in the TransitionDensity.py for the same mistake since I only test the EulerDensity part.
Thank you for your effort in this python package, it is really nice as the first package for simulate and estimate SDE systemly with Python language.
Hi, Justin
Thanks for your great codes. However, I am wondering how to deal with the negative value appearing in the np.log()
and np.sqrt()
function when calculating the density.
For example, for the OzakiDensity, Kt
can be negative if 1 + self._model.drift(x0, t) * (np.exp(self._model.drift_x(x0, t) * t) - 1) / ( x0 * self._model.drift_x(x0, t)) < -1
, and Vt
can be negative when performing Vt=np.sqrt(Vt)
Kt = (1 / t) * np.log(1 + self._model.drift(x0, t) * (np.exp(self._model.drift_x(x0, t) * t) - 1) / (
x0 * self._model.drift_x(x0, t)))
Vt = sig ** 2 * (np.exp(2 * Kt * t) - 1) / (2 * Kt)
Vt = np.sqrt(Vt)
Thanks in advance for any suggestion! I am not sure if the strategy you did for other densities is optimal, e.g.,
pymle/pymle/TransitionDensity.py
Lines 182 to 183 in 6344c2e
Hello,
I've tried installing your package using "pip install git+https://github.com/jkirkby3/pymle.git" but I got this error: "error: package directory 'ctmc' does not exist". Could you please explain how to get rid of that error?
Thank you in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.