pyfmc


Namepyfmc JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/ethanlee928/pyfmc
SummaryFinance Monte-Carlo Simulation using PyTorch
upload_time2023-10-20 15:24:22
maintainer
docs_urlNone
authorEthan Lee
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install pyfmc
```

## Geometric Brownian Motion Simulation

### Configure the simulation

```python
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

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

![Price Distribution](./images/price_dist.png)

#### Return Distribution

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

![Return Distribution](./images/return_dist.png)

### Walkers Trajectories

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

![Trajectories](./images/trajectory.png)

### Value at Risk (VaR)

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

## For Development

Python virtual environment:

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

## Reference

- [How to Use Monte Carlo Simulation With GBM (Investopedia)](https://www.investopedia.com/articles/07/montecarlo.asp)
- [Understanding Value at Risk (VaR) and How It’s Computed (Investopedia)](https://www.investopedia.com/terms/v/var.asp)
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ethanlee928/pyfmc",
    "name": "pyfmc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ethan Lee",
    "author_email": "ethan2000.el@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/cd/e1/e578920884440773865edc0b43e8b1e5573f42b8bca39fca82a387d75437/pyfmc-0.1.4.tar.gz",
    "platform": null,
    "description": "# Finance Monte-Carlo Simulation using PyTorch\n\n- An easy-to-use python package to do Monte-Carlo Simulation on stock prices\n- GPU accelerated Monte-Carlo simulation, that could allow simulation more random walkers without a large time penalty\n\n## Installation\n\n```bash\npip install pyfmc\n```\n\n## Geometric Brownian Motion Simulation\n\n### Configure the simulation\n\n```python\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pyfmc.simulations.gbm import GBM\n\ndata_path = \"./tests/data/AAPL.csv\" # Replace with one's desired data\nsimulation = GBM(\n        df=pd.read_csv(data_path),\n        n_walkers=500_000,\n        n_steps=100,\n        n_trajectories=50,\n        open_index=\"Open\",  # Make sure the DataFrame has column index specified here\n        close_index=\"Close\", # Make sure the DataFrame has column index specified here\n    )\nresult = simulation.simulate()\n```\n\n### Simulation Results\n\n#### Price Distribution\n\n```python\nprice_dist = result.price_distribution()\nprice_dist.plot(bins=500)\nplt.show()\n```\n\n![Price Distribution](./images/price_dist.png)\n\n#### Return Distribution\n\n```python\nreturn_dist = result.return_distribution()\nreturn_dist.plot(kde=True)\nplt.show()\n```\n\n![Return Distribution](./images/return_dist.png)\n\n### Walkers Trajectories\n\n```python\ntrajectories = result.trajectories()\ntrajectories.plot()\nplt.show()\n```\n\n![Trajectories](./images/trajectory.png)\n\n### Value at Risk (VaR)\n\n```python\nvar = result.VaR(alpha=5)\n# output: -0.2515...\n# The worst 5% chance -> -25% return\n```\n\n## For Development\n\nPython virtual environment:\n\n```bash\npython3 -m venv .venv\nsource .venv/bin/activate\npip3 install -r requirements.txt\n```\n\n## Reference\n\n- [How to Use Monte Carlo Simulation With GBM (Investopedia)](https://www.investopedia.com/articles/07/montecarlo.asp)\n- [Understanding Value at Risk (VaR) and How It\u2019s Computed (Investopedia)](https://www.investopedia.com/terms/v/var.asp)",
    "bugtrack_url": null,
    "license": "",
    "summary": "Finance Monte-Carlo Simulation using PyTorch",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/ethanlee928/pyfmc"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cde1e578920884440773865edc0b43e8b1e5573f42b8bca39fca82a387d75437",
                "md5": "e60b84b1a0a2f11136658c3006fdc53f",
                "sha256": "ec858a40a90767ad14f6bf4b6d7db252cf85e131b5070a0af81d8d056bfe79cc"
            },
            "downloads": -1,
            "filename": "pyfmc-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "e60b84b1a0a2f11136658c3006fdc53f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4417,
            "upload_time": "2023-10-20T15:24:22",
            "upload_time_iso_8601": "2023-10-20T15:24:22.772255Z",
            "url": "https://files.pythonhosted.org/packages/cd/e1/e578920884440773865edc0b43e8b1e5573f42b8bca39fca82a387d75437/pyfmc-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-20 15:24:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ethanlee928",
    "github_project": "pyfmc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "pyfmc"
}
        
Elapsed time: 0.13185s