猿问

高效前沿不好看

我正在尝试绘制一个有效的边界。下面是我用的。回报参数由投资组合的 9 列回报组成。我选择了 10,000 个投资组合,这就是我的有效边界的样子。这不是我们熟悉的通常的边界形状。


有人可以向我解释这个问题。


def monteCarlo_Simulation(returns):


    #returns=returns.drop("Date")

    returns=returns/100

    stocks=list(returns)

    stocks1=list(returns)

    stocks1.insert(0,"ret")

    stocks1.insert(1,"stdev")

    stocks1.insert(2,"sharpe")

    print (stocks)

    #calculate mean daily return and covariance of daily returns

    mean_daily_returns = returns.mean()

    #print (mean_daily_returns)

    cov_matrix = returns.cov()


    #set number of runs of random portfolio weights

    num_portfolios = 10000



    #set up array to hold results

    #We have increased the size of the array to hold the weight values for each stock

    results = np.zeros((4+len(stocks)-1,num_portfolios))


    for i in range(num_portfolios):

        #select random weights for portfolio holdings

        weights = np.array(np.random.random(len(stocks)))

        #rebalance weights to sum to 1

        weights /= np.sum(weights)


        #calculate portfolio return and volatility

        portfolio_return = np.sum(mean_daily_returns * weights) * 252

        portfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights))) * np.sqrt(252)


        #store results in results array

        results[0,i] = portfolio_return

        results[1,i] = portfolio_std_dev

        #store Sharpe Ratio (return / volatility) - risk free rate element excluded for simplicity

        results[2,i] = results[0,i] / results[1,i]

        #iterate through the weight vector and add data to results array

        for j in range(len(weights)):

            results[j+3,i] = weights[j]

http://img4.mukewang.com/619350280001351607000258.jpg

明月笑刀无情
浏览 206回答 2
2回答

开心每一天1111

很可能不是图形或代码搞砸了 - 而是您的输入。尝试使用资产。可能是您包含在优化算法中的资产高度正相关 - 导致多样化的影响可以忽略不计。反过来,这会影响您的有效边界的形状。编辑:如果这不是问题的根源。也许使用以下代码行重试该程序:def monteCarlo_Simulation(returns):    noa = len(tickers)    random_returns = []    random_volatility = []    for i in range (10000):        weights = np.random.random(noa)        weights = weights / np.sum(weights)        random_returns.append(np.sum(returns.mean()*weights)*252)        random_volatility.append(np.sqrt(np.dot(weights.T, np.dot(returns.cov()*252, weights))))    random_returns = np.array(random_returns)    random_volatility = np.array(random_volatility)    fig_random = plt.figure(figsize = [6,4])    plt.scatter(random_volatility, random_returns,                c= random_returns / random_volatility, marker = '.')    plt.grid(True)    plt.xlabel('Expected volatility')    plt.ylabel('Expected return')    plt.colorbar(label='Sharpe ratio')    plt.title('Mean Variance Analysis Plot')    plt.show()

杨__羊羊

我遇到了类似的问题,我通过查看 NaN 值解决了这个问题,有些公司的 IPO 很晚,有些是在同一年。因此,您需要收集与您的股票投资组合中最新 IPO 相同的数据。或者剔除在您检索数据之日之前未 IPO 的所有股票。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答