Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,84 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
#
|
7 |
-
st.
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
st.line_chart(df.set_index('Día'))
|
32 |
-
|
33 |
-
# Decisión de inversión basada en la rentabilidad ajustada al riesgo
|
34 |
-
# Calculamos el rendimiento esperado como un promedio ponderado
|
35 |
-
expected_return = (w1 * mean_action1 + w2 * mean_action2) / (w1 + w2)
|
36 |
-
st.write(f"Retorno esperado ponderado: {expected_return:.2f}")
|
37 |
-
|
38 |
-
# Sugerencia de inversión basada en la mayor rentabilidad esperada
|
39 |
-
if mean_action1 > mean_action2:
|
40 |
-
st.write("Sugerencia: Invertir más en Acción 1 puede ser más rentable a largo plazo.")
|
41 |
-
else:
|
42 |
-
st.write("Sugerencia: Invertir más en Acción 2 puede ser más rentable a largo plazo.")
|
43 |
|
44 |
-
|
45 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import random
|
6 |
|
7 |
+
# Configuring the page
|
8 |
+
st.set_page_config(page_title="Historical Statistics Calculator")
|
9 |
|
10 |
+
# Streamlit app layout
|
11 |
+
st.header("Calculate Statistics from Historical Data")
|
12 |
+
|
13 |
+
with st.expander("Data Generation Settings"):
|
14 |
+
N = st.number_input("Define the number of elements for each data list:", min_value=10, max_value=1000, step=1, value=100)
|
15 |
+
|
16 |
+
# Button to generate the numbers
|
17 |
+
generate_button = st.button("Generate Data")
|
18 |
+
|
19 |
+
if generate_button:
|
20 |
+
# Generate two lists of N random numbers each within a defined range
|
21 |
+
x = [random.randint(0, 20) for _ in range(N)]
|
22 |
+
y = [random.randint(0, 20) for _ in range(N)]
|
23 |
+
|
24 |
+
st.session_state['x'] = x
|
25 |
+
st.session_state['y'] = y
|
26 |
+
|
27 |
+
# Check if data is available in the session state
|
28 |
+
if 'x' in st.session_state and 'y' in st.session_state:
|
29 |
+
x = st.session_state['x']
|
30 |
+
y = st.session_state['y']
|
31 |
+
|
32 |
+
# Calculate statistics
|
33 |
+
x_bar = np.mean(x)
|
34 |
+
y_bar = np.mean(y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
st.subheader("Summary Statistics")
|
37 |
+
col1, col2 = st.columns(2)
|
38 |
+
with col1:
|
39 |
+
st.metric("E(x)", f"{x_bar:.2f}")
|
40 |
+
st.metric("var(x)", f"{np.var(x):.2f}")
|
41 |
+
with col2:
|
42 |
+
st.metric("E(y)", f"{y_bar:.2f}")
|
43 |
+
st.metric("var(y)", f"{np.var(y):.2f}")
|
44 |
+
|
45 |
+
# Correlation
|
46 |
+
cov_xy = np.corrcoef(x, y)[0, 1]
|
47 |
+
st.metric("Correlation (x, y)", f"{cov_xy:.2f}")
|
48 |
+
|
49 |
+
# Plots
|
50 |
+
fig, ax = plt.subplots()
|
51 |
+
ax.scatter(x, y)
|
52 |
+
ax.set_title("Scatter Plot of x vs y")
|
53 |
+
ax.set_xlabel("x values")
|
54 |
+
ax.set_ylabel("y values")
|
55 |
+
st.pyplot(fig)
|
56 |
+
|
57 |
+
# Plot of returns
|
58 |
+
fig2, ax2 = plt.subplots()
|
59 |
+
ax2.plot(np.arange(len(x)), x, label='x')
|
60 |
+
ax2.plot(np.arange(len(y)), y, label='y')
|
61 |
+
ax2.set_ylim(-10, 40)
|
62 |
+
ax2.set_title("Time Series of Returns")
|
63 |
+
ax2.set_xlabel("Time")
|
64 |
+
ax2.set_ylabel("Returns")
|
65 |
+
ax2.legend()
|
66 |
+
st.pyplot(fig2)
|
67 |
+
|
68 |
+
# Portfolio weights and returns calculation
|
69 |
+
w_x = (x_bar - y_bar + np.var(y) - cov_xy * np.sqrt(np.var(x) * np.var(y))) / (
|
70 |
+
np.var(x) + np.var(y) - 2 * cov_xy * np.sqrt(np.var(x) * np.var(y))
|
71 |
+
)
|
72 |
+
w_y = 1 - w_x
|
73 |
+
|
74 |
+
var_r = (w_x**2)*np.var(x) + (w_y**2)*np.var(y) + 2*w_x*w_y*cov_xy*np.sqrt(np.var(x) * np.var(y))
|
75 |
+
|
76 |
+
st.subheader("Portfolio Metrics")
|
77 |
+
st.write("Assuming x and y represent portfolio returns:")
|
78 |
+
st.write(f"Weight of x (w_x): {w_x:.2f}")
|
79 |
+
st.write(f"Weight of y (w_y): {w_y:.2f}")
|
80 |
+
st.write(f"Expected Portfolio Return (E(r)): {w_x*x_bar + w_y*y_bar:.2f}")
|
81 |
+
st.write(f"Portfolio Variance (var(r)): {var_r:.2f}")
|
82 |
+
|
83 |
+
else:
|
84 |
+
st.warning("Please generate data to display results.")
|