proc (2)=theta_2w(x,y,t,n);
/*
theta_2w.src is written by Kristian Jönsson (November 12, 2002), Department of Economics, Lund University.
Contact info:  kristian.jonsson@nek.lu.se

See separate documentation and 'Econometric Analysis of Panel Data' 2nd edition, Baltagi (2001).

Note:
This code can be used freely as long as proper reference is given.
No performance guarantee is made.
Bugreports are welcome.
*/

local Q,P_mu,P_lambda;
local z,k;
local ols_res,u;
local x_tw,y_tw,alfa_fe,beta_fe;
local mu_i, lam_i,sig_mu,sig_lam;
local Q_1,Q_2,Q_3;
local sig_v,sig_1,sig_2;
local lam_1,lam_2,lam_3,lam_4;
local theta_1,theta_2,theta_3;
local theta,lambda;

local temp;


theta=zeros(4,3);
lambda=zeros(4,4);

Q=eye(n).*.eye(t)-eye(n).*.(ones(t,t)/t)-(ones(n,n)/n).*.eye(t)+(ones(n,n)/n).*.(ones(t,t)/t);
P_mu=eye(n).*.ones(t,t)/t;     /* Time average for different individuals */
P_lambda=ones(n,n)/n.*.eye(t);  /* Individual average for different time-periods */ 

z=ones(rows(x),1)~x;
k=cols(z);

/* Wallace and Hussain */

ols_res=y-z*(y/z);
u=ols_res;
Q_1=(eye(n)-ones(n,n)/n).*.(eye(t)-ones(t,t)/t);
Q_2=(eye(n)-ones(n,n)/n).*.(ones(t,t)/t);
Q_3=(ones(n,n)/n).*.(eye(t)-ones(t,t)/t);
sig_v=sqrt((u'*Q_1*u)/sumc(diag(Q_1)));
lam_1=sig_v^2;
sig_1=sqrt((u'*Q_2*u)/sumc(diag(Q_2)));
lam_2=sig_1^2;
sig_2=sqrt((u'*Q_3*u)/sumc(diag(Q_3)));
lam_3=sig_2^2;

lam_4=lam_2+lam_3-lam_1;

theta_1=1-sig_v/sqrt(lam_2); if theta_1<0; theta_1=0; endif;
theta_2=1-sig_v/sqrt(lam_3); if theta_2<0; theta_2=0; endif;
theta_3=theta_1+theta_2+sig_v/sqrt(lam_4)-1; if theta_3<0; theta_3=0; endif;

lambda[1,.]=lam_1~lam_2~lam_3~lam_4;
theta[1,.]=theta_1~theta_2~theta_3;



/* Amemiya */

y_tw=Q*y;
x_tw=Q*x;

beta_fe=y_tw/x_tw;
alfa_fe=meanc(y)-meanc(x)'*beta_fe;

u=y-alfa_fe*ones(rows(y),1)-x*beta_fe;

sig_v=sqrt((u'*Q_1*u)/sumc(diag(Q_1)));
lam_1=sig_v^2;
sig_1=sqrt((u'*Q_2*u)/sumc(diag(Q_2)));
lam_2=sig_1^2;
sig_2=sqrt((u'*Q_3*u)/sumc(diag(Q_3)));
lam_3=sig_2^2;

lam_4=lam_2+lam_3-lam_1;

theta_1=1-sig_v/sqrt(lam_2); if theta_1<0; theta_1=0; endif;
theta_2=1-sig_v/sqrt(lam_3); if theta_2<0; theta_2=0; endif;
theta_3=theta_1+theta_2+sig_v/sqrt(lam_4)-1; if theta_3<0; theta_3=0; endif;

lambda[2,.]=lam_1~lam_2~lam_3~lam_4;
theta[2,.]=theta_1~theta_2~theta_3;


/* Swamy and Arora */

sig_v=sqrt((y'*Q_1*y-y'*Q_1*x*inv(x'*Q_1*x)*x'*Q_1*y)/((n-1)*(t-1)-k+1));

lam_2=(y'*Q_2*y-y'*Q_2*x*inv(x'*Q_2*x)*x'*Q_2*y)/((n-1)-k+1);
sig_mu=sqrt((lam_2-sig_v^2)/t);

lam_3=(y'*Q_3*y-y'*Q_3*x*inv(x'*Q_3*x)*x'*Q_3*y)/((t-1)-k+1);
sig_lam=sqrt((lam_3-sig_v^2)/n);

theta_1=1-sig_v/sqrt(lam_2); if theta_1<0; theta_1=0; endif;
theta_2=1-sig_v/sqrt(lam_3); if theta_2<0; theta_2=0; endif;
theta_3=theta_1+theta_2+sig_v/sqrt(lam_4)-1; if theta_3<0; theta_3=0; endif;

lambda[3,.]=lam_1~lam_2~lam_3~lam_4;
theta[3,.]=theta_1~theta_2~theta_3;


/* Nerlove */

y_tw=Q*y;
x_tw=Q*x;
beta_fe=y_tw/x_tw;
alfa_fe=meanc(y)-meanc(x)'*beta_fe;

mu_i=unique((P_mu*y-meanc(y))-(P_mu*x-meanc(x)')*beta_fe,1);

lam_i=unique((P_lambda*y-meanc(y))-(P_lambda*x-meanc(x)')*beta_fe,1);

u=y_tw-x_tw*beta_fe;
/* u=y-alfa_fe*ones(rows(y),1)-x*beta_fe; */

sig_v=sqrt(u'*u/(n*t));
lam_1=sig_v^2;
sig_mu=sqrt((mu_i)'*(mu_i)/(n-1));
lam_2=t*(sig_mu^2)+sig_v^2;
sig_lam=sqrt((lam_i)'*(lam_i)/(t-1));
lam_3=n*(sig_lam^2)+sig_v^2;

lam_4=lam_2+lam_3-lam_1;

theta_1=1-sig_v/sqrt(lam_2); if theta_1<0; theta_1=0; endif;
theta_2=1-sig_v/sqrt(lam_3); if theta_2<0; theta_2=0; endif;
theta_3=theta_1+theta_2+sig_v/sqrt(lam_4)-1; if theta_3<0; theta_3=0; endif;

lambda[4,.]=lam_1~lam_2~lam_3~lam_4;
theta[4,.]=theta_1~theta_2~theta_3;

retp(theta,lambda);
endp;