!
! Ascertainment correction, estimating pi
!
G1: Model parameters
 Data Calc NGroups=4
 Begin Matrices;
  H Full 1 1
  Q Full 1 1
  X Lower 1 1 Free
  Y Lower 1 1 Free
  Z Lower 1 1 Free
  W Lower 1 1 
  I Full 1 2
  O Zero 1 2
  P full 1 1 Free ! ascertainment probability p
  U unit 1 1
  T full 1 2
 End Matrices;
 ! parameters are fixed by default, unless declared free 
 Matrix H .5
 Matrix Q .25
 Matrix I 1 3
 Matrix T 1.282 1.282
 Start .6 All
 Bound .01 1 P 1 1
 Begin Algebra;
  A= X*X'; 
  C= Y*Y';
  E= Z*Z';
  D= W*W';
  S= (P) _ (P) _ P*(U+U-P) ; ! Vector of ascertainment probabilities
  L=  A+C+D+E   | H@A+C+Q@D _
      H@A+C+Q@D | A+C+D+E ;  ! DZ cov matrix
  M=  A+C+D+E | A+C+D _ 
      A+C+D   | A+C+D+E ;    ! MZ cov matrix
  J= (P+P)*\mnor(M_O_T_T_I)-P*P*\mnor(M_O_T_T_U|U);
  K= (P+P)*\mnor(L_O_T_T_I)-P*P*\mnor(L_O_T_T_U|U);
 End Algebra:
End

G2: MZ twin pairs
 Data Ninput=4
 Labels T1 T2 Asc Freq
 Ordinal File=mzpi.frq
 Definition Asc Freq ;
 Begin Matrices= Group 1
  F full 1 1 ! Frequency
  G full 1 4 ! to select ascertainment prob
 End Matrices;
 Specify F Freq
 Matrix G 1 1 1 1
 Specify G asc 0 asc 0
 Covariances M;
 Thresholds T ;
 Frequency F ;
 Weight \part(S,G).J ;
 Options RSidual
End 

G3: DZ twin pairs
 Data Ninput=4
 Labels T1 T2 Asc Freq
 Ordinal File=dzpi.frq
 Definition Asc Freq ;
 Begin Matrices= Group 1
  F full 1 1 = F2 ! Frequency
  G full 1 4 = G2 ! To select ascertainment probability
 End Matrices;
 Covariances L;
 Thresholds T ;
 Frequency F ;
 Weight \part(S,G).K ;
 Options RSidual NDecimals=4
End

Group 4: constrain variance to 1
 Constraint NI=1
 Begin Matrices = Group 1 ;
  I unit 1 1
 End Matrices;
 Constraint I = A+C+E+D ;
 Option Multiple issat
End
 Drop @.5 P 1 1 1
End
