Skip to content

ethanlee928/pyfmc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Finance Monte-Carlo Simulation using PyTorch

  • An easy-to-use python package to do Monte-Carlo Simulation on stock prices
  • GPU accelerated Monte-Carlo simulation, that could allow simulation more random walkers without a large time penalty

Installation

pip install pyfmc

Geometric Brownian Motion Simulation

Configure the simulation

import pandas as pd
import matplotlib.pyplot as plt
from pyfmc.simulations.gbm import GBM

data_path = "./tests/data/AAPL.csv" # Replace with one's desired data
simulation = GBM(
        df=pd.read_csv(data_path),
        n_walkers=500_000,
        n_steps=100,
        n_trajectories=50,
        open_index="Open",  # Make sure the DataFrame has column index specified here
        close_index="Close", # Make sure the DataFrame has column index specified here
    )
result = simulation.simulate()

Simulation Results

Price Distribution

price_dist = result.price_distribution()
price_dist.plot(bins=500)
plt.show()

Price Distribution

Return Distribution

return_dist = result.return_distribution()
return_dist.plot(kde=True)
plt.show()

Return Distribution

Walkers Trajectories

trajectories = result.trajectories()
trajectories.plot()
plt.show()

Trajectories

Value at Risk (VaR)

var = result.VaR(alpha=5)
# output: -0.2515...
# The worst 5% chance -> -25% return

For Development

Python virtual environment:

python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt

Reference