Sofi1606 commited on
Commit
76ff76e
1 Parent(s): 829f6d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -64,7 +64,7 @@ if uploaded_file is not None:
64
 
65
  st.write(df[["Date"] + stocks_rets])
66
 
67
- fig = px.line(df, x=df.Date, y=stocks, labels={'value': 'Value', 'variable': 'Series'}, title='Time Series Plot')
68
  fig.update_layout(xaxis_title='Date', yaxis_title='Value')
69
 
70
  st.plotly_chart(fig)
@@ -82,10 +82,10 @@ if uploaded_file is not None:
82
 
83
  if optim_choice == "max returns":
84
  def objective(trial):
85
- w1 = trial.suggest_uniform('w1', 0, 1)
86
- w2 = trial.suggest_uniform('w2', 0, 1)
87
- w3 = 1 - w1 - w2
88
- weights = np.array([w1, w2, w3]).reshape(-1, 1)
89
  return np.dot(weights.T, ret_list)
90
 
91
  study = optuna.create_study(direction="maximize")
@@ -93,10 +93,10 @@ if uploaded_file is not None:
93
 
94
  elif optim_choice == "min variance":
95
  def objective(trial):
96
- w1 = trial.suggest_uniform('w1', 0, 1)
97
- w2 = trial.suggest_uniform('w2', 0, 1)
98
- w3 = 1 - w1 - w2
99
- weights = np.array([w1, w2, w3]).reshape(-1, 1)
100
  return np.dot(weights.T, np.dot(cov_matrix, weights))
101
 
102
  study = optuna.create_study(direction="minimize")
@@ -104,24 +104,24 @@ if uploaded_file is not None:
104
 
105
  else:
106
  def objective(trial):
107
- w1 = trial.suggest_uniform('w1', 0, 1)
108
- w2 = trial.suggest_uniform('w2', 0, 1)
109
- w3 = 1 - w1 - w2
110
  weights = np.array([w1, w2, w3]).reshape(-1, 1)
111
  return np.dot(weights.T, ret_list) - np.dot(weights.T, np.dot(cov_matrix, weights))
112
 
113
  study = optuna.create_study(direction="maximize")
114
  study.optimize(objective, n_trials=100, show_progress_bar=True)
115
 
116
- w1 = study.best_params['w1']
117
- w2 = study.best_params['w2']
118
- w3 = 1- w1 - w2
119
 
120
- weights = np.array([w1, w2, w3]).reshape(-1, 1)
121
 
122
  yearly_returns = (1 + np.dot(weights.T, ret_list)[0, 0]) ** 252 - 1
123
  yearly_variance = np.dot(weights.T, np.dot(cov_matrix, weights))[0, 0] * 252
124
 
125
- st.write(f"Los pesos son: :green[{stocks[0]} -> {w1:,.4f}], :green[{stocks[1]} -> {w2:,.4f}], :green[{stocks[2]} -> {w3:,.4f}]")
126
  st.write(f"El retorno anualizado del portafolio es: :green[{yearly_returns:,.4f}]")
127
  st.write(f"La varianza anualizado del portafolio es: :green[{yearly_variance:,.4f}]")
 
64
 
65
  st.write(df[["Date"] + stocks_rets])
66
 
67
+ fig = px.line(df, x=df.Date, y=stocks, labels={'value': 'Value', 'variable': 'Series'}, title='Serie de tiempo')
68
  fig.update_layout(xaxis_title='Date', yaxis_title='Value')
69
 
70
  st.plotly_chart(fig)
 
82
 
83
  if optim_choice == "max returns":
84
  def objective(trial):
85
+ a1 = trial.suggest_uniform('a1', 0, 1)
86
+ a2 = trial.suggest_uniform('a2', 0, 1)
87
+ a3 = 1 - a1 - a2
88
+ weights = np.array([a1, a2, a3]).reshape(-1, 1)
89
  return np.dot(weights.T, ret_list)
90
 
91
  study = optuna.create_study(direction="maximize")
 
93
 
94
  elif optim_choice == "min variance":
95
  def objective(trial):
96
+ a1 = trial.suggest_uniform('a1', 0, 1)
97
+ a2 = trial.suggest_uniform('a2', 0, 1)
98
+ a3 = 1 - a1 - a2
99
+ weights = np.array([a1, a2, a3]).reshape(-1, 1)
100
  return np.dot(weights.T, np.dot(cov_matrix, weights))
101
 
102
  study = optuna.create_study(direction="minimize")
 
104
 
105
  else:
106
  def objective(trial):
107
+ a1 = trial.suggest_uniform('a1', 0, 1)
108
+ a2 = trial.suggest_uniform('a2', 0, 1)
109
+ a3 = 1 - a1 - a2
110
  weights = np.array([w1, w2, w3]).reshape(-1, 1)
111
  return np.dot(weights.T, ret_list) - np.dot(weights.T, np.dot(cov_matrix, weights))
112
 
113
  study = optuna.create_study(direction="maximize")
114
  study.optimize(objective, n_trials=100, show_progress_bar=True)
115
 
116
+ a1 = study.best_params['a1']
117
+ a2 = study.best_params['a2']
118
+ a3 = 1- a1 - a2
119
 
120
+ weights = np.array([a1, a2, a3]).reshape(-1, 1)
121
 
122
  yearly_returns = (1 + np.dot(weights.T, ret_list)[0, 0]) ** 252 - 1
123
  yearly_variance = np.dot(weights.T, np.dot(cov_matrix, weights))[0, 0] * 252
124
 
125
+ st.write(f"Los pesos son: :green[{stocks[0]} -> {a1:,.4f}], :green[{stocks[1]} -> {a2:,.4f}], :green[{stocks[2]} -> {a3:,.4f}]")
126
  st.write(f"El retorno anualizado del portafolio es: :green[{yearly_returns:,.4f}]")
127
  st.write(f"La varianza anualizado del portafolio es: :green[{yearly_variance:,.4f}]")