CoolFace
Apppublic

AdolfoCrz/Correlation

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py77 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3import pandas as pd4import matplotlib.pyplot as plt5import random6 7# Configuración de la página8st.set_page_config(page_title="Correlation Analysis", page_icon="📊")9 10# Título de la aplicación11st.title("Statistical Analysis of Randomly Generated Lists 📊")12 13# Formulario de entrada del usuario14with st.form("input_form"):15    num_elements = st.number_input("Enter the number of elements for each list:", min_value=1, step=1)16 17    # Botón para generar las listas de números aleatorios18    generate_btn = st.form_submit_button("Generate Random Lists")19 20if generate_btn:21    # Generar dos listas de números aleatorios22    list_x = [random.randint(0, 20) for _ in range(num_elements)]23    list_y = [random.randint(0, 20) for _ in range(num_elements)]24 25    # Calcular estadísticas básicas26    mean_x = np.mean(list_x)27    mean_y = np.mean(list_y)28    variance_x = np.var(list_x)29    variance_y = np.var(list_y)30    correlation_xy = np.corrcoef(list_x, list_y)[0, 1]31 32    # Mostrar estadísticas calculadas33    st.subheader("Calculated Statistics")34    st.write(f"Mean of list X (E(x)): {mean_x:.2f}")35    st.write(f"Mean of list Y (E(y)): {mean_y:.2f}")36    st.write(f"Variance of list X (Var(x)): {variance_x:.2f}")37    st.write(f"Variance of list Y (Var(y)): {variance_y:.2f}")38    st.write(f"Correlation between X and Y (Corr(x, y)): {correlation_xy:.2f}")39 40    # Visualización de los datos41    fig, ax = plt.subplots()42    ax.scatter(list_x, list_y, color='blue', alpha=0.6)43    ax.set_title("Scatter Plot of X and Y")44    ax.set_xlabel("X values")45    ax.set_ylabel("Y values")46    st.pyplot(fig)47 48    # Gráfico de las listas49    fig, ax = plt.subplots()50    ax.plot(np.arange(num_elements), list_x, label="List X", marker='o')51    ax.plot(np.arange(num_elements), list_y, label="List Y", marker='s')52    ax.set_ylim(-10, 40)53    ax.set_title("Line Plot of X and Y")54    ax.set_xlabel("Index")55    ax.set_ylabel("Values")56    ax.legend()57    st.pyplot(fig)58 59    # Cálculo de los pesos de la cartera y la varianza de la cartera60    weight_x = (mean_x - mean_y + variance_y - correlation_xy * np.sqrt(variance_x * variance_y)) / (61        variance_x + variance_y - 2 * correlation_xy * np.sqrt(variance_x * variance_y)62    )63    weight_y = 1 - weight_x64    portfolio_variance = (weight_x**2) * variance_x + (weight_y**2) * variance_y + 2 * weight_x * weight_y * correlation_xy * np.sqrt(variance_x * variance_y)65 66    # Mostrar los pesos y la varianza de la cartera67    st.subheader("Portfolio Weights and Returns")68    st.write("Assuming X and Y represent returns of portfolios:")69    st.write(f"Weight of X (w_x): {weight_x:.2f}")70    st.write(f"Weight of Y (w_y): {weight_y:.2f}")71    st.write(f"Expected Return (E(r)): {weight_x * mean_x + weight_y * mean_y:.2f}")72    st.write(f"Portfolio Variance (Var(r)): {portfolio_variance:.2f}")73else:74    st.write(":red[Please, enter the number of elements and click the generate button.]")75 76 77