* NJC rewrite
*! 2.0.0 3 January 2006
*! Joseph Hilbe 4\24\91 EXACT POISSON PROBABILITY
program expoissoni, rclass
	version 8
	args expect cases garbage

	if "`cases'" == "" { // zero or one argument
		ErrorMsg
		exit 198
	}
	else if "`garbage'" != "" { /// three arguments
		tokenize "`0'"
		args ratio obs cases garbage

		if "`garbage'" != "" {
			ErrorMsg
			exit 198
		}

		if `ratio' <= 0 {
			di as err "ratio should be positive"
			exit 498
		}

		if `obs' != floor(`obs') | `obs' <= 0 {
			di as err "observations should be integer > 0"
			exit 498
		}

		if `cases' > `obs' {
			di as err "`cases' greater than `obs'"
			exit 498
		}

		tempname expect
		scalar `expect' = `ratio' * `obs'
	}
	else { // two arguments
		if `expect' <= 0 {
			di as err "mean should be positive"
			exit 498
		}
	}

	if `cases' != floor(`cases') | `cases' < 0 {
		di as err "cases should be integer >= 0"
		exit 498
	}

	tempname pr
	scalar `pr' = `expect'
	scalar `pr' = (`pr'^`cases') * exp(-`pr' - lnfactorial(`cases'))

	di as txt "Exact Poisson probability " as res `pr'
	return scalar prob = `pr'
end

program ErrorMsg
	di as err "Type 2 arguments: population mean, cases"
	di as err "OR type 3 arguments: population ratio, observations, cases"
end