# 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:

- 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¶

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.

### Mcmc¶

Generic structure of the algorithms implemented.

### Series¶

Class implementing time series analysis.

### Samplers¶

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

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