efficient frontier

Home   »   efficient frontier

# This function gives us a list with values between any given value inside a list:
def alpha(list):
  for i in range(1000):
      list.append(list[i] + 1/100)
  return list
# Let's calculate a list with values between -5 and 5:
alpha = alpha([-5])

# Let's use this function to generate the optimal solution from the two solutions calculated before:
def w3(weight1,weight2,alpha):
  weight3 = []
  for i in alpha:
    weight3.append(i*weight1 + (1-i)*weight2)
  return np.array(weight3)
# Now we have obtained the entire efficient frontier just from the two original optimal solutions:
weight3 = w3(weights1,weights2,alpha)

# Let's calculate the variance for each solution
def var_portfolio(weights,covar_matrix):
  empty_list = []
  for i in weights:
    # We use the variance of a portfolio formula:
    empty_list.append([email protected][email protected])
  return np.array(empty_list)

# Now we have a list with the variance for each solution for the entire efficient frontier
var_portafolio = var_portfolio(weight3,df_cov)

# Now we use this function for calculating the expected return of each portfolio

def exp_return(weights,retornos_esperados):
  empyt_list = []
  for i in weights:
    # We multiply each weight by it's expected return:
    empty_list.append([email protected]_esperados)
  return np.array(lista)

r = exp_return(weights3,returns)

# Plotting the efficient Frontier:

def scatterplot(df, x_dim, y_dim):
  x = np.sqrt(var_portafolio)
  y = r
  fig, ax = plt.subplots(figsize=(10, 5))
  ax.scatter(x, y, alpha=0.70)
  ax.set_title('Efficient Frontier')
  ax.grid(color='blue', linestyle='-', linewidth=0.25, alpha=0.5)
scatterplot(df, "Volatility", "Expected Return")

Leave a Reply

Your email address will not be published. Required fields are marked *