trendpy

trendpy is a bayesian filtering micro library.

The library also supports bayesian regression models (Lasso and Ridge).

Models are fitted using MCMC algorithms.

User’s Guide

Requirements

trendpy is build on top of the following libraries:

Issues

Should you encounter any issue with the library you can raise them here: https://github.com/ronsenbergVI/trendpypy/issues

Installing trendpy

Installation from GitHub

trendpy releases are also available on github (https://github.com/ronsenbergVI/trendpy). You first need to clone (or fork if you want to modify it) and

$ git clone https://github.com/ronsenbergVI/trendpy.git
$ cd trendpy
$ python setup.py build
$ python setup.py install

Introduction to filtering theory

Consider \((y_t)_{t \in [0,T]}\) the (continuous), normalized price process of a stock, verifying the decomposition:

\[\]

where \(x\) is the price trend and \(\epsilon\) a stochastic noise. The process of trend filtering consists in recovering \(x\) from the observations of \(y\). Under regularity conditions, the first derivative of \(x\) indicates up or down price trends:

\[\mu_t = \dfrac{dx_t}{dt}\]

The trend filtering equation becomes:

\[dy_t = \mu_tdt + d\epsilon_t\]

A common assumption on the dynamic of the noise is:

\[d\epsilon_t = \sigma_t dW_t\]

with \(\sigma>0\) and \(W\) a standard Brownian motion. From a theoretical point of view trend filtering is equivalent to finding the functional form:

\[x_t = \textbf{f}(t,y)\]

Quickstart

To create a new Series() instance from a csv file:

>>> from trendpy.series import Series
>>> data = Series.from_csv('data.csv')
>>> data.plot()

Adding new algorithms

The MCMC class containts the generic structure of any MCMC algorithm:

  • definition of the parameters (posterior distribution, dimensions)
  • initialisation of the parameters to be estimated
  • random simulation of the parameters from their respective posterior distributions

Thus any new algorithm can be added to the library by subclassing the Strategy class:

class BlackScholes(Strategy):

        def __init__():
                pass

Then the new MCMC instance just needs to be initialized with the new strategy and ran:

>>> new_mcmc = MCMC(self, SamplerFactory.create("BlackScholes",data))
>>> mcmc.run(number_simulations=50)
>>> estimation = mcmc.output()

API Reference

If you are looking for information on a specific function, class or method, this part of the documentation is for you.

Factory

Factory class handling the creation of bayesian algorithms.

class trendpy.factory.SamplerFactory[source]
static add(id, factory)[source]

Adds a class to factory with a chosen id.

Parameters:
  • id (str) – name of the class.
  • factory (Strategy.Factory) – factory subclass of a Strategy instace.
static create(id, *args, **kwargs)[source]

Creates an instance of the class.

Parameters:
  • id (str) – name of the class.
  • args (list) – Positional arguments.
  • kwargs (dict) – Keyword arguments.
Returns:

new instance of a Strategy() subclass

Return type:

Numpy.dnarray

Mcmc

Generic structure of the algorithms implemented.

class trendpy.mcmc.MCMC(sampler)[source]
sampler

implementation of the MCMC algorithm

simulations

dictionary containing the history of simulations (is None if the MCMC algorithm has not been ran yet)

define_parameters()[source]

Method to set the parameter set to be updated in the MCMC algorithm.

initial_value(parameter_name)[source]
Method that sets the initial value of the
parameters to be estimated.
Parameters:parameter_name (str) – name of the parameter.
Returns:initial value of the parameter
Return type:Numpy.dnarray
distribution_parameters(parameter_name)[source]
Method that sets the parameters of the posterior
distribution of the parameters to be estimated.
Parameters:parameter_name (str) – name of the parameter.
Returns:dictionary the parameters needed to compute the next value of the Markov chain for the parameter with name: parameter_name.
Return type:dict
generate(parameter_name)[source]
This method handles the generation of the random draws of
the Markov chain for each parameters.
Parameters:parameter_name (string) – name of the parameter of interest
Returns:random draw from the posterior probability distribution
Return type:Numpy.dnarray
output(burn, parameter_name)[source]

Computes the poserior mean of the parameters.

Parameters:
  • parameter_name (string) – name of the parameter of interest
  • burn (int) – number of draws dismissed as burning samples
Returns:

output of the MCMC algorithm

Return type:

Numpy.dnarray

run(number_simulations=100, max_restart=10)[source]

Runs the MCMC algorithm.

Parameters:number_simulations (int) – number of random draws for each parameter.

Series

Class implementing time series analysis.

class trendpy.series.Series[source]

Implements univariate time series.

Import the class

>>> from trendpy.series import Series

create from csv

>>> data = Series.from_csv('data.csv')
data
static from_csv(filename)[source]
Instantiate new time series from a csv file where the first
column is a timestamp or a date or a datetime. For now
Parameters:filename (str) – path of the file with extension (.csv or .txt)
Returns:the price time series
Return type:trendpy.series.Series
summary()[source]

Returns an ASCII table with basic statistics of the time series loaded.

Returns:ASCII table with main description of the data set
Return type:str
returns(period=1)[source]
Adds a new time series to the data with the returns of the original
time series.
Parameters:period (int, optional) – number of days between two consecutive observations used to compute the returns.
Returns:output of the MCMC algorithm
Return type:Numpy.dnarray
annualized_return()[source]

Computes the annualized return over a given period.

Parameters:period (str) – period of interest either a year, year-month or date range.
Returns:annualized return on the whole time series.
Return type:float
annualized_volatility()[source]

Computes the annualized return over a given period.

Returns:annualized return on the whole time series.
Return type:float
skewness()[source]

Computes the skewness of the returns empirical distribution.

Returns:skewness on the whole time series returns.
Return type:float
kurtosis()[source]

Computes the kurtosis of the returns empirical distribution.

Returns:kurtosis on the whole time series returns.
Return type:float
max_drawdown(window=30)[source]

Computes the maximum drawdown of the price time series.

Parameters:window (int, optional) – number of observations used.
Returns:drawdown duration of the time series.
Return type:float
periodic_returns(show=True)[source]

Computes the maximum drawdown of the price time series.

Returns:periodic return plot of the time series.
Return type:float
rolling_max_drawdown(window=30)[source]

Computes the rolling maximum drawdown of the time series.

Parameters:window (int, optional) – number of observations used.
Returns:rolling maximum drawdown of the time series.
Return type:pandas.DataFrame
rolling_volatility(window=360)[source]
Computes the annualized rolling volatility of the time series
with annualization possible. The formula used is the following:
Parameters:
  • window (int, optional) – number of observations used.
  • annualize (str, optional) –
Returns:

rolling_volatility of the time series.

Return type:

pandas.DataFrame

save(filename='export.csv', separator=', ', date_format='%d-%m-%y')[source]

Saves the data contained in the object to a csv file.

Parameters:
  • filename (str, optional) – path and name of the file to export
  • separator (str, optional) – separator between columns in file.
plot()[source]

Plots the time series.

filter(method='L1Filter', number_simulations=100, burns=50, total_variation=2, merge=True)[source]

Filters the trend of the time series.

Parameters:
  • method (str, optional) – path and name of the file to export
  • number_simulations (int, optional) – number of simulations in the MCMC algorithm
  • burns (int, optional) – number of draws dismissed as burning samples
  • merge (bool, optional) – joind the filtered trend to the current Series instance.
Returns:

Series of trend filtered.

Return type:

pandas.DataFrame

Samplers

Samplers tell the mcmc algorithm how to simulate the Markov chain.

class trendpy.samplers.Parameter(name, distribution, size, current_value=None)[source]

Implements an unknown parameter to be estimated

We first need to import the wanted posterior distribution in Scipy:

>>> from scipy.stats import norm

and then we can instanciate parameter:

>>> param1 = Parameter('lambda',norm,(1,1),0.1)
distribution

Subclass of the Scipy rv_continuous class.

size

Dimensions of the parameter.

name

Name of the parameter.

__init__(name, distribution, size, current_value=None)[source]

Creates a parameter to estimate in the MCMC algorithm.

Parameters:
  • name (tuple) – Name of the parameter (unique identification)
  • distribution (Scipy.stats.rv_continuous) – Posterior Probability distribution of the parameter.
  • size – Dimension of the parameter.
  • current_value (array) – Current value of the parameter
is_multivariate()[source]

Checks if the parameter is univariate.

class trendpy.samplers.Parameters(list=None, hierarchy=None)[source]

Implements the set of parameters to be estimated

We first need to import the wanted posterior distribution in Scipy.stats:

>>> from scipy.stats import invgamma

then we can create an empty parameter set and add a new parameter:

>>> param1 = Parameter('sigma2',invgamma,(1,1),0.09)
>>> params = Params()
>>> params.append(param1)
>>> print(params)
list

A dictionary with the parameters to estimate.

hierarchy

List containing the order in which the Gibbs sampler updates the parameter values.

__init__(list=None, hierarchy=None)[source]

Creates a parameter set to estimate in the MCMC algorithm.

Parameters:
  • list (dict) – A dictionary with the parameters to estimate
  • hierarchy (array) – List containing the order in which the Gibbs sampler updates the parameter values.
append(parameter)[source]
Adds a parameter to the parameter set.
First parameter added is the first in the hierarchy.
Parameters:parameter (trendpy.Parameter) – parameter to estimate
class trendpy.samplers.Sampler[source]

Abstract class for implementing Gibbs sampling algorithms and providing outputs.

parameters

Parameters to be estimated in the MCMC algorithm.

data

array with the price time series

define_parameters()[source]

Method to set the parameter set to be updated in the MCMC algorithm.

initial_value(parameter_name)[source]
Method that sets the initial value of the
parameters to be estimated.
Parameters:parameter_name (str) – name of the parameter.
Returns:initial value of the parameter
Return type:Numpy.dnarray
distribution_parameters(parameter_name)[source]
Method that sets the parameters of the posterior
distribution of the parameters to be estimated.
Parameters:parameter_name (str) – name of the parameter.
Returns:dictionary the parameters needed to compute the next value of the Markov chain for the parameter with name: parameter_name.
Return type:dict
generate(parameter_name)[source]
This method handles the generation of the random draws of
the Markov chain for each parameters.
Parameters:parameter_name (string) – name of the parameter of interest
Returns:random draw from the posterior probability distribution
Return type:Numpy.dnarray
output(simulations, burn, parameter_name)[source]

Computes the poserior mean of the parameters.

Parameters:
  • simulations (dict) – history of the Markov chain simulation
  • burn (int) – number of draws dismissed as burning samples
  • parameter_name (string) – name of the parameter of interest
Returns:

output of the MCMC algorithm

Return type:

Numpy.dnarray

Trendpy Changelog

We detail here the changes made to the library

Version 0.1.0

Release day: July 21 2017

Initial public release.

License

trendpy is licensed under the MIT Licence. It means that the source code provided in the binaries can be used, modified, or distributed freely for commercial or personal use with conditions only requiring preservation of copyright and license notices.

The full license text can be found below (trendpy-license).

Authors

trendpy is written and maintained by Rene-Jean Corneille

Contributing

Contribution will be welcomed once a first stable release is ready.

License Definitions

The following section contains the full license texts for trendpy and the documentation.

  • “AUTHORS” hereby refers to all the authors listed in the authors section.
  • The “trendpy-license” applies to all the source code shipped as part of trendpy (trendpy itself as well as the examples and the unittests) as well as documentation.

trendpy License

MIT License

Copyright (c) 2017 Rene Jean Corneille

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.