Coder Social home page Coder Social logo

ranaroussi / pandas-montecarlo Goto Github PK

View Code? Open in Web Editor NEW
207.0 10.0 58.0 372 KB

A lightweight Python library for running simple Monte Carlo Simulations on Pandas Series data

License: GNU Lesser General Public License v3.0

Python 100.00%
python pandas montecarlo monte-carlo

pandas-montecarlo's Introduction

Monte Carlo Simulator for Pandas Series

Python version

PyPi version

PyPi status

Travis-CI build status

Patreon Status

Star this repo

Follow me on twitter

pandas-montecarlo is a lightweight Python library for running simple Monte Carlo Simulations on Pandas Series data.

Changelog »


Quick Start

Let's run a monte carlo simulation on the returns of SPY (S&P 500 Spider ETF).

First, let's download SPY's data and calculate the daily returns.

from pandas_datareader import data

df = data.get_data_yahoo("SPY")
df['return'] = df['Adj Close'].pct_change().fillna(0)

Next, we'll import pandas_montecarlo and run monte carlo simulation with 10 simulations (for demo simplifications) and bust/max drawdown set to -10.0% and goal threshhold set to +100.0% (defaults is >=0%):

import pandas_montecarlo
mc = df['return'].montecarlo(sims=10, bust=-0.1, goal=1)

Plot simulations

mc.plot(title="SPY Returns Monte Carlo Simulations")  # optional: , figsize=(x, y)

demo

Show test stats

print(mc.stats)

# prints
{
    'min':    0.98088401987146789,
    'max':    0.98088401987146934,
    'mean':   0.98088401987146911,
    'median': 0.98088401987146911,
    'std':    4.0792198665315552e-16,
    'maxdd': -0.17221175099828012,  # max drawdown
    'bust':   0.2,  # probability of going bust
    'goal':   0.0   # probability of reaching 100% goal
}

Show bust / max drawdown stats

print(mc.maxdd)

# prints
{
    'min':    -0.27743285515585991,
    'max':    -0.00031922711279186444,
    'mean':   -0.07888087155686732,
    'median': -0.06010335858432081,
    'std':     0.062172124557467685
}

Access raw simulations' DataFrame

print(mc.data.head())
original          1          2          3          4  ...       10

0 0.000000 0.017745 -0.002586 -0.005346 -0.042107 ... 0.00139 1 0.002647 0.000050 0.000188 0.010141 0.007443 ... 0.00108 2 0.000704 0.002916 0.005324 0.000073 -0.003238 ... 0.00071 3 0.004221 0.008564 0.001397 0.007950 -0.006392 ... 0.00902 4 0.003328 -0.000511 0.005123 0.013491 -0.005105 ... 0.00252

Installation

Install pandas_montecarlo using pip:

$ pip install pandas_montecarlo --upgrade --no-cache-dir

Requirements

pandas-montecarlo is distributed under the GNU Lesser General Public License v3.0. See the LICENSE.txt file in the release for details.

P.S.

Please drop me an note with any feedback you have.

Ran Aroussi

pandas-montecarlo's People

Contributors

ranaroussi 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pandas-montecarlo's Issues

double error MC simulation

Hi,

How can I use this package to do a Monte Carlo simulation for a demand model with two errors?

THX !

Predictions + main source ?

Hi,

I've been using your nice library recently but I was wondering if the main monte carlo source was pushed cause I can't seem to find anything else than the init.py file in the repo.

I also have a feature request : would it be possible to have a prediction feature such as a predict method for data forecasting ?

Thanks for your reply.

Total cumsum

Hi. As I see it "total = cumsum[-1:].T" always has the same value for all simulations (because all of them end in the same point). Which messes stats based on total.

The same issue is with "nobust = cumsum[cumsum.min()[cumsum.min() > -abs(bust)].index][-1:]"

I am afraid this is wrong completely

How does it do an approxiation? read wikipedia about it, MC simulation has to generate approximation points randomly. However you produce just random numbers, Am I wrong?

Example broken in pandas 0.24.1

Hi
the readme example fails with:

ImportError                               Traceback (most recent call last)
<ipython-input-2-51d2dd43773f> in <module>()
      1 #from pandas.api.types import is_list_like
----> 2 from pandas_datareader import data
      3 
      4 df = data.get_data_yahoo("SPY")
      5 df['return'] = df['Adj Close'].pct_change().fillna(0)

~/anaconda3/lib/python3.6/site-packages/pandas_datareader/__init__.py in <module>()
      1 from ._version import get_versions
----> 2 from .data import (DataReader, Options, get_components_yahoo,
      3                    get_dailysummary_iex, get_data_enigma, get_data_famafrench,
      4                    get_data_fred, get_data_google, get_data_moex,
      5                    get_data_morningstar, get_data_quandl, get_data_stooq,

~/anaconda3/lib/python3.6/site-packages/pandas_datareader/data.py in <module>()
     12     ImmediateDeprecationError
     13 from pandas_datareader.famafrench import FamaFrenchReader
---> 14 from pandas_datareader.fred import FredReader
     15 from pandas_datareader.google.daily import GoogleDailyReader
     16 from pandas_datareader.google.options import Options as GoogleOptions

~/anaconda3/lib/python3.6/site-packages/pandas_datareader/fred.py in <module>()
----> 1 from pandas.core.common import is_list_like
      2 from pandas import concat, read_csv
      3 
      4 from pandas_datareader.base import _BaseReader
      5 

ImportError: cannot import name 'is_list_like'

The solution is to use

import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like

However we then run into:

ImmediateDeprecationError                 Traceback (most recent call last)
<ipython-input-3-3d15747a3d26> in <module>()
      2 from pandas_datareader import data
      3 
----> 4 df = data.get_data_yahoo("SPY")
      5 df['return'] = df['Adj Close'].pct_change().fillna(0)

~/anaconda3/lib/python3.6/site-packages/pandas_datareader/data.py in get_data_yahoo(*args, **kwargs)
     61 
     62 def get_data_yahoo(*args, **kwargs):
---> 63     raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Actions'))
     64     return YahooDailyReader(*args, **kwargs).read()
     65 

ImmediateDeprecationError: 
Yahoo Actions has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.

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.