Fluorite | Level 6

## calculating variance and covariance using a minimum and maximum no. observations

Hello everyone,

I need to calculate stock beta using the formula: beta= covariance (Rm,Ri) / variance (Rm). I need to calculate beta using 24-60 observations. So, I need to create a new column that has the betas for each date-company calculated depending on the last 24-60 observations (no problem if it started from the current observation). I can create lags but I want more practical solution please giving that my data have several companies. here is how my data looks:

date                 company       Ri      Rm

Jan-2001              A                      3%       3.5%

Feb-2001              A                      3.2%     3.9%

Mar-2001              A                      2.7%     3.1%

.

.

.

Jan-2001              B                      3.9%       3.5%

Feb-2001              B                      4.1%     3.9%

Mar-2001              B                      3.3%     3.1%

.

.

your help is appreciated.

thank you in advance

11 REPLIES 11
Diamond | Level 26

## Re: calculating variance and covariance using a minimum and maximum no. observations

PROC CORR will calculate covariances and variances for you. Use the BY statement to have this calculated for each company.

--
Paige Miller
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

thank you Paige Miller

Diamond | Level 26

## Re: calculating variance and covariance using a minimum and maximum no. observations

@Batool wrote:

thank you Paige Miller

You do NOT need a macro.

Explain further, give a concrete example of what you mean by "determine the time span between 24 and 60".

--
Paige Miller
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

i need 24-60 observations to calculate the  variance and covariance. so the minimum number of observations used to calculate the variance and covariance should be 24 and the maximum is 60.

Diamond | Level 26

## Re: calculating variance and covariance using a minimum and maximum no. observations

So if there are less than 24 observations, then you don't calculate the variance and covariance? Is that correct?

If there are more than 60 observations, then what do you do?

--
Paige Miller
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

that is right. if there is more than 60 i want to use the last 60 only.

I was searching and I think I found what i need (code attached),

can you please help me making it a roll regression so instead of calculating the beta using cov/var, i want to calculate it as the estimated coefficient from the model: (Ri-Rf)= beta (Ri-Rm) using the same window.

Diamond | Level 26

## Re: calculating variance and covariance using a minimum and maximum no. observations

@Batool wrote:

that is right. if there is more than 60 i want to use the last 60 only.

I was searching and I think I found what i need (code attached),

can you please help me making it a roll regression so instead of calculating the beta using cov/var, i want to calculate it as the estimated coefficient from the model: (Ri-Rf)= beta (Ri-Rm) using the same window.

So if I am interpreting this properly, you no longer need help with the original problem? Is that correct?

What is a "roll regression"? What is Rf?

--
Paige Miller
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

I mean a macro regression. and Rf is another variable in my data.

Diamond | Level 26

## Re: calculating variance and covariance using a minimum and maximum no. observations

So please define "macro regression".

--
Paige Miller
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

thank you Paige Miller. I think I should search for it more.
Fluorite | Level 6

## Re: calculating variance and covariance using a minimum and maximum no. observations

I mean a window

Discussion stats
• 11 replies
• 1769 views
• 0 likes
• 2 in conversation