Git Product home page Git Product logo

pymle's Introduction

PyMLE

Python library for Maximum Likelihood estimation (MLE) and simulation of Stochastic Differntial Equations (SDE), i.e. continuous diffusion processes.

This library supports many models out of the box (e.g. Brownian Motion, Geometric Brownian Motion, CKLS, CIR, OU, etc.), and is designed to make it easy to add new models with minimial code, and to inheret the fitting and simulation of these models for free.

MLE procedures: Ait-Sahalia Hermite Polynomial Expansion, Kessler, Euler, Shoji-Ozaki, Elerian, and Exact MLE (when applicable)

Simulation schemes: Euler, Milstein, Milstein-2, Exact (when available)

Diffusion Models: Brownian motion, Geometric Brownian motion (GBM), IGBM, Peral-Verhulst, Linear SDE, Logistic, 3/2, CEV, CIR, CKLS, Feller's square root, Hyperbolic, Hyperbolic 2, Jacobi, Modified CIR, OU, Radial OU, Pearson, Nonlinear Mean Reversion, flexible Nonlinear SDE, Custom models easily supported

Origin and Future Goals

This library was designed from a research collaboration with J.L. Kirkby, Dang H. Nguyen, Duy Nguyen, and Nhu N. Nguyen. The goal is to provide a wide set of functionality for python users to simulate and estimate SDEs, as well as estimation tools for related statistical problems. The starting point is standard / state-of-the-art MLE estimation procedures, to be followed up with more cutting edge approaches.

User installation

pip install pymle-diffusion

For the latest version, you can also install directly from github:

pip install git+https://github.com/jkirkby3/pymle.git

Dependencies

pymle requires:

  • Python (>= 3.7)
  • NumPy (tested with 1.20.2)

Source code

You can check the latest sources with the command

git clone https://github.com/jkirkby3/pymle.git

Example: Fit MLE to Simulated Cox-Ingersol-Ross (CIR) sample

from pymle.models import CIR
from pymle.sim.Simulator1D import Simulator1D
from pymle.core.TransitionDensity import ExactDensity, KesslerDensity
from pymle.fit.AnalyticalMLE import AnalyticalMLE
import numpy as np

# ===========================
# Set the true model (CIR) params, to simulate the process
# ===========================
model = CIR()  # Cox-Ingersol-Ross 

kappa = 3  # rate of mean reversion
mu = 0.3  # long term level of process
sigma = 0.2  # volatility

model.params = np.array([kappa, mu, sigma])

# ===========================
# Simulate a sample path (we will fit to this path)
# ===========================
S0 = 0.4  # initial value of process
T = 5  # num years of the sample
freq = 250  # observations per year
dt = 1. / freq
seed = 123  # random seed: set to None to get new results each time

simulator = Simulator1D(S0=S0, M=T * freq, dt=dt, model=model).set_seed(seed=seed)
sample = simulator.sim_path()

# ===========================
# Fit maximum Likelihood estimators
# ===========================
# Set the parameter bounds for fitting  (kappa, mu, sigma)
param_bounds = [(0, 10), (0, 4), (0.01, 1)]

# Choose some initial guess for params fit
guess = np.array([1, 0.1, 0.4])

# Fit using Kessler MLE
kessler_est = AnalyticalMLE(sample, param_bounds, dt, density=KesslerDensity(model)).estimate_params(guess)

print(f'\nKessler MLE: {kessler_est} \n')

# Fit using Exact MLE
exact_est = AnalyticalMLE(sample, param_bounds, dt, density=ExactDensity(model)).estimate_params(guess)

print(f'\nExact MLE: {exact_est}')
Kessler MLE: 
params      | [2.9628751  0.3157062  0.19978367] 
sample size | 1250
likelihood  | 4398.276719373996 
AIC         | -8790.553438747991
BIC         | -8775.160742257101 

Exact MLE: 
params      | [3.0169684  0.31590483 0.2011907 ] 
sample size | 1250
likelihood  | 4397.641069883833 
AIC         | -8789.282139767665
BIC         | -8773.889443276776

Example: Fit MLE to Historical Interest Rates (10 Year Constant maturity)

Data Source:

Board of Governors of the Federal Reserve System (US), 10-Year Treasury Constant Maturity Rate [DGS10], retrieved from FRED, Federal Reserve Bank of St. Louis; https://fred.stlouisfed.org/series/DGS10, April 11, 2021.

import numpy as np
from pymle.models.CKLS import CKLS
from pymle.core.TransitionDensity import KesslerDensity, ShojiOzakiDensity
from pymle.fit.AnalyticalMLE import AnalyticalMLE
from pymle.data.loader import load_10yr_CMrate

# ===========================
# Create the Hypothesized model (CKLS)
# ===========================
# (Chan, Karolyi, Longstaff and Sanders Model)

model = CKLS()

# Set bounds for param search, and some psuedo-reasonable initial guess
param_bounds = [(0.0, 10), (0.0, 10), (0.01, 3), (0.1, 2)]
guess = np.array([0.01, 0.1, 0.2, 0.6])

# ===========================
# Read in the data (interest rate time series)
# ===========================

df = load_10yr_CMrate()
sample = df['Rate'].values
dt = 1. / 252  # Daily observations

# ===========================
# Fit maximum Likelihood estimators
# ===========================

# Fit using Kessler MLE
kessler_est = AnalyticalMLE(sample, param_bounds, dt, density=KesslerDensity(model)).estimate_params(guess)

print(f'\nKessler MLE: {kessler_est} \n')

# Fit using Shoji-Ozaki MLE
shojioz_est = AnalyticalMLE(sample, param_bounds, dt, density=ShojiOzakiDensity(model)).estimate_params(guess)

print(f'\nShoji-Ozaki MLE: {shojioz_est}')
Kessler MLE: 
params      | [0.07140132 0.00326561 0.56032624 0.33638139] 
sample size | 14801 
likelihood  | 20274.894525560834 
AIC         | -40541.78905112167
BIC         | -40511.37925102152 

Shoji-Ozaki MLE: 
params      | [1.89153215e-02 2.13208446e-05 5.58760343e-01 3.37675286e-01] 
sample size | 14801 
likelihood  | 20275.049010989227 
AIC         | -40542.098021978454
BIC         | -40511.68822187831

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

Watchers

 avatar  avatar

pymle's Issues

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

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.

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.