fergoma commited on
Commit
3405057
1 Parent(s): e1cee8f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Streamlit app layout
8
+ st.title("Estimating statistics from two sets of historical data")
9
+
10
+ with st.form("data_input_form"):
11
+ N = st.number_input("Enter the number of data points for each set:", step=1)
12
+
13
+ # Button to generate random data sets
14
+ if st.form_submit_button("Generate Random Data Sets"):
15
+ # Generate two sets of N random data points each
16
+ set1 = [random.randint(0, 20) for _ in range(N)]
17
+ set2 = [random.randint(0, 20) for _ in range(N)]
18
+
19
+ if "set1" in globals():
20
+ # Calculating mean of each set
21
+ set1_mean = np.mean(set1)
22
+ set2_mean = np.mean(set2)
23
+
24
+ st.subheader("Expected Values")
25
+ st.write(f"Mean of Set 1: {set1_mean:,.2f}")
26
+ st.write(f"Mean of Set 2: {set2_mean:,.2f}")
27
+
28
+ # Calculating variances
29
+ set1_var = np.var(set1)
30
+ set2_var = np.var(set2)
31
+
32
+ st.subheader("Variances")
33
+ st.write(f"Variance of Set 1: {set1_var:,.2f}")
34
+ st.write(f"Variance of Set 2: {set2_var:,.2f}")
35
+
36
+ # Calculating correlation coefficient
37
+ corr_coefficient = np.corrcoef(set1, set2)[0, 1]
38
+
39
+ st.subheader("Correlation Coefficient")
40
+ st.write(f"Correlation Coefficient: {corr_coefficient:,.2f}")
41
+
42
+ # Scatter plot
43
+ plt.scatter(set1, set2)
44
+ plt.title("Scatter Plot")
45
+ plt.xlabel("Set 1")
46
+ plt.ylabel("Set 2")
47
+
48
+ st.pyplot(plt)
49
+ plt.close()
50
+
51
+ # Line plot
52
+ plt.title("Line Plot of Data Sets")
53
+ plt.plot(np.arange(len(set1)), set1, label="Set 1")
54
+ plt.plot(np.arange(len(set2)), set2, label="Set 2")
55
+ plt.ylim(-10, 40)
56
+ plt.xlabel("Time")
57
+ plt.ylabel("Returns")
58
+ plt.legend()
59
+
60
+ st.pyplot(plt)
61
+ plt.close()
62
+
63
+ # Calculating weights and portfolio returns
64
+ w_set1 = (set1_mean - set2_mean + set2_var - corr_coefficient * np.sqrt(set1_var * set2_var)) / (
65
+ set1_var + set2_var - 2 * corr_coefficient * np.sqrt(set1_var * set2_var)
66
+ )
67
+ w_set2 = 1 - w_set1
68
+
69
+ portfolio_var = (w_set1*2)*set1_var + (w_set2*2)*set2_var + 2*w_set1*w_set2*corr_coefficient*np.sqrt(set1_var * set2_var)
70
+
71
+ st.subheader("Weights and Portfolio Returns")
72
+ st.write("Assuming Set 1 and Set 2 represent returns of portfolios")
73
+ st.write(f"Weight of Set 1: {w_set1:,.2f}")
74
+ st.write(f"Weight of Set 2: {w_set2:,.2f}")
75
+ st.write(f"Expected Portfolio Return: {w_set1*set1_mean + w_set2*set2_mean:,.2f}")
76
+ st.write(f"Portfolio Variance: {portfolio_var:,.2f}")
77
+
78
+ else:
79
+ st.write("Please input the number of data points for each set.")