import pandas as pd
import numpy as np
data = {
'Year': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'Stock 1': [11.91, 18.37, 3.64, 24.37, 30.42, -1.45, 20.11, 9.28, 17.63, 15.71],
'Stock 2': [29.59, 15.25, 3.53, 17.67, 12.74, -2.56, 25.46, 6.92, 9.73, 25.09],
'Stock 3': [23.27, 19.47, -6.58, 15.08, 16.24, -15.05, 17.80, 18.82, 3.05, 16.94],
'Stock 4': [27.24, 17.05, 10.20, 20.26, 19.84, 1.51, 12.24, 16.12, 22.93, 3.49],
'Market': [23.00, 17.54, 2.70, 19.34, 19.81, -4.39, 18.90, 12.78, 13.34, 15.31],
'Riskless': [6.20, 6.70, 6.40, 5.70, 5.90, 5.20, 4.90, 5.50, 6.10, 5.80]
}
# Convert the data to a DataFrame
df = pd.DataFrame(data)
# Display the DataFrame
df
# Select only the stock columns
stocks = df[['Stock 1', 'Stock 2', 'Stock 3', 'Stock 4']]
# Calculate the covariance matrix
cov_matrix = stocks.cov()
# Display the covariance matrix
cov_matrix
# Calculate the eigenvalues and eigenvectors of the covariance matrix
eig_values, eig_vectors = np.linalg.eig(cov_matrix)
# Find the index of the largest eigenvalue
index_max_eig_value = np.argmax(eig_values)
# Get the largest eigenvalue and corresponding eigenvector
largest_eig_value = eig_values[index_max_eig_value]
corresponding_eig_vector = eig_vectors[:, index_max_eig_value]
# Normalize the eigenvector
normalized_eig_vector = corresponding_eig_vector / sum(corresponding_eig_vector)
# Display the largest eigenvalue and corresponding normalized eigenvector
largest_eig_value, normalized_eig_vector
df['Principal Component'] = stocks @ normalized_eig_vector
df