Skip to content

Instantly share code, notes, and snippets.

@memonkey01
Created December 13, 2017 19:49
Show Gist options
  • Select an option

  • Save memonkey01/1f99c68bcfb0dda059f34ae8b1747e3a to your computer and use it in GitHub Desktop.

Select an option

Save memonkey01/1f99c68bcfb0dda059f34ae8b1747e3a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Precios y Distribución Log Normal
Autor: Guillermo Izquierdo
Este código es para fines educativos exclusivamente
"""
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
from datetime import date, timedelta
matplotlib.style.use('ggplot')
def get_index(index):
# Definimos las fechas de nuestro indice
today = date.today()
day = timedelta(days=1)
today2 = today - day
enddate = today2.isoformat()
years = timedelta(weeks=1000)
period = today - years
startdate = period.isoformat()
# Definimos el indice que queremos descargar
index = index
# Obtenemos los datos usando pandas_datareader
#Dividimos los datos en dos, precios y retornos
data = pdr.get_data_yahoo(index, start=startdate, end=enddate)
data['returns'] = data['Close'].pct_change()
data = data.dropna()
prices = data['Close']
returns = data['returns']
return [prices, returns]
def lognorm_check(dataframe):
sigma = np.std(np.log(dataframe))
mu = np.mean(np.log(dataframe))
count, bins, ignored = plt.hist(
dataframe, 100, normed=True, align='mid', color='blue', label='Histograma de precios')
x = np.linspace(min(bins), max(bins), 10000)
pdf = (np.exp(-((np.log(x) - mu)**2) / (2 * sigma**2))) / \
(x * sigma * np.sqrt(2 * np.pi))
plt.plot(x, pdf, linewidth=2, color='r', label='Distribución Lognom')
plt.axis('tight')
plt.legend()
plt.title('Lognorm Prices')
plt.xlabel("Precios")
plt.ylabel("Frecuencia")
plt.show()
index = '^MXX'
indice = get_index(index)
lognorm_check(indice[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment