<reponame>oort77/nb_dev<filename>nb_dev/core.py
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).

__all__ = ['sharpe']

# Cell
### Main function
def sharpe(portfolio):
    data = p[portfolio]
    returns = data/data.shift(1)
    logReturns = np.log(returns)

    noOfPortfolios = 20000

    weight = np.zeros((noOfPortfolios, 5))
    expectedReturn = np.zeros(noOfPortfolios)
    expectedVolatility = np.zeros(noOfPortfolios)
    sharpeRatio = np.zeros(noOfPortfolios)

    meanLogRet = logReturns.mean()
    sigma = logReturns.cov()
    for k in range(noOfPortfolios):
        # Generate random weight vector
        w = np.array(np.random.random(5))
        w = w / np.sum(w)
        weight[k,:] = w
        # Expected log return
        expectedReturn[k] = np.sum(meanLogRet * w)
        # Expected volatility
        expectedVolatility[k] = np.sqrt(np.dot(w.T,np.dot(sigma,w)))
        # Sharpe Ratio
        sharpeRatio[k] = expectedReturn[k]/expectedVolatility[k]
    maxIndex = sharpeRatio.argmax()
    plt.figure(figsize=(16,8))
    plt.scatter(expectedVolatility, expectedReturn, c=sharpeRatio)
    plt.xlabel( 'Expected Volatility')
    plt.ylabel( 'Expected Log Returns')
    plt.suptitle(f'Markowitz Efficient Frontier\nPortfolio {portfolio}', fontsize=18)
    plt.colorbar(label='Sharpe Ratio')
    plt.scatter(expectedVolatility[maxIndex], expectedReturn[maxIndex], c='red')
    plt.show()
    return weight[maxIndex,:]
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).

__all__ = ['sharpe']

# Cell
### Main function
def sharpe(portfolio):
    data = p[portfolio]
    returns = data/data.shift(1)
    logReturns = np.log(returns)

    noOfPortfolios = 20000

    weight = np.zeros((noOfPortfolios, 5))
    expectedReturn = np.zeros(noOfPortfolios)
    expectedVolatility = np.zeros(noOfPortfolios)
    sharpeRatio = np.zeros(noOfPortfolios)

    meanLogRet = logReturns.mean()
    sigma = logReturns.cov()
    for k in range(noOfPortfolios):
        # Generate random weight vector
        w = np.array(np.random.random(5))
        w = w / np.sum(w)
        weight[k,:] = w
        # Expected log return
        expectedReturn[k] = np.sum(meanLogRet * w)
        # Expected volatility
        expectedVolatility[k] = np.sqrt(np.dot(w.T,np.dot(sigma,w)))
        # Sharpe Ratio
        sharpeRatio[k] = expectedReturn[k]/expectedVolatility[k]
    maxIndex = sharpeRatio.argmax()
    plt.figure(figsize=(16,8))
    plt.scatter(expectedVolatility, expectedReturn, c=sharpeRatio)
    plt.xlabel( 'Expected Volatility')
    plt.ylabel( 'Expected Log Returns')
    plt.suptitle(f'Markowitz Efficient Frontier\nPortfolio {portfolio}', fontsize=18)
    plt.colorbar(label='Sharpe Ratio')
    plt.scatter(expectedVolatility[maxIndex], expectedReturn[maxIndex], c='red')
    plt.show()
    return weight[maxIndex,:]
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified). # Cell ### Main function # Generate random weight vector # Expected log return # Expected volatility # Sharpe Ratio