Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RollingBeta x and y are mixed up #9

Open
Javdat opened this issue Aug 27, 2020 · 0 comments
Open

RollingBeta x and y are mixed up #9

Javdat opened this issue Aug 27, 2020 · 0 comments

Comments

@Javdat
Copy link

Javdat commented Aug 27, 2020

From function definition:

x | A vector, matrix, list or other object coercible to a matrix; the independent variable
y | A vector, matrix, list or other object coercible to a vector; the dependent variable

But, the results are opposite:

x = rnorm(100)
y = x * 2
RollingBeta(x = x, y = y, window = 50)

expected result 2 (as we have defined y = x*2), actual result: 0.5.

Hence definition of x and y is opposite.

This gives correct results:

RollingBeta(x = y, y = x, window = 50)

I wondered why tests pass. This is the result.
From unit tests:

Beta = function(lhs, rhs) cov(lhs, rhs) / var(rhs)

Tests call it as (x, y) hence lhs = x

run_tests_xy(x, y, window, funcs_xy, trace = TRACE)

The correct formula for simple linear regression is: cov(x,y)/var(x)
Hence (note var(lhs)):

Beta = function(lhs, rhs) cov(lhs, rhs) / var(lhs)

The main culprit:

case BETA : rollxy[i] = cov / var_y; break;

Should be cov/var_x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant