Histograms

Nhập môn Python cho Tài chính

Adina Howe

Professor

Why histograms for financial analysis?

histogram

Nhập môn Python cho Tài chính

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?
Nhập môn Python cho Tài chính

Histograms and matplotlib.pyplot

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

hist1

Nhập môn Python cho Tài chính

Changing the number of bins

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

hist2

Nhập môn Python cho Tài chính

Normalizing histogram data

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

normed

Nhập môn Python cho Tài chính

Layering histograms on a plot

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True)
plt.hist(x=prices_new, bins=6, density=True)
plt.show()
Nhập môn Python cho Tài chính

Histogram result

two_hist_normed

Nhập môn Python cho Tài chính

Alpha: Changing transparency of histograms

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True, alpha=0.5)
plt.hist(x=prices_new, bins=6, density=True, alpha=0.5)
plt.show()
Nhập môn Python cho Tài chính

Histogram result

two-hist-transparent

Nhập môn Python cho Tài chính

Adding a legend

import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True, alpha=0.5, label="Prices 1")
plt.hist(x=prices_new, bins=6, density=True, alpha=0.5, label="Prices New")
plt.legend()
plt.show()
Nhập môn Python cho Tài chính

Histogram result

pretty hist

Nhập môn Python cho Tài chính

Let's practice!

Nhập môn Python cho Tài chính

Preparing Video For Download...