azarazua commited on
Commit
fb81260
1 Parent(s): b0d34f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -33
app.py CHANGED
@@ -1,38 +1,79 @@
1
  import streamlit as st
2
  import numpy as np
 
3
  import matplotlib.pyplot as plt
 
4
 
5
  # Streamlit app layout
6
- st.title("Random Sample Generator and Statistics Calculator")
7
-
8
- with st.form("my_form"):
9
- sample_size = st.number_input("Enter the sample size:", min_value=1, step=1, value=100)
10
- low = st.number_input("Enter the lower bound of the uniform distribution:", value=0)
11
- high = st.number_input("Enter the upper bound of the uniform distribution:", value=1)
12
- if st.form_submit_button("Generate Samples"):
13
- # Generate random samples
14
- samples = np.random.uniform(low, high, sample_size)
15
-
16
- # Display the generated samples
17
- if "samples" in locals():
18
- st.subheader("Generated Samples")
19
- st.write(samples)
20
-
21
- # Calculate and display descriptive statistics
22
- st.subheader("Descriptive Statistics")
23
- st.write(f"Mean: {np.mean(samples):.2f}")
24
- st.write(f"Standard Deviation: {np.std(samples):.2f}")
25
- st.write(f"Variance: {np.var(samples):.2f}")
26
- st.write(f"Min: {np.min(samples):.2f}")
27
- st.write(f"Max: {np.max(samples):.2f}")
28
-
29
- # Visualize the distribution with a histogram
30
- st.subheader("Histogram")
31
- plt.hist(samples, bins=20, color='skyblue', edgecolor='black')
32
- plt.xlabel('Value')
33
- plt.ylabel('Frequency')
34
- plt.title('Histogram of Random Samples')
35
- st.pyplot(plt)
36
- plt.close()
37
- else:
38
- st.warning("Please generate samples first.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")