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:
- Numpy (http://www.numpy.org)
- SciPy (http://www.scipy.org)
- Pandas (http://pandas.pydata.org/)
- matplotlib (http://http://matplotlib.org/)
- statsmodels (http://www.statsmodels.org/stable/index.html)
Issues¶
Should you encounter any issue with the library you can raise them here: https://github.com/ronsenbergVI/trendpypy/issues
Installing trendpy¶
Installation with pip (recommended)¶
The library is available on pypi and to install the last available version, run this command:
$ pip install trendpy
To test the installation:
>>> import trendpy
>>> trendpy.__version__
this should display the version installed on your system.
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:
The trend filtering equation becomes:
A common assumption on the dynamic of the noise is:
with \(\sigma>0\) and \(W\) a standard Brownian motion. From a theoretical point of view trend filtering is equivalent to finding the functional form:
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
>>> from pandas import read_csv
>>> data = read_csv('data.csv')
>>> 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.
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)
-
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
-
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
-
-
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.
-
-
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
-
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
-
Trendpy Changelog¶
We detail here the changes made to the library
Version 0.2.0¶
Release day: October 5 2017
- Major bug fixes in the L1 Sampler (problem with output type)
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.