last day (16 days later) » 

3:43 PM
3
A: Testing homoscedasticity with Breusch-Pagan test

CharlieThe problem isn't heteroskedasticity, that's why it's passing the test. The problem is that your model doesn't work well for (at least some of) your observations. I've never seen anyone analyze stock prices without looking at their differences. Try a Dickey-Fuller test for a unit root---I bet t...

 
I have replied to mpiktas, before Breusch-Pagan I check the series with: Phillips-Perron and KPSS unit root tests. Unfortunately, the series pass these tests. I also check the cointegration with Johansen and the series pass again(cointegrated). What could I do?
 
Your data do not reject the null in the KPSS and do reject the null in the Phillips-Perron test?

As I said, BP is telling you that heteroskedasticity isn't a problem here, so you don't need to correct for it. The pattern of your residuals suggests that there may be some kind of time trend lurking around if there isn't a unit root; I added that part to my answer.

Don't worry about heteroskedasiticy (you pass BP), worry about your model.

Suggestions to remove spikes in residuals: Try incorporating time trends or seasonality; try using logs of prices instead.
If you really worry about heteroskedasticity for some reason, even though BP says that you don't need to and your plots suggest that your model needs to be tweaked instead, you can use robust standard errors.
 
the PP reject the null and KPSS can't reject the null. So the series should not have unit root and should be stationary...and also johansen procedure tell me that the series are cointegrated. My problem is not to correct it, I only what to delete the pair (stockA - stockB) that don't have constant variance. BP test is saying that the data is homoscedastic but is not. So what is the method that i can use to understand if this "variance" is constant for real ?
as you surely understand I need to do the linear regression to know the coefficient, EVERY stock of A how many stocks of B I need to have? If i remove spikes, or doing any other change I cant get fixed coefficients.
 
When I look at your plots, I don't see heteroskedasticity---I see areas where the model seems inappropriate. Also, you don't want to throw heteroskedastic data away; instead, you correct it using weighted least squares or robust standard errors. I don't understand what you mean by "EVERY stock of A how many stocks of B I need to have?"
 
Hello Charlie, I would thank you for you help! very much appreciated....
 
3:47 PM
Sure, no problem.
 
i try to explain what I mean (excuse me for the english, i'm Italian)
ok, so I have two stocks A and B, those stocks prices are in a matrix, as you saw i have
prices[,1] and prices[,2] (stock A - stock B)
there are at least 600 prices each
obviously each prices corrispond to the same day both, example:
i know that prices[,1][1] has the same date of prices[,2][1] etc etc
ok, now I do a linear regression and i test the residuals with PP and KPSS tests
 
prices [,1] is the price of stock A and prices[,2] is the price of stock B?
 
and then pass prices matrix to johansen pricedure to understand if there is the cointegration...
yes exactly
 
What do you mean by (stock A - stock B)?
 
so now, after all those tests i would know if the variance is constant.....why? because as you saw i get a lot of plots that shown different variance.....example: at the begginning there is a big more and then flat..... i only want pair with the same variance (less or more)
I mean: prices[,1] is the first stock and prices[,2] is the second stock
then i answer to your question...
 
3:52 PM
When it comes to testing for unit roots, it's best to test each variable before you put them into a regression. Regression only makes sense when the included variables are stationary, as opposed to just the residuals themselves being stationary. Hence, I would test whether the price of stock A is stationary and whether the price of stock B is stationary.
 
i mean, when i do a linear regression i get the coeffients to understand for 1 stockA how many stocks of b do i have to buy to have the same "value" (less or more)
 
When you say that the residuals are bigger, note that they are bigger in a positive direction. Heteroskedasticity is about spread. If heteroskedasticity was the real problem, you would see residuals spread highly above and below 0.\
 
yes i could do it...but i don't know if there is a lot of sense because as i told you i have to "use" a pair...so stock A and stock B.....so maybe stock A is not stationary but if it is compared to another it should be.
 
You have a bunch of points way above 0, but few below in some points and the opposite situation in others. This makes me believe that there is some kind of missing variable or pattern; a time trend, for instance.
 
with "bigger" i mean that there are not constant....i can re-type with: the volatility is not stationary.
can i show you another pair (fgood for me? )
Charlie, take a look at: imageshack.us/f/843/3341032.png this spread is very good to me....as you can see the variance is constant
the green lines are the deviation sd()
 
3:58 PM
Okay
 
first line: mean + sd() second line: mean + sd()*2
so i thought that the variance is what i need to looking for.....to understand if it is constant or not
 
My opinion is that the negative spike at around 180 in your index or 14 in your fitted values makes me wonder whether the linear model is working well for those points. I would worry about this before I worried about heteroskedasticity, a point echoed by the results of the BP test that you're getting. If you aren't worried about this, but are worried about heteroskedasticity, all you have to do is apply robust standard errors and you're finished.
 
i have to remove the pairs that have not a constant variance (constant moves)
 
No, you don't have to remove those pairs. Robust standard errors corrects for their heteroskedasticity.
Also, note that heteroskedasticity doesn't give you bad coefficients, only bad standard errors.
Indeed, if you use robust standard errors, your coefficients are exactly the same as they are under OLS.
You could do weighted least squares, which is more complicated and I wouldn't recommend it for your particular situation. This is another way to correct for heteroskedasticity. In neither case do you remove any data.
 
one moment....I told heterodastic because i find on wikipedia that heterodastic has different variance....so i need to find homoscedastic variance....but if it is not a good test.. is not a problem for me...
so one moment....recap, please:
1. UNIT ROOT TESTS
2. COINTEGRATION TESTS
3. ?
as i told you the link i gave you has a good spread for me...i need to have this kind of spread
 
4:06 PM
I'm sorry, what are you asking? What procedure to follow? I
1. Test every variable in my model for a unit root. Take a difference if there is one. My guess is that you test each price, as opposed to the residuals from your regression, you'll get unit roots.
2. Run your regression (you don't need to worry about cointegration if you fix unit roots in step 1).
3. You should test your residuals for serial correlation/autocorrelation in the context of time series data.
4. Apply robust standard errors to correct for heteroskedasticity and, potentially, autocorrelation.
What do you mean by "i need to have this kind of spread"?
Why?
 
yes the procedure to follow....i need this spread because i'm studing pair trading, so i need to have pairs (Stocks) that have constant variance but after N days they reconvert to the mean.... i can know that with UNIT Root but as i told you i don't want to use pair that have different moves...only constant.... .I read your procedure
so 1 ok, 2 ok.....about the third point are you referring to Durbin-Watson test ?
 
Durbin-Watson has a lot of problems---I use Breusch-Godfrey.
 
ok so......the point number 3..... Breush-Godfrey
if it pass i go to point 4....right?
 
So are you saying that you need pairs with constant variance because: (1) You need this assumption to get good estimates from OLS or (2) Because those are the stock pairs (A and B) that you are interested in finding?
Yes
 
i don't know how to answer to your question...but I try to explain..
 
4:12 PM
If you're interested, I have the slides from the econometrics course that I teach here: cgibbons.berkeley.edu/Courses/econ140-s11.htm
 
wow, sure i'm reallyu interesting in econometrics...
i try to exlpain....
i have many stocks.... i have to compare two stocks each time... example:
AAA - BBB
AAA - CCC
(are the symbols)
then....i do all the test i wrote you because...i need to find a good pair that their "errors" (divergence) are not too much (<---probably i wrote it very very wrong in english :)
 
So you compare AAA to BBB at 600 times (days, maybe), then compare AAA to CCC at 600 times, and so on?
Regress AAA on BBB, then AAA on CCC?
 
exaclty
yes
i have 600 prices of each stock
*daily price
 
So you have some coefficient from the regression of AAA on BBB and a coefficient of AAA on CCC.
 
exactly
 
4:17 PM
Okay
 
those coefficients are the UNIT that i need to calculate how many stocks to buy....example 1 stock (A) corrisponding to 3 stocks (B)
 
The coefficient says that, if stock BBB goes up by $1, then AAA goes up the $(coefficient).
 
if a price of AAA is 10 and a value of BBB is 1 it means that i need 1 AAA and 10 BBB (obviously if this relationship is constant)
exactly
 
You just said different things.
 
yes but i mean what you said....you have wrote it better :D
 
4:21 PM
Suppose that the price of AAA is 10 and BBB is 1. The coefficient may be 2. This would say that on the day when AAA=10 and BBB=1, you need 10 BBB to have the same value as AAA. But, for the change in the value in AAA to equal that of BBB on ANY average day, you need 2 BBB for 1 AAA.
 
interesting
example:
> a <- c(1,2,3)
> b <- c(10,20,30)
> lm(a~b)

Call:
lm(formula = a ~ b)

Coefficients:
(Intercept) b
0.0 0.1
A <- 1, 2, 3 anche B should be B <- 10*0.1, 20*0.1, 30*0.1
 
Try letting b <- c(15, 25, 35)
 
i try
> b <- c(15, 25, 35)
> lm(a~b)

Call:
lm(formula = a ~ b)

Coefficients:
(Intercept) b
-0.5 0.1
 
In the case that you gave, my distinction doesn't matter. But that's because the intercept is 0.
 
aaah i forgot to tell you that when i do the regression i set the intercept to 0
> lm(a~b+0)

Call:
lm(formula = a ~ b + 0)

Coefficients:
b
0.08193
 
4:25 PM
Are A and B mean 0?
 
they should have mean 0
he wrote: m <- lm(gld ~ gdx + 0, data=t)
always paul teetor said: "I forced the zero intercept because of the economic logic of the spread: If one leg of the spread is priced at zero, the other leg should be zero, too. Or, in common sense terms, if one stock is worth nothing, the related stock should be worth nothing, too. If I did not force a zero intercept, the regression might find a non-zero intercept (just by chance), and the intercept would be meaningless and difficult to interpret; but more seriously it would distort the model."
 
I can't tell what his data look like, but if gld and gdx don't have exactly mean 0, then you actually get biased coefficients. It's usually a bad idea to force the intercept to be 0.
 
ok but related to trading...if i don't set intecept to 0
 
Even if logic says it should be exactly true.
 
when i get the coefficent i also get the intercept...so how to "Trade" the intercept :-)
 
4:28 PM
It might not be exactly true in every sample.
Right.
 
:-) how can i do it?
 
Do what?
 
ehhe, i mean.... if i get:
Coefficients:
(Intercept) b
-0.5 0.1
doing trading i can BUY or SELL
so, with this intecept how can i calculate how many stocks do i have to buy/sell ?
 
The intercept is positive, so price movements between these stocks are positively related---one goes up, we expect the other to go up. If you were hedging, you'd want the opposite. So you could buy AAA and short BBB to get a lower variance/risk.
... risk in you portfolio
 
yes exactly what i do
EXACTLY
but i mean....how to calculate HOW MANY stocks do i have to buy AAA and sell BBB
?
 
4:35 PM
Let me think..
Wait, so is this what you are trying to learn from the regressions?
 
yes
 
If so, you want to minimize the variance of the returns. You shouldn't regress one price on the other, you want to regress the return (today's price minus yesterday's divided by yesterday's) for one on the return for the other.
 
i think....for each 1 AAA stock i need to buy BBB coefficient (the number of the coefficient)
 
You need to know the relationship in the returns, not in the prices themselves.
Every finance model I've ever seen has this form.
 
hmm strange in the page i shown you (paul teetor) seems not...he using normal regression of prices
however, I need to think about it... I do a recap :)
1. unit root for each stock---> if pass i go to the second point
2. regression
 
4:44 PM
The very first step is for you to say what you want to do. Why do you want to buy two pairs of stocks?
Are you maximizing your return with least risk? Are you an airline trying to buy sometime to offset future changes in oil prices?
 
this is the pair trading...arbitrage...to earn on the different
 
If you hedge as he's showing you, you get 0 return in expectation. This is what an airline would want to do to avoid getting beat up by price spikes in fuel.
As I understand what he is doing.
"Mean reverting" = Makes no money on average
As I get his explanaition.
 
no no why no money?
take a look at the plot i sand you
when the spread touch a standard dev it meand that one of the stocks is outoerforming respect to the other...so you will earn when the stocks return to the mean
example.....
 
I'm an airline. I need to buy fuel in the future but I sell tickets today. I want to avoid losing money when fuel goes up. So I buy fuel future. My futures go up, making money, but my costs go up, losing money. On net, I'm even. He's calculating how to come out even.
 
no wait
one moment
i have two stocks...
cointegrated...no unit root etc etc etc
ok.
now
one stock (AAA) goes up and one stock goes DOWN so they are not following... the spread between them is rising...so you sell AAA and buy BBB
because when they will return to the mean it mean that no stock is outperforming respect to the other
and the return to constant
do you understand?
so maybe you will have one stock is loosing...sure, but the other is earning more because the reduce his spread
something like......the stocks are correlated....then sometimes they will be uncorrelated...and then they return correlated again...
 
4:50 PM
Suppose that you get a regression of the price of AAA on the price of BBB with a coefficient of $2. Then, if the price of BBB goes up by $1, you expect the price of AAA to go up by $2. If it the price of AAA goes up by only $1, that's less than you would have expected. Maybe you'd want to buy AAA in that case.
 
yes
becuase the other stock is OUT performing...or the AAA is not performing.....so yes
 
If that's your model of how the world works, in my example, you'd want to buy infinity AAA stocks.
 
uh?
 
If you know that AAA is going to go up, you want to buy as much as you can.
 
no no
is going UP respect to BBB
the relathionsing is reducing
and the spread will be more
example
AAA: 1, 5, 10
BBB: 1,20,8
BBB: 20
are you see at the second
AAA: 5
bbb IS outperforming
so you SELL BBB and buy AAA
when they reduce their gap you will earn
maybe one stock will lose but one will earn more
that's how pair trading works
understand?
Charlie, do you have skype? there we could have more fast chat...i can see you some chart to explain better....then if you want you can remove my account..... I hope not :)
 
4:57 PM
Okay, well I don't know anything about trading really; finance isn't my field. But I'll say this: If the coefficient in your regression is 2, then, if you hold two BBB stocks and sell 1 AAA stock, then you expect the price change in any period to be 0.
 
maybe i have to exaplain better
:-(
 
Change in Pa = 2 change in Pb
That's what the regression is telling you.
If the coefficient is 2.
 
ooh Charlie we never talked about the forth point
do you have skype? am i annoying you?
 
As I said, I can help you interpret a regression that you run, but I can't help you with finance models.
I do need to get back to focusing on work soon.
 
yes yes no problem...maybe we can be in touch if you want...then if i do something wrong you can BAN me :-D ehehhe
 
4:59 PM
Robust standard errors are easy. Just use the sandwich package in R to implement them.
 
ooh charle something worng with BG test
that's what i get
> bgtest(prices[,1]~prices[,2])

Breusch-Godfrey test for serial correlation of order 1

data: prices[, 1] ~ prices[, 2]
LM test = 694.5436, df = 1, p-value < 2.2e-16
take a look at the plot: imageshack.us/f/24/9381831.png
 
That's not surprising when you're using time series data.
 
is ti wrong?
*it
prices is a matrix
 
Serial correlation at best makes your standard errors wrong. At worst, it makes all your coefficients biased.
 
wait, one moment...what do you mean? before you told me BG is better then durbin watson, but how to use it with my prices matrix (two list of prices stocks)
 
5:08 PM
Isn't that just what you reported?
 
> bgtest(prices[,1]~prices[,2])

Breusch-Godfrey test for serial correlation of order 1

data: prices[, 1] ~ prices[, 2]
LM test = 694.5436, df = 1, p-value < 2.2e-16
this ?
 
It uses the residuals from your regression
Yeah, that's it
 
yes...but did you see the p-value ?
do i have to "accept" p--value below 0.05 ?
 
Yeah, so? You have serial correlation.
You reject the null hypothesis of no serial correlation.
 
aaaaaah shit, you are right....good....so if the p-value is BELOW the 0.05 level i reject the null that no serial correlation
oook so this test...should OK....NO?
 
5:10 PM
What do you mean?
 
i mean....can i do it with my 'prices' matrix
 
You just did the test...
 
I mean....do i have to use other variable to do that test or not
ok
i tolud you that because i read:
bgtest(formula, order = 1, order.by = NULL, type = c("Chisq", "F"), data = list())
i only put prices[, 1] ~ prices[, 2] formula
 
Yeah, that's right.
 
ok perfect....so thise result permit me to pass to the last (4) step
sandwich
what is the function that you use for it?
 
5:12 PM
Wait, no you don't pass the test. You fail. You need to take serial correlation into account.
 
hmmm wait, do you mean that i need to looking forp-value ABOVE 0.05 ?
 
The null hypothesis is that you have no serial correlation---this is what is required for regression. You reject that null hypothesis.
So, yes, you need a big p-value.
 
hmmmmm
wait....if i can't reject the null it's mean that there is no serial correlation
 
Yes.
That would be good.
 
so it sounds bad to me...no? the last link i gave you....this plot donì't pass that test :(
is NO SERIAL good?
 
5:15 PM
Yes
 
imageshack.us/f/24/9381831.png return very low p-value
:-(
how is it possible? it's mean that i can't use it...?
 
Why don't you send me the output of the PP and KPSS tests.
 
sure
one momment
PP:
Value of test-statistic, type: Z-tau is: -4.2675

aux. Z statistics
Z-tau-mu -0.0618

Critical values for Z statistics:
1pct 5pct 10pct
critical values -3.441561 -2.865770 -2.569028
I use: ur.pp(res, type='Z-tau', model='constant', lags='short')
> summary(pp)

##################################
# Phillips-Perron Unit Root Test #
##################################

Test regression with intercept


Call:
lm(formula = y ~ y.l1)

Residuals:
Min 1Q Median 3Q Max
-2.93378 -0.37440 0.00785 0.35753 2.40092

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.001856 0.022931 -0.081 0.935
y.l1 0.958312 0.010596 90.438 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> kpss <- ur.kpss(res, type='mu', lags='short')
> summary(kpss)

#######################
# KPSS Unit Root Test #
#######################

Test is of type: mu with 6 lags.

Value of test-statistic is: 0.231

Critical value for a significance level of:
10pct 5pct 2.5pct 1pct
critical values 0.347 0.463 0.574 0.739
where RES has i told you before are the residuals of my regressions
 
Use the prices instead
 
i tried
UNIT ROOT
but is strange the plot seems wanderful!
*wonderful
 
5:22 PM
Okay, so regress the change in the price of AAA on the change in the price of BBB. See if the residuals from that regression have serial correlation.
 
how can i do it with R ?
 
Get the package dyn
 
ok one moment
 
dyn$lm(diff(price[,1]) ~ diff(price[,2]))
 
hmm wait...ok, but i have to pass a formula to BG test
i have done: a <- dyn$lm(diff(prices[,1]) ~ diff(prices[,2]))
can i pass it to BG test^?
> bgtest(a)

Breusch-Godfrey test for serial correlation of order 1

data: a
LM test = 2.4174, df = 1, p-value = 0.12
 
5:30 PM
Yes, that works.
 
fuck Charlie, you are the best
:D
 
So that's the regression that I'd use.
The coefficient has the same interpretation as your regression.
 
so wait...
so you diff to do the difference between today - yesterday prices
right?
then regress it with the other stock
 
Well, tell me... What is the coeff from Pa on Pb and what is the coeff on diff Pa on diff Pb
Yup
 
> a

Call:
lm(formula = dyn(diff(prices[, 1]) ~ diff(prices[, 2])))

Coefficients:
(Intercept) diff(prices[, 2])
-0.003463 0.287091
hmmm wait i get the same result doing:
> a <- lm(diff(prices[,1]) ~ diff(prices[,2]))
> bgtest(a)

Breusch-Godfrey test for serial correlation of order 1

data: a
LM test = 2.4174, df = 1, p-value = 0.12
why to use dyn ?
 
5:33 PM
Hmm, I thought that it gave you the right diff() function for regression. Maybe you don't need it.
What was your original coefficient?
Using the undifferenced prices?
 
i try
> diff <- lm(diff(prices[,1]) ~ diff(prices[,2]))
> nodiff <- lm(prices[,1] ~ prices[,2])
> diff

Call:
lm(formula = diff(prices[, 1]) ~ diff(prices[, 2]))

Coefficients:
(Intercept) diff(prices[, 2])
-0.003463 0.287091

> nodiff

Call:
lm(formula = prices[, 1] ~ prices[, 2])

Coefficients:
(Intercept) prices[, 2]
3.2586 0.9484

>
 
How does the plot of the residuals look from diff?
 
i do an image....one moment
good?
 
Looks good to me.
 
yeees....perfect
so now the test pass (Correctly)
why i need the point 4 ?
 
5:43 PM
You don't have heteroskedasticity or serial correlation any more, so you don't
 
so perfect.....if this test pass no other tests
goood
do you keep always 0.05 confidence level ?
i can set IF is above 0.05 GOOD
otherwise NOT
 
Sometimes you want a big number, sometimes a small number. It depends upon what you are testing.
 
yes sure... i mean for these kind of checks (stocks)
correct?
 
Unit root tests are good for small p-values, but BG tests are good for big ones.
 
yes
correct...
I would be disappointed if we do not see anymore.
can we will be in touch ?
 
5:57 PM
Sure.
 
how? Email? Msn? Skype? what ever you want....i like to talk about these things
 
You can e-mail me: cgibbons@berkeley.edu
I don't really use Skype
 
i just sent you and email....
are you from California?
 
Not originally... From near Boston, Massachusetts, US. Out here for grad school.
I got your e-mail. Glad that I was helpful. I need to focus on some work now. Good luck!
 
sure, thank you again!!! see you soon
good works
have dinner now
(i'm from Rome)
goodbye
btw i need to investigate
maybe BG test is not very useful
pass the test: imageshack.us/f/560/23609.png °_°
 

  last day (16 days later) »