Code Monkey home page Code Monkey logo

pymle's People

Contributors

jkirkby3 avatar justinhdesk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pymle's Issues

wrong in TransitionDensity.py

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.

How to deal with the negative value appearing in the np.log() and np.sqrt() function when calculating density

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.,

if z <= 0:
return 0

Could you kindly share some references for handling this negative values like returning zero?
Best,
Shanchao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.