

# \begin{code}
arma p q ; y
# \end{code}


# \begin{code}
arma p q ; y --conditional
# \end{code}


# \begin{code}
arma p q ; y const x1 x2
# \end{code}


# \begin{code}
arma p q ; y const x1 x2 --conditional
# \end{code}


# \begin{code}
arma p q ; P Q ; y
# \end{code}


# \begin{code}
arma 1 1 ; 1 1 ; y
# \end{code}


# \begin{code}
matrix pvec = {1, 4}
arma pvec 1 ; y
arma {1 4} 1 ; y
# \end{code}


# \begin{code}
arma p d q ; y 
# \end{code}


# \begin{code}
series tmp = y
loop for i=1..d
  tmp = diff(tmp)
end loop
arma p q ; tmp 
# \end{code}


# \begin{code}
arma p d q ; P D Q ; y 
# \end{code}


# \begin{code}
arma 1 0 0 ; 1 1 1 ; y 
# \end{code}


# \begin{code}
genr dsy = sdiff(y)
arma 1 0 ; 1 1 ; dsy 
# \end{code}


# \begin{code}
open data10-1
arma 1 1 ; r
arma 1 1 ; r --conditional
# \end{code}


# \begin{code}
matrix start = { 0, 0.85, 0.34 }
set initvals start
arma 1 1 ; y
# \end{code}


# \begin{code}
arma p q ; y --x-12-arima
# \end{code}


# \begin{code}
                  y          fc1          fc2       fcdiff
1981:1      7.964086     7.940930      0.02668      0.02668
1981:2      7.978654     7.997576      0.03349      0.03349
1981:3      8.009463     7.997503      0.01885      0.01885
1981:4      8.015625     8.033695      0.02423      0.02423
1982:1      8.014997     8.029698      0.01407      0.01407
1982:2      8.026562     8.046037      0.01634      0.01634
1982:3      8.032717     8.063636      0.01760      0.01760
1982:4      8.042249     8.081935      0.01830      0.01830
1983:1      8.062685     8.100623      0.01869      0.01869
1983:2      8.091627     8.119528      0.01891      0.01891
1983:3      8.115700     8.138554      0.01903      0.01903
1983:4      8.140811     8.157646      0.01909      0.01909
# \end{code}


# \begin{code}
adf 4 x1 --c --ct
# \end{code}


# \begin{code}
kpss m y
# \end{code}


# \begin{code}
kpss n y --trend
# \end{code}


# \begin{code}
garch p q ; y const x
# \end{code}


# \begin{code}
loop control-expression [ --progressive | --verbose | --quiet ]
   loop body
endloop
# \end{code}


# \begin{code}
loop while essdiff > .00001
# \end{code}


# \begin{code}
open greene22_2
open greene22_2
discrete Z8
v8 = values(Z8)
n = rows(v8)
n = rows(v8)
loop for i=1..n
  scalar xi = v8[$i]
  smpl (Z8=xi) --restrict --replace
  printf "mean(Y | Z8 = %g) = %8.5f, sd(Y | Z8 = %g) = %g\n", \
    xi, mean(Y), xi, sd(Y)
end loop
# \end{code}


# \begin{code}
loop foreach i peach pear plum
   print "$i"
endloop
# \end{code}


# \begin{code}
genr time
loop foreach i AL..WY
   ols $i const time
endloop
# \end{code}


# \begin{code}
list ylist = y1 y2 y3
loop foreach i ylist
   ols $i const x1 x2
endloop
# \end{code}


# \begin{code}
loop for (r=0.01; r<.991; r+=.01)
# \end{code}


# \begin{code}
Model1 <- ols Ct 0 Yt
# \end{code}


# \begin{code}
"Model 1" <- ols Ct 0 Yt
# \end{code}


# \begin{code}
Model1.show
# \end{code}


# \begin{code}
"Model 1".show
# \end{code}


# \begin{code}
CrossPlot <- gnuplot Ct Yt
CrossPlot.show
# \end{code}


# \begin{code}
ADF1 <- adf 2 x1
ADF1.show
# \end{code}


# \begin{code}
setobs unitvar timevar --panel-vars
# \end{code}


# \begin{code}
setobs 20 1:1 --stacked-time-series
setobs 5 1:1 --stacked-cross-section
# \end{code}


# \begin{code}
open panel.txt
genr x1 = stack(p1..p5) --length=50
genr x2 = stack(p1..p5) --offset=50 --length=50
setobs 50 1:1 --stacked-cross-section
store panel.gdt x1 x2
# \end{code}


# \begin{code}
genr x = stack(p1,p3,p5)
# \end{code}


# \begin{code}
open panel.txt
loop for i=1..20
  genr k = ($i - 1) * 50
  genr x$i = stack(p1..p5) --offset=k --length=50
endloop
setobs 50 1.01 --stacked-cross-section
store panel.gdt x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 \
  x11 x12 x13 x14 x15 x16 x17 x18 x19 x20
# \end{code}


# \begin{code}
genr time
genr year = 1960 + (5 * time)
genr markers = "%s:%d", marker, year
# \end{code}


# \begin{code}
# Gretl: various illustrative datafiles
"arma","artificial data for ARMA script example"
"ects_nls","Nonlinear least squares example"
"hamilton","Prices and exchange rate, U.S. and Italy"
# \end{code}


# \begin{code}
# Gretl: various sample scripts
"arma","ARMA modeling","artificial data"
"ects_nls","Nonlinear least squares (Davidson)","artificial data"
"leverage","Influential observations","artificial data"
"longley","Multicollinearity","US employment"
# \end{code}


# \begin{code} 
foo = x1 * x2
# \end{code}


# \begin{code}
function function-name(parameters)
   function body
end function
# \end{code}


# \begin{code}
function myfunc(series y, list xvars, bool verbose)
# \end{code}


# \begin{code}
function myfunc(series *y, scalar *b)
# \end{code}


# \begin{code}
int order[1:12:4]
# \end{code} 


# \begin{code}
bool verbose[0]
# \end{code} 


# \begin{code}
function myfunc2(void)
# \end{code}


# \begin{code}
# function definition
function ols_ess(series y, list xvars)
  ols y 0 xvars --quiet
  scalar myess = $ess
  printf "ESS = %g\n", myess
  return scalar myess
end function
# main script
open data4-1
list xlist = 2 3 4
# function call (the return value is ignored here)
ols_ess(price, xlist)
# \end{code}


# \begin{code}
# function definition
function get_uhat(series y, list xvars)
  ols y 0 xvars --quiet
  series uh = $uhat
  return series uh
end function
# main script
open data4-1
list xlist = 2 3 4
# function call
series resid = get_uhat(price, xlist)
# \end{code}


# \begin{code}
function myfunc delete
function get_uhat clear
# \end{code}


# \begin{code}
function triple1(series x)
  series ret = 3*x
  return series ret
end function
  
function triple2(series *x)
  series ret = 3*x
  return series ret
end function
# \end{code}


# \begin{code}
function get_uhat_and_ess(series y, list xvars, scalar *ess)
  ols y 0 xvars --quiet
  ess = $ess
  series uh = $uhat
  return series uh
end function
# main script
open data4-1
list xlist = 2 3 4
# function call
scalar SSR
series resid = get_uhat_and_ess(price, xlist, &SSR)
# \end{code}


# \begin{code}
function get_uhat_and_ess(series y, list xvars, scalar *ess[null])
  ols y 0 xvars --quiet
  if !isnull(ess) 
     ess = $ess
  endif
  series uh = $uhat
  return series uh
end function
# \end{code}


# \begin{code}
series resid = get_uhat_and_ess(price, xlist, null)
# \end{code}


# \begin{code}
Elapsed time: 
	without pointers (copy) = 3.66 seconds,
	with pointers (no copy) = 0.01 seconds.
# \end{code}


# \begin{code}
loop foreach i X
   scalar sd_$i = sd($i)
end loop
# \end{code}


# \begin{code}
function myfunc (scalar y, const list X)
# \end{code}


# \begin{code}
function myfunc (scalar y, list X[null])
# \end{code}


# \begin{code}
function movavg (series y, string vname)
   series @vname_2 = (y+y(-1)) / 2
   series @vname_4 = (y+y(-1)+y(-2)+y(-3)) / 4
   list retlist = @vname_2 @vname_4
   return list retlist
end function

open data9-9
list malist = movavg(nocars, "nocars")
print malist --byobs
# \end{code}


# \begin{code}
function somefun (series y)
# \end{code}


# \begin{code}
function namefun (series y)
  printf "the series given as 'y' was named %s\n", argname(y)
end function

open data9-7
namefun(QNC)
# \end{code}


# \begin{code}
the series given as 'y' was named QNC
# \end{code}


# \begin{code}
return scalar SSR
# \end{code}


# \begin{code}
function make_cubes (list xlist)
   list cubes = null
   loop foreach i xlist --quiet
      series $i3 = $i^3
      setinfo $i3 -d "cube of $i"
      list cubes += $i3
    end loop
    return list cubes
end function

open data4-1
list xlist = price sqft
list cubelist = make_cubes(xlist)
print xlist cubelist --byobs
labels
# \end{code}


# \begin{code}
if nelem(xlist) = 0
   funcerr "xlist must not be empty"
end if
# \end{code}


# \begin{code}
set echo on
set messages on
# \end{code}


# \begin{code}
function pc(series y)
  series foo = diff(y)/y(-1)
  return series foo
end function
# \end{code}


# \begin{code}
genr x = uniform()
genr dpcx = pc(x)
print x dpcx --byobs
# \end{code}


# \begin{code}
include pc.gfn
open np
foo = pc(iprod)
# \end{code}


# \begin{code}
gretlcli -b scriptfile > outputfile
# \end{code}


# \begin{code} 
(* 
  DATA9-6: 
  Data on log(money), log(income) and interest rate from US. 
  Source: Stock and Watson (1993) Econometrica 
  (unsmoothed data) Period is 1900-1989 (annual data). 
  Data compiled by Graham Elliott. 
*) 
lmoney lincome intrate ; 
1 1900 1989 BYOBS
# \end{code}


# \begin{code}
G0M910  Composite index of 11 leading indicators (1987=100) 
M 1948.01 - 1995.11  n = 575
currbal Balance of Payments: Balance on Current Account; SA 
Q 1960.1 - 1999.4 n = 160
# \end{code}


# \begin{code}
# Federal Reserve Board (interest rates)
# \end{code}


# \begin{code}
apt-get install gcc autoconf automake1.9 libtool flex bison gcc-doc \
libc6-dev libc-dev libgfortran1 libgfortran1-dev gettext pkgconfig
# \end{code}


# \begin{code}
cvs -d:pserver:anonymous@gretl.cvs.sourceforge.net:/cvsroot/gretl login
cvs -z3 -d:pserver:anonymous@gretl.cvs.sourceforge.net:/cvsroot/gretl co -P gretl
# \end{code}


# \begin{code}
cvs update -d -P
# \end{code}


# \begin{code}
./configure --help
# \end{code}


# \begin{code}
./configure --prefix=/usr
# \end{code}


# \begin{code}
./configure --enable-build-doc
# \end{code}


# \begin{code}
make 
# \end{code}


# \begin{code}
make install
# \end{code}


# \begin{code}
sudo make install
# \end{code}


# \begin{code}
list xlist = 1 2 3 4
list reglist = income price 
# \end{code}


# \begin{code}
list xlist = x1 x2 x3 x4
ols y 0 xlist
# \end{code}


# \begin{code}
list xlist = 1 2 3
xlist = 4 5 6
# \end{code}


# \begin{code}
list xlist = xlist 5 6 7
xlist = 9 10 xlist 11 12
# \end{code}


# \begin{code}
xlist += cpi
# \end{code}


# \begin{code}
xlist -= cpi
# \end{code}


# \begin{code}
list L3 = L1 || L2
# \end{code}


# \begin{code}
list L3 = L1 && L2
# \end{code}


# \begin{code}
matrix m = {1,2,3,4}
list L = m
# \end{code}


# \begin{code}
series xl1 = log(x1)
series xl2 = log(x2)
list xlogs = xl1 xl2
genr is1 = islist(xlogs)
genr is2 = islist(xl1)
# \end{code}


# \begin{code}
list xlist = 1 2 3
nl = nelem(xlist)
# \end{code}


# \begin{code}
? list xlist = x1 x2 x3
Added list 'xlist'
? xlist
 x1 x2 x3
# \end{code}


# \begin{code}
list xlist = x1 x2 x3
list lxlist = log(xlist)
list difflist = diff(xlist)
# \end{code}


# \begin{code}
list xlist = x1 x2 x3
list laglist = lags(2, xlist)
# \end{code}


# \begin{code}
scalar order = 4
list laglist = lags(order, xlist)
# \end{code}


# \begin{code}
list xlist = x1 x2 x3
series xok = ok(xlist)
# \end{code}


# \begin{code}
string s1 = "some stuff I want to save"
string s2 = getenv("HOME")
string s3 = s1 + 11
# \end{code}


# \begin{code}
string s1 = "some stuff I want to "
string s1 += "save"
# \end{code}


# \begin{code}
scalar x = 8
sprintf foo "var%d", x
# \end{code}


# \begin{code}
? scalar x = 8
 scalar x = 8
Generated scalar x (ID 2) = 8
? sprintf foo "var%d", x
Saved string as 'foo'
? print "@foo"
var8
# \end{code}


# \begin{code}
? print @foo
# \end{code}


# \begin{code}
print var8
# \end{code}


# \begin{code}
string vstr = "variance"
Generated string vstr
printf "vstr: %12s\n", vstr
vstr:     variance
# \end{code}


# \begin{code}
? string vstr_copy = vstr
# \end{code}


# \begin{code}
? string user = getenv("USER")
Saved string as 'user'
? string home = getenv("HOME")
Saved string as 'home'
? print "@user's home directory is @home"
cottrell's home directory is /home/cottrell
# \end{code}


# \begin{code}
? string temp = getenv("TEMP")
Saved empty string as 'temp'
? scalar x = strlen(temp)
Generated scalar x (ID 2) = 0
# \end{code}


# \begin{code}
? string hw = "hello world"
Saved string as 'hw'
? string w = strstr(hw, "o")
Saved string as 'w'
? print "@w"
o world
# \end{code}


# \begin{code}
  coint2 p ylist [ ; xlist [ ; zlist ] ]
# \end{code}


# \begin{code}
  vecm p r ylist [ ; xlist [ ; zlist ] ]
# \end{code}


# \begin{code}
open denmark
coint2 2 LRM LRY IBO IDE --rc --seasonal
# \end{code}


# \begin{code}
Johansen test:
Number of equations = 4
Lag order = 2
Estimation period: 1974:3 - 1987:3 (T = 53)

Case 2: Restricted constant
Rank Eigenvalue Trace test p-value   Lmax test  p-value
   0    0.43317     49.144 [0.1284]     30.087 [0.0286]
   1    0.17758     19.057 [0.7833]     10.362 [0.8017]
   2    0.11279     8.6950 [0.7645]     6.3427 [0.7483]
   3   0.043411     2.3522 [0.7088]     2.3522 [0.7076]
# \end{code}


# \begin{code}
open money.gdt 
smpl 1954:1 1994:4 
vecm 6 2 m infl cpr y tbr --rc
# \end{code}


# \begin{code}
Maximum likelihood estimates, observations 1954:1-1994:4 (T = 164)
Cointegration rank = 2
Case 2: Restricted constant

beta (cointegrating vectors, standard errors in parentheses)

m           1.0000       0.0000 
           (0.0000)     (0.0000) 
infl        0.0000       1.0000 
           (0.0000)     (0.0000) 
cpr        0.56108      -24.367 
          (0.10638)     (4.2113) 
y         -0.40446     -0.91166 
          (0.10277)     (4.0683) 
tbr       -0.54293       24.786 
          (0.10962)     (4.3394) 
const      -3.7483       16.751 
          (0.78082)     (30.909) 
# \end{code}


# \begin{code}
restrict
  b[1,1] = -1
  b[1,3] = 0
  b[2,1] = 0
  b[2,3] = -1
end restrict
# \end{code}


# \begin{code}
Cointegrating vectors (standard errors in parentheses)

m          -1.0000       0.0000 
           (0.0000)     (0.0000) 
infl     -0.023026     0.041039 
        (0.0054666)   (0.027790) 
cpr         0.0000      -1.0000 
           (0.0000)     (0.0000) 
y          0.42545    -0.037414 
         (0.033718)    (0.17140) 
tbr      -0.027790       1.0172 
        (0.0045445)   (0.023102) 
const       3.3625      0.68744 
          (0.25318)     (1.2870) 
# \end{code}


# \begin{code}
  b[1,1] = 1.618
  b[1,4] + 2*b[2,5] = 0
  a[1,3] = 0
  a[1,1] - a[1,2] = 0
# \end{code}


# \begin{code}
restrict
  b1 + b2 = 0
end restrict
# \end{code}


# \begin{code}
restrict
  a3 = 0
  a4 = 0
end restrict
# \end{code}


# \begin{code}
matrix I4 = I(4)
matrix vR = I4**(I4~zeros(4,1))
matrix vq = mshape(I4,16,1)
restrict
  R = vR
  q = vq
end restrict
# \end{code}


# \begin{code}
open denmark.gdt
vecm 2 1 LRM LRY IBO IDE --rc --seasonals

matrix BA = {1, -1, 6, -6, -6, -0.2, 0.1, 0.02, 0.03}
set initvals BA
restrict
  b[1] = 1
  b[1] + b[2] = 0
  b[3] + b[4] = 0
end restrict
# \end{code}


# \begin{code}
open denmark.gdt
vecm 2 1 LRM LRY IBO IDE --rc --seasonals

matrix phi = {-8, -6}
set initvals phi
restrict --lbfgs
  b[1] = 1
  b[1] + b[2] = 0
  b[3] + b[4] = 0
end restrict
# \end{code}


# \begin{code}
restrict
  b[1] = 1
  b[1] + b[2] = 0
end restrict
# \end{code}


# \begin{code}

Model 1: Logit estimates using the 32 observations 1-32
Dependent variable: GRADE

      VARIABLE       COEFFICIENT        STDERROR      T STAT       SLOPE
                                                                  (at mean)
  const               -13.0213           4.93132      -2.641
  GPA                   2.82611          1.26294       2.238      0.533859   
  TUCE                  0.0951577        0.141554      0.672      0.0179755  
  PSI                   2.37869          1.06456       2.234      0.449339   

  Mean of GRADE = 0.344
  Number of cases 'correctly predicted' = 26 (81.2%)
  f(beta'x) at mean of independent vars = 0.189
  McFadden's pseudo-R-squared = 0.374038
  Log-likelihood = -12.8896
  Likelihood ratio test: Chi-square(3) = 15.4042 (p-value 0.001502)
  Akaike information criterion (AIC) = 33.7793
  Schwarz Bayesian criterion (BIC) = 39.6422
  Hannan-Quinn criterion (HQC) = 35.7227

           Predicted
             0    1
  Actual 0  18    3
         1   3    8

Model 2: Probit estimates using the 32 observations 1-32
Dependent variable: GRADE

      VARIABLE       COEFFICIENT        STDERROR      T STAT       SLOPE
                                                                  (at mean)
  const                -7.45232          2.54247      -2.931
  GPA                   1.62581          0.693883      2.343      0.533347   
  TUCE                  0.0517288        0.0838903     0.617      0.0169697  
  PSI                   1.42633          0.595038      2.397      0.467908   

  Mean of GRADE = 0.344
  Number of cases 'correctly predicted' = 26 (81.2%)
  f(beta'x) at mean of independent vars = 0.328
  McFadden's pseudo-R-squared = 0.377478
  Log-likelihood = -12.8188
  Likelihood ratio test: Chi-square(3) = 15.5459 (p-value 0.001405)
  Akaike information criterion (AIC) = 33.6376
  Schwarz Bayesian criterion (BIC) = 39.5006
  Hannan-Quinn criterion (HQC) = 35.581

           Predicted
             0    1
  Actual 0  18    3
         1   3    8

# \end{code}


# \begin{code}
tobit depvar indvars
# \end{code}


# \begin{code}
heckit y X ; d Z
# \end{code}


# \begin{code}
/* multi-line comment
   # hello
   # hello */
# \end{code}


# \begin{code}
# single-line comment /* hello
# \end{code}


# \begin{code}
ols 1 0 2 3 # estimate the baseline model
# \end{code}


# \begin{code}
/*
# let's skip this for now
ols 1 0 2 3 4
omit 3 4
*/
# \end{code}


# \begin{code}
/* initialize stuff */
series e = 0
scalar beta = 0
matrix V = I(1)

/* proceed with estimation */
gmm 
  series e = y - x*beta
  orthog e ; x
  weights V
  params beta
end gmm
# \end{code}


# \begin{code}
  orthog x ; Z
# \end{code}


# \begin{code}
Model 1: TSLS estimates using the 48 observations 1-48
Dependent variable: lpackpc
Instruments: rtaxso rtax 
Heteroskedasticity-robust standard errors, variant HC0

      VARIABLE       COEFFICIENT        STDERROR      T STAT   P-VALUE

  const                 9.89496          0.928758     10.654  <0.00001 ***
  lravgprs             -1.27742          0.241684     -5.286  <0.00001 ***
  lperinc               0.280405         0.245828      1.141   0.25401

Model 2: 1-step GMM estimates using the 48 observations 1-48
e = lpackpc - b0 - b1*lravgprs - b2*lperinc

      PARAMETER       ESTIMATE          STDERROR      T STAT   P-VALUE

  b0                    9.89496          0.928758     10.654  <0.00001 ***
  b1                   -1.27742          0.241684     -5.286  <0.00001 ***
  b2                    0.280405         0.245828      1.141   0.25401

  GMM criterion = 0.0110046
# \end{code}


# \begin{code}
set force_hc on
# \end{code}


# \begin{code}
# \documentclass[11pt]{article}
# \usepackage[latin1]{inputenc} %% but see below
# \usepackage{amsmath}
# \usepackage{dcolumn,longtable}
# \begin{document}
# \thispagestyle{empty}
# \end{code}


# \begin{code}
--format="%.4f|%.4f|%.4f|%.4f"
--format="%.4f|%.4f|%.3f|"
--format="%.5f|%.4f||%.4f"
--format="%.8g|%.8g||%.4f"
# \end{code}


# \begin{code}
# \usepackage{ucs}
# \usepackage[utf8x]{inputenc}
# \end{code}


# \begin{code}
list xlist = x1 x2 x3
discrete z1 xlist z2
# \end{code}


# \begin{code}
discrete foo
# now foo is discrete
discrete foo --reverse
# now foo is continuous
# \end{code}


# \begin{code}
nulldata 100
# generate a variable with mean 2 and variance 1
genr x = normal() + 2
# split into 4 classes
genr z = (x>0) + (x>2) + (x>4)
# now declare z as discrete
discrete z
# \end{code}


# \begin{code}
open greene22_2
discrete Z5 # mark Z5 as discrete
dummify Z5
# \end{code}


# \begin{code}
list dlist = dummify(x)
# \end{code}


# \begin{code}
open greene22_2
discrete Z5 # mark Z5 as discrete
list foo = dummify(Z5)
loop foreach i foo
  smpl $i --restrict --replace
  summary Y
end loop
smpl full
# \end{code}


# \begin{code}
open greene22_2
discrete Z5 # mark Z5 as discrete
ols Y 0 dummify(Z5)
# \end{code}


# \begin{code}
open greene19_1
freq TUCE
discrete TUCE # mark TUCE as discrete
freq TUCE
# \end{code}


# \begin{code}
Read datafile /usr/local/share/gretl/data/greene/greene19_1.gdt
periodicity: 1, maxobs: 32,
observations range: 1-32

Listing 5 variables:
  0) const    1) GPA      2) TUCE     3) PSI      4) GRADE  

? freq TUCE

Frequency distribution for TUCE, obs 1-32
number of bins = 7, mean = 21.9375, sd = 3.90151

       interval          midpt   frequency    rel.     cum.

          <  13.417     12.000        1      3.12%    3.12% *
    13.417 - 16.250     14.833        1      3.12%    6.25% *
    16.250 - 19.083     17.667        6     18.75%   25.00% ******
    19.083 - 21.917     20.500        6     18.75%   43.75% ******
    21.917 - 24.750     23.333        9     28.12%   71.88% **********
    24.750 - 27.583     26.167        7     21.88%   93.75% *******
          >= 27.583     29.000        2      6.25%  100.00% **

Test for null hypothesis of normal distribution:
Chi-square(2) = 1.872 with p-value 0.39211
? discrete TUCE # mark TUCE as discrete
? freq TUCE

Frequency distribution for TUCE, obs 1-32

          frequency    rel.     cum.

  12           1      3.12%    3.12% *
  14           1      3.12%    6.25% *
  17           3      9.38%   15.62% ***
  19           3      9.38%   25.00% ***
  20           2      6.25%   31.25% **
  21           4     12.50%   43.75% ****
  22           2      6.25%   50.00% **
  23           4     12.50%   62.50% ****
  24           3      9.38%   71.88% ***
  25           4     12.50%   84.38% ****
  26           2      6.25%   90.62% **
  27           1      3.12%   93.75% *
  28           1      3.12%   96.88% *
  29           1      3.12%  100.00% *

Test for null hypothesis of normal distribution:
Chi-square(2) = 1.872 with p-value 0.39211
# \end{code}


# \begin{code}
xtab ylist ; xlist
# \end{code}


# \begin{code}
xtab xlist
# \end{code}


# \begin{code}
open greene22_2
discrete Z* # mark Z1-Z8 as discrete
xtab Z1 Z4 ; Z5 Z6
# \end{code}


# \begin{code}
Cross-tabulation of Z1 (rows) against Z5 (columns)

       [   1][   2][   3][   4][   5]  TOT.
  
[   0]    20    91    75    93    36    315
[   1]    28    73    54    97    34    286

TOTAL     48   164   129   190    70    601

Pearson chi-square test = 5.48233 (4 df, p-value = 0.241287)

Cross-tabulation of Z1 (rows) against Z6 (columns)

       [   9][  12][  14][  16][  17][  18][  20]  TOT.
  
[   0]     4    36   106    70    52    45     2    315
[   1]     3     8    48    45    37    67    78    286

TOTAL      7    44   154   115    89   112    80    601

Pearson chi-square test = 123.177 (6 df, p-value = 3.50375e-24)

Cross-tabulation of Z4 (rows) against Z5 (columns)

       [   1][   2][   3][   4][   5]  TOT.
  
[   0]    17    60    35    45    14    171
[   1]    31   104    94   145    56    430

TOTAL     48   164   129   190    70    601

Pearson chi-square test = 11.1615 (4 df, p-value = 0.0248074)

Cross-tabulation of Z4 (rows) against Z6 (columns)

       [   9][  12][  14][  16][  17][  18][  20]  TOT.
  
[   0]     1     8    39    47    30    32    14    171
[   1]     6    36   115    68    59    80    66    430

TOTAL      7    44   154   115    89   112    80    601

Pearson chi-square test = 18.3426 (6 df, p-value = 0.0054306)
# \end{code}


# \begin{code}
matrix A = { 1, 2, 3 ; 4, 5, 6 }
# \end{code}


# \begin{code}
matrix A = { x1, x2, x3 }
# \end{code}


# \begin{code}
matrix A = { x, x^2 }
# \end{code}


# \begin{code}
list xlist = x1 x2 x3
matrix A = { xlist }
# \end{code}


# \begin{code}
matrix A = { dataset }
# \end{code}


# \begin{code}
matrix A = {const}~{dataset}
# \end{code}


# \begin{code}
matrix A = {}
# \end{code}


# \begin{code}
matrix B = A[1,]
matrix B = A[2:3,3:5]
matrix B = A[2,2]
matrix idx = { 1, 2, 6 }
matrix B = A[idx,]
# \end{code}


# \begin{code}
matrix A = { 1, 2, 3; 4, 5, 6; 7, 8, 9 }
matrix B = A[1:2,2:3]
# \end{code}


# \begin{code}
matrix A = { 1, 2, 3; 4, 5, 6; 7, 8, 9 }
matrix A[2:3,2:3] = I(2)
matrix d = { 1, 1, 1 }
matrix A[diag] = d
# \end{code}


# \begin{code}
matrix C = A + k
matrix D = A - k
# \end{code}


# \begin{code}
matrix C = X'Y
# \end{code}


# \begin{code}
matrix C = A/B
# \end{code}


# \begin{code}
matrix C = A .* B
# \end{code}


# \begin{code}
matrix Y = X .* v
# \end{code}


# \begin{code}
matrix C = A ~ B
# \end{code}


# \begin{code}
matrix C = A | B
# \end{code}


# \begin{code}
matrix B = sqrt(A)
# \end{code}


# \begin{code}
matrix a = mnormal(2,3)
a
matrix b = mshape(a,3,1)
b
matrix b = mshape(a,5,2)
b
# \end{code}


# \begin{code}
?   a
a

      1.2323      0.99714     -0.39078
     0.54363      0.43928     -0.48467

?   matrix b = mshape(a,3,1)
Generated matrix b
?   b
b

      1.2323
     0.54363
     0.99714

?   matrix b = mshape(a,5,2)
Replaced matrix b
?   b
b

      1.2323     -0.48467
     0.54363       1.2323
     0.99714      0.54363
     0.43928      0.99714
    -0.39078      0.43928
# \end{code}


# \begin{code}
B = qform(A, X)
# \end{code}


# \begin{code}
matrix B = func(A, &C)
# \end{code}


# \begin{code}
matrix V
matrix E = eigensym(M, &V)
matrix E = eigensym(M, null)
# \end{code}


# \begin{code}
matrix R
matrix Q = qrdecomp(M, &R)
matrix Q = qrdecomp(M, null)
# \end{code}


# \begin{code}
matrix B = func(A, &C, &D)
# \end{code}


# \begin{code}
matrix s = svd(A, &U, &V)
matrix s = svd(A, null, null)
matrix s = svd(A, null, &V)
# \end{code}


# \begin{code}
matrix s = svd(A, &U, &V)
matrix B = (U.*s)*V
# \end{code}


# \begin{code}
matrix B = mols(Y, X, &U)
# \end{code}


# \begin{code}
  matrix A = {}
  matrix B = A ~ X
# \end{code}


# \begin{code}
matrix u = $uhat
matrix b = m1.$coeff
matrix v2 = m1.$vcv[1:2,1:2]
# \end{code}


# \begin{code}
matrix Pi = $jalpha * $jbeta'
# \end{code}


# \begin{code}
scalar x = 3
matrix x = ones(2,2) # wrong!
# \end{code}


# \begin{code}
scalar x = 3
delete x
matrix x = ones(2,2) # OK
# \end{code}


# \begin{code}
series s = x
series u1 = U[,1]
# \end{code}


# \begin{code}
matrix M = { listname }
# \end{code}


# \begin{code}
matrix M = listname
# \end{code}


# \begin{code}
list Xl = M
# \end{code}


# \begin{code}
delete M
# \end{code}


# \begin{code}
matrix M = mnormal(100,2)
M
print M
# \end{code}


# \begin{code}
set hc_version 2
# \end{code}


# \begin{code}
set hac_kernel parzen
set hac_kernel qs
set hac_kernel bartlett
# \end{code}


# \begin{code}
set hac_lag nw2
# \end{code}


# \begin{code}
set hac_lag 6
# \end{code}


# \begin{code}
set qs_bandwidth 3.5
# \end{code}


# \begin{code}
set hac_prewhiten on
# \end{code}


# \begin{code}
set hac_lag nw3
# \end{code}


# \begin{code}
set pcse on
# \end{code}


# \begin{code}
set pcse off
# \end{code}


# \begin{code}
smpl 1970:1 1979:4
# \end{code}


# \begin{code}
smpl ; 2000:4
# \end{code}


# \begin{code}
smpl 1970:1 2003:4
smpl ; 2000:4
# \end{code}


# \begin{code}
smpl +1 ;
# \end{code}


# \begin{code}
smpl +2 -1
# \end{code}


# \begin{code}
smpl gender=0 --restrict
# \end{code}


# \begin{code}
smpl income>50000 --restrict
# \end{code}


# \begin{code}
smpl --full
# \end{code}


# \begin{code}
smpl income>50000 --restrict --replace
# \end{code}


# \begin{code}
smpl year=1995 --restrict
# \end{code}


# \begin{code}
smpl firm=3 --restrict
# \end{code}


# \begin{code}
smpl 100 --random
# \end{code}


# \begin{code}
genr Y = fracdiff(X,0.5)
# \end{code}


# \begin{code}
genr ct = hpfilt(yt)
genr gt = yt - ct
# \end{code}


# \begin{code}
set bkbp_limits 18 96
set bkbp_k 24
# \end{code}


# \begin{code}
genr index 
genr dum = ((index-1) % 30) = 0
# \end{code}


# \begin{code}
genr pmx = pmean(x)
# \end{code}


# \begin{code}
smpl (psd(x) > 0) --restrict
# \end{code}


# \begin{code}
genr xr = resample(x)
# \end{code}


# \begin{code}
? genr p1 = cdf(X, 50, 0.9)
Generated scalar p1 (ID 2) = 8.94977e-35
? genr p2 = pvalue(X, 50, 0.9)
Generated scalar p2 (ID 3) = 1
? genr test = 1 - p2
Generated scalar test (ID 4) = 0
# \end{code}


# \begin{code}
? genr p1 = cdf(X, 50, 30)
Generated scalar p1 (ID 2) = 0.0111648
? genr p2 = pvalue(X, 50, 30)
Generated scalar p2 (ID 3) = 0.988835
? genr test = 1 - p2
Generated scalar test (ID 4) = 0.0111648
# \end{code}


# \begin{code}
genr nmiss_x = sum(missing(x))
# \end{code}


# \begin{code}
genr time
genr x0 = min(zeromiss(time * ok(x)))
# \end{code}


# \begin{code}
? adf 4 x1 --c
Augmented Dickey-Fuller tests, order 4, for x1
sample size 59
unit-root null hypothesis: a = 1
  test with constant
  model: (1 - L)y = b0 + (a-1)*y(-1) + ... + e
  estimated value of (a - 1): -0.216889
  test statistic: t = -1.83491
  asymptotic p-value 0.3638
P-values based on MacKinnon (JAE, 1996)
? genr pv = $pvalue
Generated scalar pv (ID 13) = 0.363844
? label pv    
  pv=Dickey-Fuller pvalue (scalar)
# \end{code}


# \begin{code}
matrix X = { dataset }
matrix theta = { 1, 100 }'
scalar J = BFGSmax(theta, ObjFunc(&theta, &X))
# \end{code}


# \begin{code}
function ObjFunc (matrix *theta, matrix *X)
  scalar val = ...  # do some computation
  return scalar val
end function
# \end{code}


# \begin{code}
matrix Jac = fdjac(theta, SumOC(&theta, &X))
# \end{code}


# \begin{code}
function SumOC (matrix *theta, matrix *X)
  matrix V = ...  # do some computation
  return matrix V
end function
# \end{code}


# \begin{code}
x1 = { 1 ; 2 ; 3 }
# perform the transform
f = fft(a)
# perform the inverse transform
x2 = ffti(f)
# \end{code}


# \begin{code}
# define the two polynomials
a = { 1, 0.5, 0, 0 }'
b = { 1, 0.3, -0.8, 0 }'
# perform the transforms
fa = fft(a)
fb = fft(b)
# complex-multiply the two transforms 
fc = cmult(fa, fb)
# compute the coefficients of c via the inverse transform
c = ffti(fc)
# \end{code}


# \begin{code}
# define the two polynomials
a = { 1 ; 0.5; 0 ; 0 }
b = { 1 ; 0.3 ; -0.8 ; 0 }
# perform the transforms jointly
f = fft(a ~ b)
# complex-multiply the two transforms 
fc = cmult(f[,1:2], f[,3:4])
# compute the coefficients of c via the inverse transform
c = ffti(fc)
# \end{code}


# \begin{code}
genr unitdum
ols y x du_*
# \end{code}


# \begin{code}
font = Times-Roman
fontsize = 16
max = 4.0
min = 0
width = 400
height = 448
numbers = %3.2f
outliers = true
# \end{code}


# \begin{code}
salary (GENDER=1) salary (GENDER=0)
# \end{code}


# \begin{code}
scalar alpha = 1
scalar p = 1

mle logl =  p*ln(alpha * x) - lngamma(p) - ln(x) - alpha * x 
end mle 
# \end{code}


# \begin{code}
alpha = 1
p = 1
# \end{code}


# \begin{code}
scalar alpha = 1
scalar p = 1

mle logl =  p*ln(ax) - lngamma(p) - ln(x) - ax 
series ax = alpha*x
params alpha p
end mle 
# \end{code}


# \begin{code}
scalar m = mean(x)
scalar alpha = m/var(x)
scalar p = m*alpha

mle logl =  p*ln(ax) - lngamma(p) - ln(x) - ax 
series ax = alpha*x
params alpha p
end mle 
# \end{code}


# \begin{code}
scalar m = mean(x)
scalar alpha = m/var(x)
scalar p = m*alpha

mle logl = check ? p*ln(ax) - lngamma(p) - ln(x) - ax : NA
  series ax = alpha*x
  scalar check = (alpha>0) & (p>0)
params alpha p
end mle 
# \end{code}


# \begin{code}
open djclose

series y = 100*ldiff(djclose)

scalar mu = 0.0
scalar omega = 1
scalar alpha = 0.4
scalar beta = 0.0

mle ll = -0.5*(log(h) + (e^2)/h)
  series e = y - mu
  series h = var(y)
  series h = omega + alpha*(e(-1))^2 + beta*h(-1)
  params mu omega alpha beta
end mle
# \end{code}


# \begin{code}
nulldata 1000

genr x1 = normal()
genr x2 = normal()
genr x3 = normal()

genr ystar = x1 + x2 + x3 + normal()
genr y = (ystar > 0)

scalar b0 = 0
scalar b1 = 0
scalar b2 = 0
scalar b3 = 0

mle logl = y*ln(P) + (1-y)*ln(1-P)
  series ndx = b0 + b1*x1 + b2*x2 + b3*x3
  series P = cnorm(ndx)
  params b0 b1 b2 b3
end mle --verbose
# \end{code}


# \begin{code}
mle logl = y*ln(P) + (1-y)*ln(1-P)
  series ndx = b0 + b1*x1 + b2*x2 + b3*x3
  series P = cnorm(ndx)
  series tmp = dnorm(ndx)*(y/P - (1-y)/(1-P))
  deriv b0 = tmp
  deriv b1 = tmp*x1
  deriv b2 = tmp*x2
  deriv b3 = tmp*x3
end mle --verbose
# \end{code}


# \begin{code}
series tmp = dnorm(ndx)*(y/P - (1-y)/(1-P))
print tmp
# \end{code}


# \begin{code}
setobs 20 9:1 --special
# \end{code}


# \begin{code}
setobs 1 1 --cross-section
genr sortkey = -obs
dataset sortby sortkey
setobs 1 1950 --time-series
# \end{code}


# \begin{code}
genr x = sortby(-obs, x)
# \end{code}


# \begin{code}
list X = x1 x2 x3
genr sel = ok(X)
smpl sel --restrict
# \end{code}


# \begin{code}
matrix vd = values(d)
m = rows(vd)
loop for i=1..m
  scalar sel = vd[i]
  smpl (d=sel) --restrict --replace
  ols y const x
end loop
smpl full
# \end{code}


# \begin{code}
  genr d = (t="1984:2")
# \end{code}


# \begin{code}
  genr DIta = (t="Italy")
# \end{code}


# \begin{code}
alpha = 0.9
theta = -0.5
series e = normal()
series y = 0
series y = alpha * y(-1) + e + theta * e(-1)
# \end{code}


# \begin{code}
series y = d ? x : z
# \end{code}


# \begin{code}
list X = x1 x2 x3
list Z = z1 z2
list dZ = null
loop foreach i Z
  series d$i = d * $i
  list dZ = dZ d$i
end loop 

ols y X Z d dZ
# \end{code} 


# \begin{code}
smpl full
genr time
genr minute = int(time/60) + 1
genr second = time % 60
setobs minute second --panel
genr rv = psd(y)^2
setobs 1 1
smpl second=1 --restrict
store foo rv
# \end{code}


# \begin{code}
ols C 0 Y
genr alpha = $coeff(0)
genr beta = $coeff(Y)
genr gamma = 1
# \end{code}


# \begin{code}
ols y 0 x1 x2
genr alpha = $coeff(0)
genr beta = $coeff(x1)
# \end{code}


# \begin{code}
set nls_toler .0001
# \end{code}
