Kfong commited on
Commit
a14709d
1 Parent(s): 1894445

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -44
app.py CHANGED
@@ -1,45 +1,61 @@
1
- from flask import Flask, request, jsonify, render_template
2
  import numpy as np
3
- import plotly.graph_objs as go
4
-
5
- app = Flask(__name__)
6
-
7
- @app.route('/')
8
- def index():
9
- return render_template('index.html')
10
-
11
- @app.route('/procesar_datos', methods=['POST'])
12
- def procesar_datos():
13
- try:
14
- # Obtener los datos ingresados por el usuario
15
- lista_x1 = [float(x.strip()) for x in request.form.get('lista_x1').split(',') if x.strip()]
16
- lista_x2 = [float(x.strip()) for x in request.form.get('lista_x2').split(',') if x.strip()]
17
-
18
- # Verificar que las listas tengan más de 10 elementos
19
- if len(lista_x1) < 10 or len(lista_x2) < 10:
20
- raise ValueError("Ambas listas deben contener al menos 10 elementos.")
21
-
22
- # Calcular E(x1) y E(x2)
23
- mean_x1 = np.mean(lista_x1)
24
- mean_x2 = np.mean(lista_x2)
25
-
26
- # Calcular la desviación estándar
27
- std_x1 = np.std(lista_x1)
28
- std_x2 = np.std(lista_x2)
29
-
30
- # Calcular la correlación
31
- correlation = np.corrcoef(lista_x1, lista_x2)[0, 1]
32
-
33
- # Generar la gráfica de puntos
34
- trace = go.Scatter(x=lista_x1, y=lista_x2, mode='markers', marker=dict(color='blue'))
35
- layout = go.Layout(title='Gráfica de Correlación', xaxis=dict(title='x1'), yaxis=dict(title='x2'))
36
- fig = go.Figure(data=[trace], layout=layout)
37
- graph_json = fig.to_json()
38
-
39
- return render_template('resultado.html', mean_x1=mean_x1, mean_x2=mean_x2, std_x1=std_x1, std_x2=std_x2, correlation=correlation, graph=graph_json)
40
-
41
- except Exception as e:
42
- return render_template('error.html', error=str(e))
43
-
44
- if __name__ == '__main__':
45
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+ def analizar_inversiones(lista1, lista2):
6
+ # Convertir las listas en arrays numpy
7
+ array1 = np.array(lista1)
8
+ array2 = np.array(lista2)
9
+
10
+ # Calcular desviación estándar
11
+ std_dev1 = np.std(array1)
12
+ std_dev2 = np.std(array2)
13
+
14
+ # Calcular valor esperado
15
+ mean1 = np.mean(array1)
16
+ mean2 = np.mean(array2)
17
+
18
+ # Calcular correlación
19
+ correlation = np.corrcoef(array1, array2)[0, 1]
20
+
21
+ # Crear gráfica
22
+ fig, ax = plt.subplots()
23
+ ax.scatter(array1, array2)
24
+ ax.set_xlabel('Lista 1')
25
+ ax.set_ylabel('Lista 2')
26
+ ax.set_title('Investment graph')
27
+ ax.grid(True)
28
+ st.pyplot(fig)
29
+
30
+ # Decidir a cuál invertir más y a cuál menos
31
+ if mean1 > mean2:
32
+ invertir_mas = "List 1"
33
+ invertir_menos = "List 2"
34
+ else:
35
+ invertir_mas = "List 2"
36
+ invertir_menos = "List 1"
37
+
38
+ # Mostrar resultados con un formato agradable
39
+ st.write("Results of the Analisis:")
40
+ st.write(f"Standar desviation of list 1: {std_dev1}")
41
+ st.write(f"Standar desviation of list 2: {std_dev2}")
42
+ st.write(f"Estimated value list 1: {mean1}")
43
+ st.write(f"Estimated value list 2: {mean2}")
44
+ st.write(f"Correlation: {correlation}")
45
+ st.write(f"Invest more in: {invertir_mas}")
46
+ st.write(f"Invest less in: {invertir_menos}")
47
+
48
+ def main():
49
+ st.title("Portfolio")
50
+ st.write("Isert the values for the lists:")
51
+
52
+ # Solicitar al usuario que ingrese los valores de las listas
53
+ lista1 = [st.number_input(f"List 1 - Value {i+1}", value=0.0) for i in range(10)]
54
+ lista2 = [st.number_input(f"List 2 - Value {i+1}", value=0.0) for i in range(10)]
55
+
56
+ # Botón para realizar el análisis
57
+ if st.button("Make Analisis"):
58
+ analizar_inversiones(lista1, lista2)
59
+
60
+ if _name_ == "_main_":
61
+ main()