From: Sean Douglas on
hey guys, i am trying to understand the cointeration approach to find the "hedge ratio" for 2 assets. I am under the impression that this should be the same as running a regression of one asset against the other, so i was thinking the slope would be the same under both methods ( method 1=cointegration using QR factorization and method 2= using least squares) and that the line produced from the equations of the two seperate methods should be the same. here is my code to test this on a very simple example.

x=[10;11;13;11]
y=[18;20;28;22]

%cointegration method with QR factorization

r11=norm(x,2)
q2=x./r11 % %%could also use:[q r] = qr(x,0) %matlab function
mybeta=r11\(q2'*y)
plot(x,mybeta*x,'r')
hold on

% now do least sqares method

n=length(x)
vect=ones(n,1)
A=[vect,x]
Atraspose_times_b=A'*y
ab=[inv(A'*A)*(A'*y)]
alpha=ab(1,1)
beta=ab(2,1)
plot(x,alpha+beta*x)



as maybe some of you know these methods will graph slightly different lines ( the betas are quite different) and i am having trouble understanding why? and which method is right???
please help
From: Bruno Luong on
"Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i21s0j$f75$1(a)fred.mathworks.com>...
> hey guys, i am trying to understand the cointeration approach to find the "hedge ratio" for 2 assets. I am under the impression that this should be the same as running a regression of one asset against the other, so i was thinking the slope would be the same under both methods ( method 1=cointegration using QR factorization and method 2= using least squares) and that the line produced from the equations of the two seperate methods should be the same. here is my code to test this on a very simple example.
>
> x=[10;11;13;11]
> y=[18;20;28;22]
>
> %cointegration method with QR factorization
>
> r11=norm(x,2)
> q2=x./r11 % %%could also use:[q r] = qr(x,0) %matlab function
> mybeta=r11\(q2'*y)

You might equivalently simply do:
mybeta = x\y

>
> % now do least sqares method
>
> n=length(x)
> vect=ones(n,1)
> A=[vect,x]
> Atraspose_times_b=A'*y
> ab=[inv(A'*A)*(A'*y)]
> alpha=ab(1,1)
> beta=ab(2,1)

You might equivalently simply do:
ab = A\y;
alpha=ab(1)
beta=ab(2)


>
> as maybe some of you know these methods will graph slightly different lines ( the betas are quite different) and i am having trouble understanding why? and which method is right???

Because they do not solve the same problem (but I confirm both are least-squares), in one case you look for first-order (affine) fit: y = alpha + beta*x, on the other hand the code in the first part looks for a linear relationship (without constant term): y = beta*x.

So let me reiterate the question: what is the goal you want to achieve?

Bruno
From: Sean Douglas on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i22bh7$js7$1(a)fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i21s0j$f75$1(a)fred.mathworks.com>...
> > hey guys, i am trying to understand the cointeration approach to find the "hedge ratio" for 2 assets. I am under the impression that this should be the same as running a regression of one asset against the other, so i was thinking the slope would be the same under both methods ( method 1=cointegration using QR factorization and method 2= using least squares) and that the line produced from the equations of the two seperate methods should be the same. here is my code to test this on a very simple example.
> >
> > x=[10;11;13;11]
> > y=[18;20;28;22]
> >
> > %cointegration method with QR factorization
> >
> > r11=norm(x,2)
> > q2=x./r11 % %%could also use:[q r] = qr(x,0) %matlab function
> > mybeta=r11\(q2'*y)
>
> You might equivalently simply do:
> mybeta = x\y
>
> >
> > % now do least sqares method
> >
> > n=length(x)
> > vect=ones(n,1)
> > A=[vect,x]
> > Atraspose_times_b=A'*y
> > ab=[inv(A'*A)*(A'*y)]
> > alpha=ab(1,1)
> > beta=ab(2,1)
>
> You might equivalently simply do:
> ab = A\y;
> alpha=ab(1)
> beta=ab(2)
>
>
> >
> > as maybe some of you know these methods will graph slightly different lines ( the betas are quite different) and i am having trouble understanding why? and which method is right???
>
> Because they do not solve the same problem (but I confirm both are least-squares), in one case you look for first-order (affine) fit: y = alpha + beta*x, on the other hand the code in the first part looks for a linear relationship (without constant term): y = beta*x.
>
> So let me reiterate the question: what is the goal you want to achieve?
>
> Bruno

hey Bruno, thanks for reply,
the goal is to find the hedge ratio of one stock against another. I want to know what beta i should mulptiply by my last x term term to make it someone equivalent to the y term. (this is to know how much of one asset should be bought to be relatively equivalent to another asset. I read in a book that this linear relationship code (without a constant term )is the way to find the hedge ratio. I was not familiar with this method before reading it and i dont fully understand it, but i know a little about the least squares mehtod and think that should tell me the correct beta... so i am just trying to understand how the two methods relate and which method is better and if this book i read is truely correct.

Is it possible that the linear relationship code ( without constant term) factors in the price difference of the assets x and y when calculating the beta or hedge ratio, and maybe the least sqares approach with the alpha term included does not factor in the asset price differences( the difference in price between x and Y). I have done the math and i know this particular theory is not 100% right, but maybe it has something to do with it.

thanks again
From: Bruno Luong on
"Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i22d79$9jh$1(a)fred.mathworks.com>...

> hey Bruno, thanks for reply,
> the goal is to find the hedge ratio of one stock against another. I want to know what beta i should mulptiply by my last x term term to make it someone equivalent to the y term. (this is to know how much of one asset should be bought to be relatively equivalent to another asset. I read in a book that this linear relationship code (without a constant term )is the way to find the hedge ratio. I was not familiar with this method before reading it and i dont fully understand it, but i know a little about the least squares mehtod and think that should tell me the correct beta... so i am just trying to understand how the two methods relate and which method is better and if this book i read is truely correct.
>
> Is it possible that the linear relationship code ( without constant term) factors in the price difference of the assets x and y when calculating the beta or hedge ratio, and maybe the least sqares approach with the alpha term included does not factor in the asset price differences( the difference in price between x and Y). I have done the math and i know this particular theory is not 100% right, but maybe it has something to do with it.
>
> thanks again

Confusing. I don't know what is "hedge ratio". I rather speak math language because it is clear, and no confusion when the problem is described in that language.

If you want to compute the (least-squares) beta WITHOUT constant term, why not use the A matrix as single column x?

A = x;

In that case, six methods on top of my head:

1. QR: [q r]=qr(x,0); beta=r\(q'*y)

2: dumb least-squares: beta = inv(x'*x)*x'*y

3: Pseudo-inverse: beta = pinv(x)*y

4: backslash: beta = x\y

5: direct method: beta: (x'*y)/(x'*x)

6: another variant: dot(x,y)/norm(x)^2

solve the same least-squares problem, and will give the same thing (fortunately).

If you want to see how to use QR in more complex least-squares problem, check out my FEX contribution:

http://www.mathworks.com/matlabcentral/fileexchange/25453-pseudo-inverse

Bruno
From: Sean Douglas on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i22glo$jgb$1(a)fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i22d79$9jh$1(a)fred.mathworks.com>...
>
> > hey Bruno, thanks for reply,
> > the goal is to find the hedge ratio of one stock against another. I want to know what beta i should mulptiply by my last x term term to make it someone equivalent to the y term. (this is to know how much of one asset should be bought to be relatively equivalent to another asset. I read in a book that this linear relationship code (without a constant term )is the way to find the hedge ratio. I was not familiar with this method before reading it and i dont fully understand it, but i know a little about the least squares mehtod and think that should tell me the correct beta... so i am just trying to understand how the two methods relate and which method is better and if this book i read is truely correct.
> >
> > Is it possible that the linear relationship code ( without constant term) factors in the price difference of the assets x and y when calculating the beta or hedge ratio, and maybe the least sqares approach with the alpha term included does not factor in the asset price differences( the difference in price between x and Y). I have done the math and i know this particular theory is not 100% right, but maybe it has something to do with it.
> >
> > thanks again
>
> Confusing. I don't know what is "hedge ratio". I rather speak math language because it is clear, and no confusion when the problem is described in that language.
>
> If you want to compute the (least-squares) beta WITHOUT constant term, why not use the A matrix as single column x?
>
> A = x;
>
> In that case, six methods on top of my head:
>
> 1. QR: [q r]=qr(x,0); beta=r\(q'*y)
>
> 2: dumb least-squares: beta = inv(x'*x)*x'*y
>
> 3: Pseudo-inverse: beta = pinv(x)*y
>
> 4: backslash: beta = x\y
>
> 5: direct method: beta: (x'*y)/(x'*x)
>
> 6: another variant: dot(x,y)/norm(x)^2
>
> solve the same least-squares problem, and will give the same thing (fortunately).
>
> If you want to see how to use QR in more complex least-squares problem, check out my FEX contribution:
>
> http://www.mathworks.com/matlabcentral/fileexchange/25453-pseudo-inverse
>
> Bruno


thanks again bruno,
I have a question that i think could help explain things. Out of my two methods that you looked at, do you know how one relates to the other? Say i ran both methods on the same x and y vector. Then I would have one answer with just a beta and another answer with an alpha and a beta. Is there some way i could mathematically get the beta from one method to be equal to the beta from the other method? ...maybe by manipulating the alpha????
i will look over your contribution, thank you again.
 |  Next  |  Last
Pages: 1 2
Prev: UITree child node expansion
Next: memory usage issue