File size: 2,127 Bytes
efa13ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random

# Streamlit app layout
st.title("Estimating statistics from two lists of historical values")

with st.form("my_form"):
    N = st.number_input("How many elements do you want for each list?", step=1)

    # Button to generate the numbers
    if st.form_submit_button("Click to generate random numbers' lists"):
        # Generate two lists of 100 random numbers each
        x = [random.randint(0, 20) for _ in range(N)]
        y = [random.randint(0, 20) for _ in range(N)]

    # Display the lists in the app
    # st.write('List 1:', x)
    # st.write('List 2:', y)

if "x" in globals():
    x_bar = np.mean(x)
    y_bar = np.mean(y)

    st.subheader("Expected values")
    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("Variances")
    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("Correlation")
    st.write(f"corr(x, y) = {cov_xy:,.2f}")

    plt.scatter(x, y)
    plt.title("Correlations")
    plt.xlabel("x")
    plt.ylabel("y")

    st.pyplot(plt)

    plt.close()

    plt.title("Plot of returns")
    plt.plot(np.arange(len(x)), x)
    plt.plot(np.arange(len(y)), y)
    plt.ylim(-10, 40)
    plt.xlabel('"time"')
    plt.ylabel('"returns"')

    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]")