0
$\begingroup$

I am learning about monte carlo simulations and I have found many blogs explaining its implementation in python. Because its a widely known and an important technique for structuring asset prices. I want to know if there are any good libraries in python for monte carlo simulations on financal instruments.

$\endgroup$
  • $\begingroup$ Hi and welcome. And I have to ask one thing: the "many blogs" that explain MC in Python ... don't they load libraries? $\endgroup$ – Ric May 12 '19 at 17:21
  • $\begingroup$ They program MC from scratch and their implementation differ from each other. I want something consistent. $\endgroup$ – Eka May 13 '19 at 0:15
5
$\begingroup$

Try Quantlib https://www.quantlib.org, it comes with everything you need.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Am I right to understand that Quantlib is written entirely in C++, i.e. there is no Python code I could use to "learn" or "modify"? In other words, I could access the MC functionality of Quantlib, via functions exported to Python as-is. However, it is not useful for looking to learn "How to code an MC algorithm in Python". Is that right? $\endgroup$ – bhutes May 12 '19 at 11:49
3
$\begingroup$

We recently released qmcpy which does both Monte Carlo and quasi-Monte Carlo with guaranteed accuracy.

For a MC/qMC problem in our framework you need to define your function, measure, discrete distribution (iid standard uniform, iid standard Gaussian, ...), and an algorithm to determine the number of points needed to meet your error tolerance. Lots of examples and components are already implemented so most problems shouldn't take more than a few lines.

If you get a chance check it out and let me know what you think!

| improve this answer | |
$\endgroup$
  • $\begingroup$ here is a link to the quickstart guide $\endgroup$ – alegresor Jun 12 at 22:53
2
$\begingroup$

You can directly use pandas-montecarlo to perform a Monte-Carlo simulation.

Code for the same:

# Import data
import pandas_montecarlo
from pandas_datareader import data
data = data.get_data_yahoo('AAPL', '2017-01-01', '2018-01-01')

# Calculate Returns
data['return'] = data.Close.pct_change()

# Perform Monte-Carlo Simulation
data['return'].montecarlo(sims=5).plot()

enter image description here

For more detail, you can read the pandas-montecarlo documentation here.

| improve this answer | |
$\endgroup$
0
$\begingroup$

That's very vague question. You don't need libraries, as first step you need to define what you want to do. E.g. if you want to use GBM. You can take a look code I have written. I have solutions for few exotic options.

My Library for GBM in Python

| improve this answer | |
$\endgroup$
0
$\begingroup$

Alternatively try scipy.stats, in combination with numpy.

MC in its raw form is just a numerical random process. You can implement your own random processes in one or two lines with the stats library.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.