*! version 2.2  09march2007
*! Jean-Benoit Hardouin
************************************************************************************************************
* Gammasym : Symmetric gamma function
*
* Historic:
* Version 1 (2004-01-29): Jean-Benoit Hardouin
* Version 2 (2004-02-01): Jean-Benoit Hardouin
* Version 2.1 (2005-04-04) : Jean-benoit Hardouin
* Version 2.2 (2007-03-09) : Jean-benoit Hardouin /*Weights*/
*
* Jean-benoit Hardouin, University of Nantes - France
* jean-benoit.hardouin@univ-nantes.fr
*
* News about this program : http://www.anaqol.org
* FreeIRT Project : http://www.freeirt.org
*
* Copyright 2004-2007 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 gammasym,rclass
version 7
syntax anything [, Weight(string) DISplay]

local B `weight'

if "`B'"!="" {
   numlist "`B'"
   local B=r(numlist)
   local nbw:word count `B'
   local score=0
   forvalues i=1/`nbw' {
      local B`i':word `i' of `B'
      local score=`score'+`B`i''
   }
}

local nbgroups:word count `anything'
if "`B'"=="" {
   forvalues i=1/`nbgroups' {
      local B`i'=1
   }
   local nbw=`nbgroups'
   local score=`nbgroups'
}
if `nbgroups'!=`nbw' {
   di in red "There is not the same number of values and of weights. Please correct that."
   error
}



tokenize `anything'
local list
forvalues i=1/`nbgroups' {
          local tmp=``i''
          local list "`list' `tmp'"
}

tokenize `list'

tempname G
matrix `G'=J(`=`score'+4',`nbgroups',0)

local poids=`score'+2
local epsilon=`score'+3
local max=`score'+4

forvalues j=1/`nbgroups' {
    matrix `G'[`poids',`j']=`B`j''
    matrix `G'[`epsilon',`j']=exp(-``j'')^`B`j''
    if `j'!=1 {
       matrix `G'[`max',`j']=`G'[`max',`=`j'-1']+`B`j''
    }
    matrix `G'[1,`j']=1
}
matrix `G'[`=`B1'+1',1]=exp(-`1')^`B1'

forvalues j=2/`nbgroups' {
    local w=`B`j''
    forvalues s=2/`=`score'+1' {
        matrix `G'[`s',`j']=`G'[`s',`j']+`G'[`s',`=`j'-1']
    }
    forvalues s=`=`w'+1'/`=`score'+1' {
        matrix `G'[`s',`j']=`G'[`s',`j']+exp(-``j'')^`w'*`G'[`=`s'-`w'',`=`j'-1']
    }
}
forvalues r=0/`score' {
        if "`display'"!="" {
           display in green "Level: " in ye `r' in green _col(25) "   Gamma=" in ye %10.5f `G'[`=`r'+1',`nbgroups']
        }
	return scalar gamma`r'= `G'[`=`r'+1',`nbgroups']
}
*return matrix G=`G'
end