LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Create a loop over the optimize function

‘’‘Hey all, I am trying to create a for loop that gives the monthly mean return for a portfolio over a 15 year period. There is supposed to be a 1 year holding period and 1 year formation period i.e we optimize over 2005 to invest in 2006 (where we generate the monthly returns). The for loop works for the first year, but breaks when when implemented over the 15 year period. ‘’’

X = 15 # years
for i in range(1,2):
data_select = aac.iloc[i*12:24+(12*i)]
test = data_select.dropna(axis=‘columns’)

test1= test.iloc[i*12:(i+1)*12]
test2 = ffmom_df.iloc[i*12:(i+1)*12]
rf = test2.rf.mean()

def port_ret(weights,ret_series=test1):
    return np.sum(weights*np.mean(ret_series,axis=0))

def port_sd(weights,ret_series=test1):
    wmat = np.outer(weights,weights)
    cvar = np.cov(np.transpose(ret_series),ddof=0) * wmat
    pvar = np.sum(cvar)
    return np.sqrt(pvar)

def sharpe_neg(weights,ret_series=test1):
    return -(port_ret(weights,ret_series)-rf)/port_sd(weights,ret_series)

def sharpe(weights,ret_series=test1):
    return (port_ret(weights,ret_series)-rf)/port_sd(weights,ret_series)

# Constraints
def cw1(w):
    return np.sum(w) - 1

cons = ({'type':'eq', 'fun': cw1})

Setup a tuple of tuples with the bounds for asset weights:

bnds = ((0, 1),)*(len(test.columns))

Optimize!

starting_values = 5
out = scipy.optimize.minimize(sharpe_neg,5,constraints=cons,bounds=bnds)

optw = []
for j in range(len(out.x)):
    if out.x[j] >= 0.0001:
        optw.append(out.x[j])
    else:
        optw.append(0)
    
def opt_returns(weights,ret_series=test1):
    return np.sum(weights*np.array(ret_series),axis=1)

result = []
result.append(opt_returns(optw))`Preformatted text`