程序代写代做代考 C MGMTMSA 403 Homework 2: Portfolio Optimization¶

MGMTMSA 403 Homework 2: Portfolio Optimization¶

Pre-processing step: Estimate expected returns and covariance¶
In [2]:
# Import gurobi and numpy
from gurobipy import *
import numpy as np
from numpy import genfromtxt
import csv

## Get index of 4 tickers
tick4 = [“MSFT”,”GS”,”PG”,”SCHP”];

# Get variable names
with open(‘Prices.csv’) as csvFile:
reader = csv.reader(csvFile)
tickers = next(reader) ## stores the tickets of all 390 stocks

tickind =[];
for t in tick4:
tickind.append(tickers.index(t)) ## collect index that corresponds to each ticker

# Load data
prices = genfromtxt(‘Prices.csv’, delimiter=’,’,skip_header = 1)

# get dimensions of data
d = prices.shape[0]
n = prices.shape[1]

# calculate monthly returns of each stock
returns = np.zeros((d-1,n))
for stock in range(n):
for month in range(d-1):
returns[month,stock] = prices[month+1,stock]/prices[month,stock]-1

# Store average return (parameter r_i in portfolio optimization model)
avg_return = np.zeros(n)
avg_return = np.mean(returns,axis=0)

# Store covariance matrix (parameter C_ij in portfolio optimization model)
C = np.zeros((n,n))
C = np.cov(np.transpose(returns))
In [ ]: