# trendpy¶

trendpy is a bayesian filtering micro library.

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¶

The filter method outputs the filtered trend of the input time series (only the time-series values can be recognized by trendpy):

      >>> from trendpy import filter
>>> trend = filter(data['y'])


## 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. new instance of a Strategy() subclass 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. initial value of the parameter 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. dictionary the parameters needed to compute the next value of the Markov chain for the parameter with name: parameter_name. 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 random draw from the posterior probability distribution 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 output of the MCMC algorithm Numpy.dnarray
run(number_simulations, max_restart, verbose)[source]

Runs the MCMC algorithm.

Parameters: number_simulations (int) – number of random draws for each parameter. max_restart (int) – number of times the MCMC routine is allowed to restart. verbose (int) – control console log information detail.

### 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. initial value of the parameter 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. dictionary the parameters needed to compute the next value of the Markov chain for the parameter with name: parameter_name. 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 random draw from the posterior probability distribution 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 output of the MCMC algorithm Numpy.dnarray

## Trendpy Changelog¶

We detail here the changes made to the library

### Version 1.0.0¶

Release day: May 28 2018

• First stable release

### Version 0.3.1¶

Release day: May 28 2018

• Minor bug fix after API change

### Version 0.3.0¶

Release day: May 28 2018

• Major bug fixes after API change

### Version 0.2.0¶

Release day: October 5 2017

• Major bug fixes in the L1 Sampler (problem with output type)

### Version 0.1.2¶

Release day: September 6 2017

• Some bug fixes
• Deleted the Series class

### Version 0.1.1¶

Release day: August 24 2017

• Initial public release.

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.

### Authors¶

trendpy is written and maintained by Rene-Jean Corneille

### Contributing¶

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

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.