基于python的蒙特卡洛仿真
时间: 2023-05-22 15:01:20 浏览: 53
可以使用numpy库中的random模块实现蒙特卡洛仿真,具体实现方法可以参考以下代码:
import numpy as np
def monte_carlo_simulation():
# 设置仿真次数
num_simulations = 10000
# 设置随机数种子,保证每次运行结果一致
np.random.seed(0)
# 生成服从正态分布的随机数
mu = 0.1
sigma = 0.2
random_returns = np.random.normal(mu, sigma, num_simulations)
# 计算投资组合的收益率
weights = np.array([0.4, 0.6])
portfolio_returns = np.dot(random_returns.reshape(-1, 1), weights)
# 计算投资组合的收益率的均值和标准差
mean_return = np.mean(portfolio_returns)
std_return = np.std(portfolio_returns)
# 输出结果
print("投资组合的收益率均值为:{:.2%}".format(mean_return))
print("投资组合的收益率标准差为:{:.2%}".format(std_return))
请注意,以上代码仅为示例,实际应用中需要根据具体情况进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)