Histograms

Introduzione a Python per la finanza

Adina Howe

Instructor

Why histograms for financial analysis?

histogram

Introduzione a Python per la finanza

Histograms and Data

  • Is your data skewed?
  • Is your data centered around the average?
  • Do you have any abnormal data points (outliers) in your data?
Introduzione a Python per la finanza

Histograms and matplotlib.pyplot

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=3)
plt.show()

hist1

Introduzione a Python per la finanza

Changing the number of bins

import matplotlib.pyplot as plt
plt.hist(prices, bins=6)
plt.show()

hist2

Introduzione a Python per la finanza

Normalizing histogram data

import matplotlib.pyplot as plt
plt.hist(prices, bins=6, normed=1)
plt.show()

normed

Introduzione a Python per la finanza

Layering histograms on a plot

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1)
plt.hist(x=prices_new, bins=6, normed=1)
plt.show()
Introduzione a Python per la finanza

Histogram result

two_hist_normed

Introduzione a Python per la finanza

Alpha: Changing transparency of histograms

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5)
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5)
plt.show()
Introduzione a Python per la finanza

Histogram result

two-hist-transparent

Introduzione a Python per la finanza

Adding a legend

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5, label="Prices 1")
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5, label="Prices New")
plt.legend()
plt.show()
Introduzione a Python per la finanza

Histogram result

pretty hist

Introduzione a Python per la finanza

Let's practice!

Introduzione a Python per la finanza

Preparing Video For Download...