1
我遇到了我的代碼問題。它只顯示一個圖形的數據,另一個圖形是空白的。我不明白爲什麼它不起作用。
我正在使用subplot()
函數,我的猜測是,原因可能是我的函數格式化的方式。圖只顯示1圖中的數據,但不顯示其他
import numpy as np
import matplotlib.pyplot as plt
import cvxopt as opt
from cvxopt import blas, solvers
import pandas as pd
import mpld3
from mpld3 import plugins
np.random.seed(123)
solvers.options['show_progress'] = False
n_assets = 4
n_obs = 1000 # original 1000
return_vec = np.random.randn(n_assets, n_obs)
def rand_weights(n):
k = np.random.rand(n)
return k/sum(k) print(rand_weights(n_assets)) print(rand_weights(n_assets))
def random_portfolio(returns):
p = np.asmatrix(np.mean(returns,axis=1))
w = np.asmatrix(rand_weights(returns.shape[0]))
C = np.asmatrix(np.cov(returns))
mu = w * p.T
sigma = np.sqrt(w * C * w.T)
#this recursion reduces outlier to keep the graph nice
if sigma > 2:
return random_portfolio(returns)
return mu, sigma
n_portfolios = 500
means, stds = np.column_stack([random_portfolio(return_vec) for _ in range(n_portfolios)])
plt.plot(return_vec.T, alpha=.4);
plt.xlabel('time')
plt.ylabel('returns')
plt.figure(1)
plt.subplot(212)
plt.plot(stds, means, 'o', markersize = 5)
plt.xlabel('std')
plt.ylabel('mean')
plt.title('Mean and standard deviation of returns of randomly generated portfolios')
plt.subplot(211)
plt.figure(1)
plt.show()
這就像一個魅力,我不能相信我錯過了。謝謝!! – Cesar
如果有效,您應該接受答案。 – bpachev