Merging coefficient in series (Updated) - Not correct mean

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

phoenix1914
Posts: 7
Joined: Sat Nov 17, 2018 1:48 pm

Merging coefficient in series (Updated) - Not correct mean

Postby phoenix1914 » Sun Nov 18, 2018 2:39 am

Hello all

I am new to programming with Eviews 10 so I am struggling a bit with the following case. I managed to create a script to run a monte carlo simulation, in order to "prove" the assumptions of OLS estimators. More specifically I use a population function with two given coefficients (slope and intercept) and 10 given values of the independent variable (X). Also with the following script I create 20 different disturbance term (u) values, with mean zero and variance 4.

Based on the above I obtain 20 different fitted values (y) which then I regress on X, from where I get 20 OLS lines and 20 pairs of coefficients.

What I want is , if there is a quick way to merge all different pairs of coefficients in a series so I can then calculate their means and variances etc? Also can I have a series where it stores the variances of its of the OLS equations? Currently I only have 20 different equations and 20 different coefficient variables with two values each.

EDIT:

I managed to find formulas to store the coefficients and the variance of the residual (which I post below).

Now I have another issue. Based on the experiment, the variance of the residuals based on the different iterations should approximately be close to the "real" value, but when I check the stored values of the object "sevec" which stores the variances of the residuals of each run, the mean value is always 1 less than the actual. E.g. in the code below I want a disturbance term with mean 0 and variance 4, but I get approximately variance 3 even if i run more than 100 iterations.

Is there an error in the formula: series u{!i} = 0 + @sqrt(4)*nrnd ?


Code: Select all

scalar iterations = 51
matrix(2,iterations) mycoefs
vector(iterations) sevec

for !i=1 to iterations
   series u{!i} = 0 + @sqrt(4)*nrnd
   series y{!i} = b1_c + b2_c* x + u{!i}
   equation eq{!i}.ls y{!i} c x
   coef c{!i} = c
   colplace(mycoefs, eq{!i}.@coefs, !i)
   sevec(!i) =  (@stdev(resid))^2
next

matrix mycoefs = @transpose(mycoefs)


Thank you in advance!

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Matt » Mon Nov 19, 2018 2:16 pm

Hello,

I believe you're just observing the consequences of a small sample size. Try increasing the number of points from 10 to, say, 100.

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Mon Nov 19, 2018 3:47 pm

EViews Matt wrote:Hello,

I believe you're just observing the consequences of a small sample size. Try increasing the number of points from 10 to, say, 100.

Hi Matt,

I don't think this is right. Given normal errors and fixed X, E(s^2) is exact in small samples.

There is another possibility: It may be that more Monte Carlos are needed for the mean of s^2 to be estimated well.

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Mon Nov 19, 2018 3:55 pm

Actually, one more possibility: You're looking at something wrong. Is your copy of EViews up-to-date?

I ran the code you posted and got a mean of 3.9938.

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Matt » Tue Nov 20, 2018 9:49 am

Something about this is bothering me. Shouldn't the expected variance of the residuals asymptotically approach (from below) the true variance of the generating process (as written in the original author's script) as the sample size increases? The extreme boundary example is n = 2, where the residual variance is zero, regardless of the generating process. With a sample size of 10, I observe an average that fluctuates around 3.6. This remains consistent if the number of iterations are increased. However, increasing the number of observations pushes the average towards 4.0.

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Tue Nov 20, 2018 10:11 am

EViews Matt wrote:Something about this is bothering me. Shouldn't the expected variance of the residuals asymptotically approach (from below) the true variance of the generating process (as written in the original author's script) as the sample size increases? The extreme boundary example is n = 2, where the residual variance is zero, regardless of the generating process. With a sample size of 10, I observe an average that fluctuates around 3.6. This remains consistent if the number of iterations are increased. However, increasing the number of observations pushes the average towards 4.0.

I don't think so. Under the classic assumptions, s^2 is an unbiased estimator. So there is Monte Carlo error on finding it's mean, but the actual mean doesn't depend on sample size.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Gareth » Tue Nov 20, 2018 1:36 pm

Use @stdevs rather than @stdev
Follow us on Twitter @IHSEViews

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Matt » Tue Nov 20, 2018 2:30 pm

Created a small EViews program (further below) to explore the expected residual variance for different sample sizes. I believe this shows the same numerical phenomenon that underlays the original poster's question.

ExpVar.png
ExpVar.png (7.17 KiB) Viewed 7592 times

The generating program:

Code: Select all

create u 19

!iterations = 10000

series n = @trend + 2
series exp_var

for !i = 1 to @obsrange
   !n = n(!i)
   !v = 0
   pagecreate u !n
   series x = @trend
   for !j = 1 to !iterations
      series y = x + 2 * nrnd
      equation eq.ls y x c
      !v = !v + @vars(resid)
   next
   pagedelete Untitled1
   exp_var(!i) = !v
next
exp_var = exp_var / !iterations
group g n exp_var
show g

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Tue Nov 20, 2018 2:56 pm

I will admit to being puzzled.

A small piece of where I'm puzzled is what we're seeing for exp_var. Isn't that a series 10,000 long?

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Matt » Tue Nov 20, 2018 3:14 pm

No, the series are very short (following the OP's example), only of length n. The 10,000 is the number of iterations for building the expectation (i.e. Monte Carlo runs). So, for example, drawing 3 Y's from N(0, 4), regressing, and calculating the variance of the residuals 10,000 times results in an expected residual variance of about 1.997...

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Tue Nov 20, 2018 3:23 pm

Sorry. I see what you did now.

startz
Non-normality and collinearity are NOT problems!
Posts: 3775
Joined: Wed Sep 17, 2008 2:25 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby startz » Tue Nov 20, 2018 3:30 pm

The answer is that you can't use @vars or @std. Those divide by n-1. The regression has a constant and X. The SSR needs to be divided by n-2. Do that and I think everything will work.

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby EViews Matt » Tue Nov 20, 2018 3:57 pm

Of course, we've lost two degrees of freedom, that's the correction! Adjusting the original author's variance calculation now produces the expected result,

Code: Select all

sevec(!i) =  @sumsq(resid - @mean(resid)) / (@obsrange - 2)

phoenix1914
Posts: 7
Joined: Sat Nov 17, 2018 1:48 pm

Re: Merging coefficient in series (Updated) - Not correct mean

Postby phoenix1914 » Thu Dec 13, 2018 2:11 am

Thank you all, I'm really grateful for all these replies! i didn't respond earlier due to busy schedule and catching up with other units on econometrics, but I believe this is the answer I was looking for.

I will test it and let you know if that works out!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 27 guests