<p align="center">
<img src="https://github.com/jason-ash/pyesg/blob/master/docs/images/pyesg.png" width="450" />
</p>
<p align="center">
<em>Simulate stocks, interest rates, and other stochastic processes.<br><strong>pyesg</strong> is a lightning fast economic scenario generator for Python.</em>
</p>
<p align="center">
<a href="https://github.com/tterb/atomic-design-ui/blob/master/LICENSEs" target="_blank">
<img src="https://img.shields.io/apm/l/atomic-design-ui.svg?style=flat-square" alt="License"/>
</a>
<a href="https://pypi.python.org/pypi/pyesg/" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/pyesg.svg?style=flat-square" alt="Supported python versions"/>
</a>
<a href="https://badge.fury.io/py/pyesg" target="_blank">
<img src="https://img.shields.io/pypi/v/pyesg?color=light%20green&label=pypi%20package&style=flat-square" alt="Pypi package version"/>
</a>
<a href="https://github.com/jason-ash/pyesg/actions" target="_blank">
<img src="https://img.shields.io/github/workflow/status/jason-ash/pyesg/pyesg%20CI/master?style=flat-square" alt="Build status"/>
</a>
<a href="https://coveralls.io/github/jason-ash/pyesg?branch=master" target="_blank">
<img src="https://img.shields.io/coveralls/github/jason-ash/pyesg?style=flat-square" alt="Coverage"/>
</a>
</p>
---
### What is an economic scenario generator?
An economic scenario generator simulates the behavior of unpredictable financial markets like stocks, interest rates, or energy prices. It uses mathematical models called _stochastic processes_ to generate thousands of unique scenarios.
What can you do with an economic scenario generator? Here are a few examples:
<details>
<summary><strong>Simulate an investment portfolio</strong></summary>
* Generate 1,000 correlated, daily stock prices
* Construct a portfolio from the stocks
* Plot the value of the portfolio over time, and calculate metrics like high, low, max drawdown, etc.
</details>
<details>
<summary><strong>Choose a mortgage</strong></summary>
* Generate 1,000 30-year, monthly scenarios of mortgage rates
* Calculate the monthly payment for different loans under each scenario:
- 30-year fixed (lock in the rate today for all 30 years)
- 15-year fixed (lock in the rate today and pay over 15 years)
- 10-year adjustable-rate (fixed rate for 10 years, then variable rate for 20 years
- etc.
* Determine which loan fits the best with your budget
</details>
<details>
<summary><strong>Plan for retirement</strong></summary>
* Generate 1,000 30-year, monthly scenarios of stock market data
* Generate 1,000 30-year, monthly scenarios of bond fund data
* Track your portfolio balance as a mix of investments in stocks and bonds
* Each month, simulate a withdrawal from your portfolio and the growth from your investments
* Adjust the withdrawal up or down and identify the types of markets that lead to the best and worst outcomes
</details>
<details>
<summary><strong>Protect your business</strong></summary>
* **Banks**: test whether loan portfolios are resilient to drops in interest rates and higher-than-expected customer refinancing
* **Insurers**: test whether assets are sufficient to cover liabilities under a wide range of bond market scenarios
* **Investment managers**: test the impact of market declines on revenue from account-based fees
* **Small Businesses**: model customer demand correlated with the stock market to plan for potential downturns
</details>
## Key Features of **_pyesg_**
- Built on **numpy** for lightning fast scenario generation: 10,000+ scenarios in milliseconds
- Dozens of stochastic processes available out of the box
- Equity: Geometric Brownian Motion / Black Scholes, Heston
- Interest rate: Vasicek, Cox-Ingersoll-Ross, Hull-White
- Complete implementation of the American Academy of Actuaries <a href="https://www.actuary.org/content/economic-scenario-generators" target="_blank">stochastic interest rate generator</a> that is **200 times faster** than the Excel version
- Curve interpolation functions like Nelson-Siegel and Svensson models
- Hundreds of unit tests and excellent <a href="https://coveralls.io/github/jason-ash/pyesg?branch=master" target="_blank">code coverage</a>
- Open source software, free to use, copy, or modify under MIT license
## Coming soon
- More models!
- Fit stochastic models using historical data
- Built-in plotting methods
- Scenario summary reports
## Installing **_pyesg_**
**_pyesg_** is available on <a href="https://pypi.org/project/pyesg/" target="_blank">PyPI</a>, and can be installed with pip.
```
pip install pyesg
```
## Usage
All models in **_pyesg_** are created and used nearly identically.
First, create a model with its required parameters.
```python
import pyesg
# create a new model with the required parameters
model = pyesg.GeometricBrownianMotion(mu=0.05, sigma=0.2)
```
Generate scenarios by calling the `<model>.scenarios` method. The example below, with 10,000 daily scenarios (2,520,000 values) took just **160 milliseconds** to run!
<details>
<summary>Under the hood: <code>model.scenarios()</code></summary>
This method generates scenarios, given the following arguments:
- `x0` : the value or array of values each scenario starts with. For example, you might pass `100.0` to model a security with a current value of 100, or `[100.0, 90.0]` to model two scenarios with start values of 100 and 90.
- `dt` : the length of each time step in the projection, in years. For example, you might pass `1/12` to model mothly timesteps, `1/52` to model weekly timesteps, `1/252` to model daily (trading day) timesteps, or `1` to model annual timesteps.
- `n_scenarios` : the number of scenarios you want to generate. Often you can generate 10,000 scenarios in fractions of a second.
- `n_steps` : the number of timesteps for each scenario. In combination with `dt`, this argument determines the total length of time a projection covers. For example, with `dt=1/52` and `n_steps=104`, the projection will cover two years with weekly timesteps. With `dt=1/12` and `n_steps=360`, the projection will cover 30 years with monthly timesteps.
- `random_state` : an optional field that may be provided if you want to be able to reproduce pseudo-random numbers for each batch of scenarios. For example, `<model>.scenarios(..., random_state=123)` will generate the exact same batch of scenarios every time.
</details>
```python
import pyesg
# instantiate a new model with the required parameters
model = pyesg.GeometricBrownianMotion(mu=0.05, sigma=0.2)
# prepare the arguments to generate scenarios. Here we'll
# generate 10,000 scenarios with 252 daily (trading day)
# time steps, for a one-year projection in total.
x0 = 100.0 # the start value of our process
dt = 1/252 # the length of each timestep in years
n_scenarios = 10000 # the number of scenarios to generate
n_steps = 252 # the number of time steps per scenario
random_state = 123 # optional random_state for reproducibility
# now we generate the scenarios; this outputs a numpy array. It will
# have shape (10000, 253), which represents (scenarios, timesteps).
# There are 253 timesteps because the initial value is included to start
results = model.scenarios(x0, dt, n_scenarios, n_steps, random_state)
# array([[100. , 98.65207527, 97.12924873, ..., 111.3500094 ,
# 112.00479028, 113.12444153],
# [100. , 101.27637842, 100.8971646 , ..., 61.8709475 ,
# 63.00222064, 62.22126261],
# [100. , 100.37636067, 99.32267874, ..., 141.66969149,
# 140.38291993, 138.91659076],
# ...,
# [100. , 99.42484152, 97.68732205, ..., 139.9306172 ,
# 139.52301459, 139.05345463],
# [100. , 100.75304745, 102.09894601, ..., 115.66615197,
# 116.16385992, 118.06267759],
# [100. , 101.24269853, 101.73381851, ..., 8
没有合适的资源?快使用搜索试试~ 我知道了~
pyesg:Python的经济情景生成器
共44个文件
py:29个
csv:3个
yml:2个
需积分: 39 4 下载量 3 浏览量
2021-05-04
18:55:53
上传
评论 2
收藏 589KB ZIP 举报
温馨提示
模拟股票,利率和其他随机过程。 pyesg是Python的闪电般快速的经济场景生成器。 什么是经济情景生成器? 经济情景生成器模拟不可预测的金融市场的行为,例如股票,利率或能源价格。 它使用称为随机过程的数学模型来生成数千个独特的场景。 您可以使用经济情景生成器做什么? 这里有一些例子: 模拟投资组合 产生1,000个相关的每日股票价格 从股票构建投资组合 绘制一段时间内投资组合的价值,并计算高,低,最大跌幅等指标。 选择抵押 生成1,000个30年期每月抵押贷款情景 计算每种情况下不同贷款的每月还款额: 固定30年(今天锁定所有30年的汇率) 固定15年(今天锁定费率,并在15年内付款) 10年可调利率(固定利率10年,然后可变利率20年 等等。 确定哪种贷款最适合您的预算 退休计划 生成1,000个30年期每月股票市场数据场景 生成1,000个30年期,每月的债券基金数
资源详情
资源评论
资源推荐
收起资源包目录
pyesg-master.zip (44个子文件)
pyesg-master
setup.py 1KB
.gitignore 115B
requirements.txt 41B
MANIFEST.in 115B
.pre-commit-config.yaml 712B
LICENSE 1KB
.github
workflows
develop.yml 1014B
publish.yml 733B
README.md 11KB
tests
test_interpolators.py 3KB
__init__.py 0B
test_utils.py 1KB
test_datasets.py 2KB
test_doctest.py 1KB
test_processes.py 13KB
test_academy_rate_model.py 6KB
pyesg
academy_rate_model.py 12KB
datasets
us_stocks.csv 321KB
__init__.py 3KB
academy_sample_scenario.csv 35KB
ust_historical.csv 60KB
__init__.py 1KB
stochastic_process.py 10KB
interpolator.py 2KB
utils.py 3KB
interpolators
svensson.py 4KB
__init__.py 0B
nelson_siegel.py 3KB
version.py 45B
processes
cox_ingersoll_ross_process.py 2KB
black_derman_toy_process.py 1KB
ho_lee_process.py 1KB
wiener_process.py 4KB
__init__.py 0B
heston_process.py 4KB
academy_rate_process.py 12KB
ornstein_uhlenbeck_process.py 2KB
black_karasinski_process.py 1KB
geometric_brownian_motion.py 2KB
.pylintrc 308B
docs
images
pyesg.png 279KB
NelsonSiegel.png 155KB
logo.py 864B
mypy.ini 820B
共 44 条
- 1
jackie陈
- 粉丝: 12
- 资源: 4597
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0