Está en la página 1de 3

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, DayLocator
# Ruta de mi archivo de pinterest
ppd_excel = "C:/Users/enriq/.spyder-py3/pinterest_daily_prices.xlsx"
#Lectura de mi archivo
data_dailyprices = pd.read_excel(ppd_excel)

#Acceso a las columnas del archivo


date = data_dailyprices['Date']
price = data_dailyprices['Close']

#Imprimir los primeros registros, esto es solo para comprobar el correcto trabajo
de los datos
print(date.head())
print(price.head())
print(date.tail())
print(price.tail())
# Impresión de gráfica de precios en función de las fechas
fig, ax = plt.subplots()
ax.plot(date, price)
plt.title('Precios diarios de Pinterest 2020-2023')
plt.xlabel('Fecha')
plt.ylabel('Precio')
plt.grid(True)

#Personalización del eje X


ax.xaxis.set_major_locator(DayLocator(interval=260))
ax.xaxis.set_major_formatter(DateFormatter("%d-%m-%Y"))
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/grafica_de_precios.pdf")
plt.show()

#Trabajo con rendimientos


returns = price.pct_change()
print(returns.head())
print(returns.tail())
summary = returns.describe()
print(summary)
# Gráfica para los rendimientos
fig, ax = plt.subplots()
ax.plot(date, returns)
plt.title('Rendimientos diarios')
plt.xlabel('Fecha')
plt.ylabel('Rendimiento')
plt.grid(True)
#Personalización eje X
ax.xaxis.set_major_locator(DayLocator(interval=260))
ax.xaxis.set_major_formatter(DateFormatter("%d-%m-%Y"))
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/grafica_de_rendimientos.pdf")
plt.show()

#Histograma DIARIO
plt.hist(returns, bins=50, edgecolor='k', alpha=0.7)
plt.title('Histograma de Rendimientos Diarios')
plt.xlabel('Rendimientos')
plt.ylabel('Frecuencia')
plt.grid(True)
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/histograma_de_rendimientos.pdf")
plt.show()

#Diagrama de caja y bigotes DIARIO


mean = np.mean(returns)
sigma = np.std(returns)
norma = (returns - mean) / sigma
print("Datos originales", returns)
print("Datos normalizados", norma)
plt.figure(figsize=(8, 6))
plt.boxplot(norma, vert=False)
plt.title('Diagrama de caja y bigotes de rendimientos diarios')
plt.xlabel('Rendimiento')
plt.grid(True)
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/caja_y_bigotes_diario.pdf")
plt.show()
#es probable que si el grafico está vvacío sea porque no hay suficiente
variabilidad

#RENDIMIENTOS ANUALES -RUTAS


year1 = "C:/Users/enriq/.spyder-py3/PINS (1).xlsx"
year2 = "C:/Users/enriq/.spyder-py3/PINS (2).xlsx"
year3 = "C:/Users/enriq/.spyder-py3/PINS (3).xlsx"
year4 = "C:/Users/enriq/.spyder-py3/PINS (4).xlsx"
#LECTURAS
data1 = pd.read_excel(year1)
data2 = pd.read_excel(year2)
data3 = pd.read_excel(year3)
data4 = pd.read_excel(year4)
#ELECCIÓN DE COLUMNAS PRECIO
price1 = data1['Close']
price2 = data2['Close']
price3 = data3['Close']
price4 = data4['Close']
#CÁLCULO DE RENDIMIENTOS
returns1 = price1.pct_change()
returns2 = price2.pct_change()
returns3 = price3.pct_change()
returns4 = price4.pct_change()
yearreturn = (returns1, returns2, returns3, returns4)
#CALCULO DE LAS MEDIAS
mean1 = np.mean(returns1)
mean2 = np.mean(returns2)
mean3 = np.mean(returns3)
mean4 = np.mean(returns4)
vmean = (mean1, mean2, mean3, mean4)
year = (1, 2 ,3 ,4)
#GRAFICA RENDIMIENTOS ANUALES
fig, ax = plt.subplots()
ax.plot(year, vmean)
plt.title('Rendimientos anuales')
plt.xlabel('Año')
plt.ylabel('Rendimiento')
plt.grid(True)
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/grafica_de_rendimientos_anuales.pdf")
plt.show()
#HISTOGRAMA RENDIMIENTOS ANUALES
plt.hist(yearreturn, bins=50, edgecolor='k', alpha=0.7)
plt.title('Histograma de Rendimientos Anuales')
plt.xlabel('Rendimientos')
plt.ylabel('Frecuencia')
plt.grid(True)
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/histograma_de_rendimientos_anuales.pdf")
plt.show()
#CAJA Y BIGOTES ANUALES
plt.figure(figsize=(8, 6))
plt.boxplot(vmean, vert=False)
plt.title('Diagrama de caja y bigotes de rendimientos anuales')
plt.xlabel('Rendimiento')
plt.grid(True)
plt.savefig(r"C:\Users\enriq\Downloads\python doc\trabajo
estadística/caja_y_bigotes_anuales.pdf")
plt.show()

También podría gustarte