You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.7 KiB
Plaintext

7 months ago
*! version 2 15jan2013
************************************************************************************************************
* 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 (Jean-Benoit Hardouin)
* Version 1.1: June 14, 2012 /*name option*/ (Jean-Benoit Hardouin)
* Version 2: January 15, 2013 /*bivariate normal distribution*/ (Jean-Benoit Hardouin, Mohand-Larbi Feddag, Myriam Blanchin)
*
* Jean-Benoit Hardouin, jean-benoit.hardouin@univ-nantes.fr
* EA 4275 "Biostatistics, Pharmacoepidemiology and Subjectives Measures in Health"
* Faculty of Pharmaceutical Sciences - University of Nantes - France
* http://www.sphere-nantes.org
*
* News about this program : http://anaqol.free.fr
* FreeIRT Project : http://freeirt.free.fr
*
* Copyright 2005, 2013 Jean-Benoit Hardouin, Mohand-Larbi Feddag, Myriam Blanchin
*
* 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 gausshermite,rclass
version 7
syntax anything [, Sigma(real -1) Var(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
tempname mean variance C
qui set obs `=`nodes'*`nodes''
if "`name'"=="" {
if `sigma'!=-1{
if "`var'"==""{
local name x
local nb=1
}
else{
di in red "{p}Please fill in the {hi:name} option{p_end}"
error 198
exit
}
}
else{
if "`var'"!=""{
local name1 x1
local name2 x2
local nb=2
}
else{
di in red "{p}Please fill in the {hi:name} option{p_end}"
error 198
exit
}
}
}
else {
local nb=wordcount("`name'")
if `nb'==2{
local name1=word("`name'",1)
local name2=word("`name'",2)
}
}
if `nb'==2{
capture confirm matrix `var'
if !_rc{
if colsof(`var')==2 & rowsof(`var')==2{
matrix `C'=cholesky(`var')
}
else{
di in red "{p}The covariance matrix in the {hi:var} option should be a 2x2 matrix for a bivariate distribution{p_end}"
error 198
exit
}
}
else{
matrix `variance'=(1,0\0,1)
matrix `C'=cholesky(`variance')
}
}
else{
if `sigma'==-1{
local sig=1
}
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{
if "`mu'"==""{
if `nb'==1{
local `mean'=0
}
else{
matrix `mean'=(0,0)
}
}
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=`sig')d`name'=" in yellow %12.8f `r(sum)'
}
drop _all
if `save'==1 {
qui use `gauss',clear
}
end