!
! @ gag_demo:demo-emcee  Amp Pos Width Noise
!
! An example of EMCEE usage
! For simplicity, let us define the GAUSSIAN function
if pro%narg.le.3 then
  SAY "Usage:"
  SAY " @ gag_demo:demo-emcee  Amp Pos Width Noise"
  SAY " "
  return error
endif
let error no /New logical
if (&1.lt.5.or.&1.gt.15) then
  SAY "Amplitude out of bounds [0,15]"
  error = yes
endif
if (&2.lt.-1.or.&2.gt.1) then
  SAY "Position  out of bounds [-1,1]"
  error = yes
endif
if (&3.lt.0.or.&3.gt.10) then
  SAY "Width out of bounds [0,10]"
  error = yes
endif
if (error) return error
!
if (function(gauss)) delete /function gauss
define function gauss(x,a,b,c) a*exp(-0.5*((x-b)/c)^2)
!
if .not.exist(my_data) then
  ! The Gaussian parameters, Amplitude, Position and Width
  define double amp pos width /global
  ! Now the data...
  define integer nx
  let nx 512
  define double my_xx[nx] my_data[nx] my_noise /global
  ! Define the Measurement points XX
  let my_xx[i] 10*(i-nx/2)/nx
endif
!
! Fill the data with some noisy values. In reality, this may come from
! an experiment or observation
let my_noise &4
let my_data gauss(my_xx,&1,&2,&3)
let my_data[i] my_data+my_noise*noise(i/i)
!
! Start with some random value
let amp &1*(1+noise(my_noise))
let width &3*(1+noise(my_noise))
let pos &2+noise(my_noise)
!
begin procedure my_chi2
  define double res /like my_data
  let res my_data-gauss(my_xx,amp,pos,width)
  let res (res/my_noise)^2
  sic\compute emcee%chi2 sum res ! The Chi^2 must be returned here
  @ emcee-timer ! Optional: report advances in the chain
end procedure my_chi2
!
emcee my_data "@ my_chi2" /start 'amp' 'pos' 'width' -
      /par amp pos width /bound amp 5 15 pos -1 1 width 0 10 -
      /step 0.02 0.02 0.02 /length 500 /walk 6 /begin
for i 1 to 4
  emcee
next
!
