0
$\begingroup$

consider the following problem i am trying to find the monthly covariance matrix given daily data. i have the following codeimport datetime

import pandas as pd
import yfinance as yf
import numpy as np

tickers = ['AAPL', 'AMZN', 'XOM']
start_date1 = datetime.date(2010, 1, 2)
end_date1 = datetime.date(2019, 12, 31)
daily_data1 = yf.download(tickers, start=start_date1, end=end_date1)  # definere datasættet
daily_data1 = daily_data1['Adj Close'].dropna()


frames = [v for _, v in daily_data1.groupby(pd.Grouper(freq='M'))]

for month in frames:
    cov_matrix = (month.pct_change().apply(lambda x: np.log(1 + x)).cov())

so i think i would multiply the cov_matrix with the length of the given month by i am unsure if its that easy so multiply the cov_matrix*len(month)? if someone could confirm it. Thanks in advance

$\endgroup$
4
  • 4
    $\begingroup$ IMO the best way to find the monthly covariance matrix is to downsample the daily data to monthly and then directly compute the covariance on that monthly data. $\endgroup$
    – nbbo2
    Commented Oct 29, 2022 at 10:52
  • $\begingroup$ how do I do it. not sure what you mean. will remind you that the above is a part of a more extensive code where I am using daily data I have just deleted all unnecessary for my question $\endgroup$
    – Robert
    Commented Oct 29, 2022 at 11:10
  • 1
    $\begingroup$ @Robert you can downsample your daily data to monthly, using one line of Python code: daily_data1.resample('M').last() will give you the last day of each month for the corresponding daily_data1 dataframe. Now transform said monthly data to returns and compute the (rolling) covariance matrix on that data. $\endgroup$
    – Pleb
    Commented Oct 30, 2022 at 21:09
  • $\begingroup$ The nice part about @nbbo2 suggestion is that alignment and weak auto correlation and cross correlation effects are less of a problem for monthly observations. The daily alignment (perhaps of last trade) is less important for returns spanning a month. The effects of correlation are included in the monthly returns. See also: quant.stackexchange.com/questions/71574/… $\endgroup$
    – krkeane
    Commented Dec 1, 2022 at 18:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.