做写字楼的网站有哪些,大连网站建设选高合科技,wordpress导航下拉菜单,windows优化大师免费版大家好#xff0c;蒙特卡洛模拟是一种广泛应用于各个领域的计算技术#xff0c;它通过从概率分布中随机抽取大量样本#xff0c;并对结果进行统计分析#xff0c;从而模拟复杂系统的行为。这种技术具有很强的适用性#xff0c;在金融建模、工程设计、物理模拟、运筹优化以…大家好蒙特卡洛模拟是一种广泛应用于各个领域的计算技术它通过从概率分布中随机抽取大量样本并对结果进行统计分析从而模拟复杂系统的行为。这种技术具有很强的适用性在金融建模、工程设计、物理模拟、运筹优化以及风险管理等领域都有广泛的应用。
蒙特卡洛模拟这个名称源自于摩纳哥王国的蒙特卡洛城市这里曾经是世界著名的赌博天堂。在20世纪40年代著名科学家乌拉姆和冯·诺依曼参与了曼哈顿计划他们需要解决与核反应堆中子行为相关的复杂数学问题。他们受到了赌场中掷骰子的启发设想用随机数来模拟中子在反应堆中的扩散过程并将这种基于随机抽样的计算方法命名为蒙特卡洛模拟Monte Carlo simulation。
蒙特卡洛模拟的核心思想是通过大量重复随机试验从而近似求解分析解难以获得的复杂问题。它克服了传统数值计算方法的局限性能够处理非线性、高维、随机等复杂情况。随着计算机性能的飞速发展蒙特卡洛模拟的应用范围也在不断扩展。
在金融领域蒙特卡洛模拟被广泛用于定价衍生品、管理投资组合风险、预测市场波动等。在工程设计中它可以模拟材料力学性能、流体动力学等复杂物理过程。在物理学研究中从粒子物理到天体物理都可以借助蒙特卡洛模拟进行探索。此外蒙特卡洛模拟还在机器学习、计算生物学、运筹优化等领域发挥着重要作用。
蒙特卡洛模拟的过程基本上是这样的首先需要定义要模拟的系统或过程包括方程和参数其次根据拟合的概率分布生成随机样本进而针对每一组随机样本运行模型模拟系统的行为最后分析结果以了解系统行为。
本文将介绍使用它来模拟未来证券价格的两种分布高斯分布和学生 t 分布。这两种分布通常被量化分析人员用于证券市场数据。
在此加载苹果公司从2020年到2024年每日证券价格的数据
import yfinance as yf
orig yf.download([AAPL], start2020-01-01, end2024-12-31)
orig orig[(Adj Close)]
orig.tail()[*********************100%%**********************] 1 of 1 completed
Date
2024-03-08 170.729996
2024-03-11 172.750000
2024-03-12 173.229996
2024-03-13 171.130005
2024-03-14 173.000000
Name: Adj Close, dtype: float64可以通过价格序列来计算简单的日收益率并将其呈现为柱状图。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
returns orig.pct_change()
last_price orig[-1]
returns.hist(bins100) 苹果证券日收益柱状图
1.标准正态分布拟合收益率
证券的历史波动率通常是通过计算每日收益率的标准差来进行假设未来的波动率与历史波动率相似。而直方图则呈现了以0.0为中心的正态分布的形状。为简单起见将该分布假定为均值为0标准差为0的高斯分布。接下来计算出标准差也称为日波动率预计明天的日收益率将会是高斯分布中的一个随机值。
daily_volatility returns.std()
rtn np.random.normal(0, daily_volatility)第二天的价格是今天的价格乘以 (1return %)
price last_price * (1 rtn)以上是证券价格和收益的基本财务公式。使用蒙特卡洛模拟预测明天的价格可以随机抽取另一个收益率从而推算后天的价格可以得出未来 200 天可能的价格走势之一。当然这只是一种可能的价格路径。重复这个过程得出另一条价格路径重复过程 1,000 次得出 1,000 条价格路径。
import warnings
warnings.simplefilter(actionignore, categorypd.errors.PerformanceWarning)num_simulations 1000
num_days 200
simulation_df pd.DataFrame()
for x in range(num_simulations):count 0 # The first price pointprice_series []rtn np.random.normal(0, daily_volatility)price last_price * (1 rtn)price_series.append(price)# Create each price pathfor g in range(num_days):rtn np.random.normal(0, daily_volatility)price price_series[g] * (1 rtn)price_series.append(price)# Save all the possible price pathssimulation_df[x] price_series
fig plt.figure()
plt.plot(simulation_df)
plt.xlabel(Number of days)
plt.ylabel(Possible prices)
plt.axhline(y last_price, color b, linestyle -)
plt.show()分析结果如下价格起始于179.66美元大部分价格路径相互交叠模拟价格范围为100美元至500美元。 使用高斯分布的蒙特卡洛模拟
假设我们想知道90%情况下5%到95%出现的正常价格范围可以使用量化方法得到上限和下限从而评估超出这些极端价格。
upper simulation_df.quantile(.95, axis1)
lower simulation_df.quantile(.05, axis1)
stock_range pd.concat([upper, lower], axis1)fig plt.figure()
plt.plot(stock_range)
plt.xlabel(Number of days)
plt.ylabel(Possible prices)
plt.axhline(y last_price, color b, linestyle -)
plt.show()使用高斯分布的 95 百分位数和 5 百分位数
2.学生t分布拟合收益率
证券价格回报偶尔会出现极端事件位于分布两端。标准正态分布预计 95% 的收益率发生在两个标准差之内5% 的收益率发生在两个标准差之外。如果极端事件发生的频率超过 5%分布看起来就会 变胖。这就是统计学家所说的肥尾定量分析人员通常使用学生 t 分布来模拟证券收益率。
学生 t 分布有三个参数自由度参数、标度和位置。 自由度自由度参数表示用于估计群体参数的样本中独立观测值的数量。自由度越大t 分布的形状越接近标准正态分布。在 t 分布中自由度范围是大于 0 的任何正实数。 标度标度参数代表分布的扩散性或变异性通常是采样群体的标准差。 位置位置参数表示分布的位置或中心即采样群体的平均值。当自由度较小时t 分布的尾部较重类似于胖尾分布。
用学生 t 分布来拟合实际证券收益率
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import treturns orig.pct_change()# Number of samples per simulation
num_samples 100# distribution fitting
returns returns[1::] # Drop the first element, which is NA
params t.fit(returns[1::]) # fit with a student-t# Generate random numbers from Students t-distribution
results t.rvs(dfparams[0], locparams[1], scaleparams[2], size1000)
# Generate random numbers from Students t-distribution
results t.rvs(dfparams[0], locparams[1], scaleparams[2], size1000)
print(degree of freedom , params[0])
print(loc , params[1])
print(scale , params[2])参数如下 自由度 3.735 位置 0.001 标度 0.014
使用这些参数来预测 Student-t 分布然后用 Student-t 分布绘制实际证券收益分布图。
returns.hist(bins100,densityTrue, alpha0.6, colorb, labelActual returns distribution)# Plot histogram of results
plt.hist(results, bins100, densityTrue, alpha0.6, colorg, labelSimulated Student/t distribution)plt.xlabel(Value)
plt.ylabel(Density)
plt.title(Actual returns vs. Projections with a Student\s t-distribution)
plt.legend(loccenter left)
plt.grid(True)
plt.show()实际回报与预测相当接近 实际收益与学生 t 分布预测对比
与之前一样模拟未来 200 天的价格走势。
import warnings
warnings.simplefilter(actionignore, categorypd.errors.PerformanceWarning)num_simulations 1000
num_days 200
simulation_student_t pd.DataFrame()
for x in range(num_simulations):count 0# The first price pointprice_series []rtn t.rvs(dfparams[0], locparams[1], scaleparams[2], size1)[0]price last_price * (1 rtn)price_series.append(price)# Create each price pathfor g in range(num_days):rtn t.rvs(dfparams[0], locparams[1], scaleparams[2], size1)[0]price price_series[g] * (1 rtn)price_series.append(price)# Save all the possible price pathssimulation_student_t[x] price_series
fig plt.figure()
plt.plot(simulation_student_t)
plt.xlabel(Number of days)
plt.ylabel(Possible prices)
plt.axhline(y last_price, color b, linestyle -)
plt.show()学生 t 分布的蒙特卡洛模拟
可以绘制出学生 t 的蒙特卡洛模拟置信区间上下限95%、5%
upper simulation_student_t.quantile(.95, axis1)
lower simulation_student_t.quantile(.05, axis1)
stock_range pd.concat([upper, lower], axis1)fig plt.figure()
plt.plot(stock_range)
plt.xlabel(Number of days)
plt.ylabel(Possible prices)
plt.title(The upper 95% and lower 5%)
plt.axhline(y last_price, color b, linestyle -)
plt.show()使用学生 t 分布的 95 百分位数和 5 百分位数