import streamlit as st import numpy as np import pandas as pd import matplotlib.pyplot as plt import random # Streamlit diseño de app st.title("Estimación de estadísticas a partir de dos listas con valores históricos") with st.form("my_form"): N = st.number_input("Cuantos ekementos quieres de cada lista?", step=1) # Para generar los números if st.form_submit_button("Click para generar lista de números simultanea"): # Genera dos listas con 100 números cada una x = [random.randint(0, 20) for _ in range(N)] y = [random.randint(0, 20) for _ in range(N)] # Muestra las listas en app # st.write('Lista 1:', x) # st.write('Lista 2:', y) if "x" in globals(): x_bar = np.mean(x) y_bar = np.mean(y) st.subheader("Valores esperados") st.write(f"E(x) = {x_bar:,.2f}") st.write(f"E(y) = {y_bar:,.2f}") var_x = np.var(x) var_y = np.var(y) st.subheader("Varianzas") st.write(f"var(x) = {var_x:,.2f}") st.write(f"var(y) = {var_y:,.2f}") cov_xy = np.corrcoef(x, y)[0, 1] st.subheader("Correlación") st.write(f"corr(x, y) = {cov_xy:,.2f}") plt.scatter(x, y) plt.title("Correlaciones") plt.xlabel("x") plt.ylabel("y") st.pyplot(plt) plt.close() plt.title("Plot de los retornos") plt.plot(np.arange(len(x)), x) plt.plot(np.arange(len(y)), y) plt.ylim(-10, 40) plt.xlabel('"tiempo"') plt.ylabel('"retornos"') st.pyplot(plt) w_x = (x_bar - y_bar + var_y - cov_xy * np.sqrt(var_x * var_y)) / ( var_x + var_y - 2 * cov_xy * np.sqrt(var_x * var_y) ) w_y = 1 - w_x var_r = (w_x**2)*var_x + (w_y**2)*var_y + 2*w_x*w_y*cov_xy*np.sqrt(var_x * var_y) st.subheader(f"Weights and portfolio returns (this is Plus, but you will need to explain how you obtained the answers)") st.write("Assuming x and y represent returns of portfolios") st.write(f"w_x = {w_x:,.2f}") st.write(f"w_y = {w_y:,.2f}") st.write(f"E(r) = {w_x*x_bar + w_y*y_bar:,.2f}") st.write(f"var(r) = {var_r:,.2f}") else: st.write(":red[Please, give the number of elements in each list]")