Multiperiod_min_variance

Here is what I want to ask,my aim is multiperiod min variance portfolio,with the for loop I want to construct rolling window
After I learned @fdabrandao ,thank a lot for helping me,
dfs =
for i, slice_df in enumerate(slices):
df = risk_models.risk_matrix(slice_df, method=“sample_cov”).stack().to_frame()
df.reset_index(inplace=True) # Turn the index into regular data columns
df.columns = [“Stock1”, “Stock2”, “S”] # Adjust column names
df[“Time”] = i # Add new column with the index of the slice
dfs.append(df)
sigma = pd.concat(dfs) # Concatenate all dataframes
sigma.set_index([“Time”, “Stock1”, “Stock2”], inplace=True)#
I can define sigma and add the model,then I define mu=expected return with same way but another thing risk aversion comes from machine learning model,it must be defined as picture with t,how I can?Also here df[“Time”] = i andafter AMPL for consistency ampl.set[“Time”]=range(n_slices) equal to 3,I tried small sample 33 data , But I got many weights plot?Why? I adjust my tau=window for rolling and n_slices with respect to data points,they give same slicing,is there any way more better coding this thing?

Here is my whole code:
data=final_data
tau=11#window size
out_of_sample_results_list =
from pypfopt import expected_returns, risk_models
for t in range(tau, len(data)):
in_sample_data = data[‘^GSPC’][t - tau : t]
#print(in_sample_data)
out_of_sample_data =data[‘^GSPC’][t : t + 1]

#print(out_of_sample_data)
selected_TA=['SMA_3','SMA_5','SMA_10','WMA_3','WMA_5','WMA_10','EMA_3','EMA_5','EMA_10','ADX_3','ADX_5','ADX_10','RSI_3','RSI_5','RSI_10','CCI_20','EMV_3','EMV_5','EMV_10','BBands_3','BBands_5','BBands_10','SAR','MFI_3','MFI_5','MFI_10','VWAP_3','VWAP_5','VWAP_10']
#out_of_sample_data_stocks
# Compute technical indicators
X_train = data[selected_TA][t - tau : t]#train

# Prepare features and labels for machine learning models

y_train_ = np.where(in_sample_data.shift(-1) > in_sample_data,1,0)#buradasadece market data lazim#y bir array  # Labels (assuming 'MarketMovement' is a binary variable)
y_train=pd.DataFrame(y_train_)
X_test =data[selected_TA][t : t + 1]
y_test=np.where(out_of_sample_data.shift(-1) > out_of_sample_data,1,0)
# Train machine learning models (LR, XGBoost)
lr_model = train_lr_model(X_train, y_train)#buraya TA ler NA verdigi zaman onu verince direkt error verdi

# Make predictions on the test set and evaluate model performance
lr_predictions = lr_model.predict_proba(X_test)[:, 1]
#print(lr_predictions)
#OPTIMIZATION MIN VAR
# Specify the initial capital, the risk aversion, and the guaranteed return rate.
risk_aversion=lr_predictions

risk_aversion
# Specify the number of asse
# Specify the number of assets, their expected return, and their covariance matrix.
stocks = [
‘HD’, ‘MCD’, ‘NKE’, ‘KO’, ‘PG’, ‘SYY’, ‘WMT’, # Consumer Staples
‘CVX’, ‘XOM’, # Energy
‘AXP’, ‘JPM’, # Financials
‘JNJ’, ‘MRK’, ‘PFE’, ‘WBA’, # Health Care
‘BA’, ‘CAT’, ‘MMM’, # Industrials
‘IRX’,# Information Technology#t-bill
]
stocks_in_sample = data[stocks][t - tau : t]
stocks_out_sample=data[stocks][t : t + 1]
mu_out=calculate_returns(stocks_out_sample)
n_slices = 3#3 tane 11 li data
slices = np.array_split(data[stocks], n_slices)
muu_ =
for i, slice_df in enumerate(slices):
for_mu=expected_returns.mean_historical_return(slice_df[stocks]).to_frame()
for_mu.reset_index(inplace=True)
for_mu.columns = [‘Asset’,“Return”]#for_mu.columns = [‘Asset’, ‘Return’] bunda asset nameleri cikiyor
for_mu[“Time”] = i # Add new column with the index of the slice
muu_.append(for_mu)#burada her time step=3 icin expected returnu dondurdum
mu=pd.concat(muu_)
mu.set_index([“Time”, “Asset”], inplace=True)
#for risk free
rf_ =
for i, slice_df in enumerate(slices):
for_rf=expected_returns.mean_historical_return(slice_df[‘IRX’]).to_frame()
for_rf.reset_index(inplace=True)
for_rf.columns = [‘Asset’,“Return”]#for_mu.columns = [‘Asset’, ‘Return’] bunda asset nameleri cikiyor
for_rf[“Time”] = i # Add new column with the index of the slice
rf_.append(for_rf)#burada her time step=3 icin expected returnu dondurdum
rf=pd.concat(rf_)
rf.set_index([“Time”, “Asset”], inplace=True)

dfs = []
for i, slice_df in enumerate(slices):
    df = risk_models.risk_matrix(slice_df, method="sample_cov").stack().to_frame()
    df.reset_index(inplace=True) # Turn the index into regular data columns
    df.columns = ["Stock1", "Stock2", "S"] # Adjust column names
    df["Time"] = i  # Add new column with the index of the slice
    dfs.append(df)
sigma = pd.concat(dfs)  # Concatenate all dataframes
sigma.set_index(["Time", "Stock1", "Stock2"], inplace=True)# Set the index to be (Time, Stock1, Stock2)
ampl = AMPL()
ampl.eval(
    r"""
   set Time;
   set A;
   param Sigma{t in Time, A, A};
   param mu{Time,A};
   param lb default 0;
   param ub default 1;
   var w{Time,A} >= lb <= ub;
   minimize average_portfolio_variance:
    (sum {t in Time} sum {i in A, j in A} w[t,i] * Sigma[t, i, j] * w[t,j]);
    s.t. portfolio_weights:
    sum {t in Time} sum{i in A} w[t,i] = 1;

    """
)
ampl.set["A"] = stocks
ampl.set["Time"]=range(n_slices)
ampl.param["Sigma"] = sigma
#ampl.param["u"] = {t: 0 for t in stocks} # just a placeholder, u needs to be calculated
ampl.param["mu"]=mu
ampl.option["solver"] = "gurobi"
ampl.solve()
#print("optimal variance:", ampl.get_value("portfolio_variance"))
Var_min=ampl.get_value("sum {t in Time}sum {i in A, j in A} w[t,i] * Sigma[t,i, j] * w[t,j]")
R_min = ampl.get_value("sum {t in Time}sum {i in A} mu[t,i] * w[t,i]")
print("min variance:", ampl.get_value("sum {t in Time}sum {i in A, j in A} w[t,i] * Sigma[t,i, j] * w[t,j]"))
print("min return:", ampl.get_value("sum {t in Time}sum {i in A} mu[t,i] * w[t,i]"))

#ampl.get_data("w").to_pandas().plot.barh()
max_ = AMPL()#R_max ve Var_max
max_.eval(
 r"""
    
   set Time;
   set A;
   param Sigma{t in Time, A, A};
   param mu{Time,A};
   param lb default 0;
   param ub default 1;
   var w{Time,A} >= lb <= ub;
   maximize average_portfolio_variance:
    (sum {t in Time} sum {i in A, j in A} w[t,i] * Sigma[t, i, j] * w[t,j]);
    s.t. portfolio_weights:
    sum {t in Time} sum{i in A} w[t,i] = 1;   

 

"""

)
max_.set[“A”] = stocks
max_.set[“Time”]=range(n_slices)
max_.param[“Sigma”] = sigma
#ampl.param[“u”] = {t: 0 for t in stocks} # just a placeholder, u needs to be calculated
max_.param[“mu”]=mu

max_.option["solver"] = "gurobi"
max_.solve()
#print("optimal variance:", max_.get_value("portfolio_variance"))
Var_max=max_.get_value("sum {t in Time}sum {i in A, j in A} w[t,i] * Sigma[t,i, j] * w[t,j]")
R_max = max_.get_value("sum {t in Time}sum {i in A} mu[t,i] * w[t,i]")
print("max variance:", Var_max)
print("max return:", R_max)
#max_.get_data("w").to_pandas().plot.barh()
#min_var 

min_var = AMPL()
min_var.eval(
 r"""
        
 set Time;
 set A ordered;
 
 param risk_aversion;
 param risk_free{Time,A};
 param Sigma{Time,A, A};
 param mu{Time,A};
 param R_min;
 param R_max;
 param Var_min;
 param Var_max;
 param lb default 0;
 param ub default 1;
 var w{Time,A} >= lb <= ub;
        
 maximize risk_adjusted_return {t in Time}:
     risk_aversion*((sum {i in A} mu[t,i] * w[t,i]- R_min)/(R_max-R_min))+
     (1-risk_aversion)*((sum {i in A, j in A} w[t,i] * Sigma[t,i, j] * w[t,j]- Var_min)/(Var_max - Var_min));
 s.t. portfolio_weights:
 sum {t in Time}sum {i in A} w[t,i] = 1;  

 

"""

)

min_var.set["A"] = stocks
min_var.set["Time"]=range(n_slices)
min_var.param["mu"] = mu
min_var.param["risk_aversion"] = risk_aversion
min_var.param["Sigma"] = sigma
#min_var.param["iteration"] = t-tau # iteration number
min_var.param["R_min"]=R_min
min_var.param["R_max"]=R_max
min_var.param["Var_min"]=Var_min
min_var.param["Var_max"]=Var_max
min_var.option["solver"] = "gurobi"
min_var.solve()
min_var.get_data("w").to_pandas().plot.barh()