Skip to content

Instantly share code, notes, and snippets.

@dkatz23238
Created June 27, 2023 18:02
Show Gist options
  • Save dkatz23238/ed3068b3c4f73b16676188fe1ea1a2ea to your computer and use it in GitHub Desktop.
Save dkatz23238/ed3068b3c4f73b16676188fe1ea1a2ea to your computer and use it in GitHub Desktop.
PCA
import numpy as np
# ROD, PB, PE, DivP, netIncomeTTM, ROA
px_0 = np.array([40, 40, 40, 40, 40, 40])
px_1 = np.array([-49.54, -37.51, -16.38, 3.82, 27.54, 71.29])
pc_0 = 0.01
pc_1 = -0.02
reconstructred_portfolio = pc_0 * px_0 + pc_1 * px_1
# It won't be an exact match since we are only using two principal components, but it's a good approximation.
# Since we know our weights must sum up to one we can normalize using something like e^X/sum(e^X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment