Question

Define and test a function to generate the covariance matrix for an input vector. For example, if...

Define and test a function to generate the covariance matrix for an input vector. For example, if the input is v=[1, 2, 5], then the matrix is v*vT = 1 2 5 2 4 10 5 10 25 (write the code in python

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import numpy as np
import math
stdv = {"ABC":0.3,"XYZ":0.2}
tickersCorr = ["ABC","XYZ"]
# Assuming a 0.5 correlation here is the correlation matrix
c = [[1,0.5],[0.5,1]]
def varCovarMatrix(stocksInPortfolio):
cm = np.array(c)
vcv = []
for eachStock in stocksInPortfolio:
row = []
for ticker in stocksInPortfolio:
if eachStock == ticker:
variance = math.pow(stdv[ticker],2)
row.append(variance)
else:
cov = stdv[ticker]*stdv[eachStock]* cm[tickersCorr.index(ticker)][tickersCorr.index(eachStock)]
row.append(cov)
vcv.append(row)
vcvmat = np.mat(vcv)
return vcvmat
print(varCovarMatrix(["ABC","XYZ"]))
Add a comment
Know the answer?
Add Answer to:
Define and test a function to generate the covariance matrix for an input vector. For example, if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT