*! version 2  11dec2012
************************************************************************************************************
* gausshermite : Estimate an integral of the form |f(x)g(x/mu,sigma)dx or f(x,y)g(x,y/mu,Sigma)dxdy where g(x/mu,sigma) is the distribution function
* of the gaussian distribution of mean mu and variance sigma^2 and g(x,y/mu,Sigma) is the distribution function
* of the bivariate normal distribution of mean mu and covariance matrix Sigma by Gauss Hermite quadratures
*
* Version 1: May 5, 2005
* Version 1.1: June 14, 2012 /*name option*/
* Version 2: December 11, 2012 /*bivariate normal distribution*/
*
* Jean-benoit Hardouin, Regional Health Observatory of Orléans - France
* jean-benoit.hardouin@orscentre.org
*
* News about this program : http://anaqol.free.fr
* FreeIRT Project : http://freeirt.free.fr
*
* Copyright 2005, 2012 Jean-Benoit Hardouin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
************************************************************************************************************


program define gausshermite3,rclass
version 7
syntax anything [, Sigma(string) MU(string) Nodes(integer 12) Display Name(string)]
tempfile gauss
qui capture save `gauss',replace
local save=0
if _rc==0 {
   qui save `gauss',replace
   local save=1
}

tokenize `anything'

drop _all
qui set obs `=`nodes'*`nodes''
if "`name'"=="" {
	local name x
	local nb=1
}
else {
	local nb=wordcount("`name'")
	if `nb'==2{
	di word("`name'",1)
		local name1=word("`name'",1)
		local name2=word("`name'",2)
	}
}

capture confirm matrix `sigma'
if !_rc{
	if colsof(`sigma')==1 & rowsof(`sigma')==1{
		local sig=`sigma'[1,1]
	}
	else{
		matrix C=cholesky(`sigma')
	}
}
else{
	local sig=`sigma'
}

capture confirm matrix `mu'
if !_rc{
	if colsof(`mu')==1 & rowsof(`mu')==1{
		local mean=`mu'[1,1]
	}
	else{
		matrix mean=`mu'
	}
}
else{
	local mean=`mu'
}

tempname noeuds poids
qui ghquadm `nodes' `noeuds' `poids'

if `nb'==1{
	qui gen `name'=.
	qui gen poids=.
	forvalues i=1/`nodes' {
	   qui replace `name'=`noeuds'[1,`i'] in `i'
	   qui replace poids=`poids'[1,`i'] in `i'
	}
	qui replace `name'=`name'*(sqrt(2)*`sig')+`mean'
	qui gen f=poids/sqrt(_pi)*(`1')
	*list `name' poids f in 1/5
}
else{
	forvalues i=1/`nb'{
		qui gen `name`i''=.
		qui gen poids`i'=.
	}
	local line=1
	forvalues i=1/`nodes' {
		forvalues j=1/`nodes' {
			qui replace `name1'=`noeuds'[1,`i'] *(sqrt(2)*C[1,1])+mean[1,1] in `line'
			qui replace `name2'=`noeuds'[1,`i'] *(sqrt(2)*C[2,1])+`noeuds'[1,`j'] *(sqrt(2)*C[2,2])+mean[1,2] in `line'
			qui replace poids1=`poids'[1,`i'] in `line'
			qui replace poids2=`poids'[1,`j'] in `line'
			local ++line
		}
	}
	qui gen f=poids1*poids2*(`1')/(_pi)
	*list `name1' `name2' poids1 poids2 f in 10/20
}

qui su f
return scalar int=r(sum)
if "`display'"!="" {
   di in green "int_R (`1')g(`name'/sigma=`sigma')d`name'=" in yellow %12.8f `r(sum)'
}
drop _all
if `save'==1 {
   qui use `gauss',clear
}
end