*! 1.0.1 NJC 15 Nov 2002
program define ciji, rclass
	version 7
	gettoken n 0 : 0
	confirm integer number `n'
	gettoken k 0 : 0, parse(" ,")
	confirm integer number `k'
	syntax [ , Level(integer $S_level) beta(numlist min=2 max=2 >0) ]
	if `level' < 10 | `level' > 99 {
		error 198
	}

	local ttl "    Mean"
	local tt1 "    Obs "

	ret scalar N = `n'
	ret scalar mean = `k' / `n'
	ret scalar se = sqrt((return(mean))*(1-return(mean))/`n')

	if "`beta'" == "" {
		local beta "0.5 0.5"
		local txt "Jeffreys"
	}
	else local txt "Supplied"

	local a : word 1 of `beta'
	local b : word 2 of `beta'

	tempname lb ub
	if `k' == 0 {
		scalar `lb' = 0
	}
	else {
	        scalar `lb' = /*
*/ invFtail(2 * (`k' + `a'), 2 * (`n' - `k' + `b'), (100 + `level') / 200)
        	scalar `lb' = /*
*/ ((`k' + `a') * `lb') / (`n' - `k' + `b' + ((`k' + `a') * `lb'))
	}
	if `k' == `n' {
		scalar `ub' = 1
	}
	else {
	        scalar `ub' = /*
*/ invFtail(2 * (`k' + `a'), 2 * (`n' - `k' + `b'), (100 - `level') / 200)
        	scalar `ub' = /*
*/ ((`k' + `a') * `ub') / (`n' - `k' + `b' + ((`k' + `a') * `ub'))
	}

	ret scalar lb = `lb'
	ret scalar ub = `ub'

	/* double save in S_# and r()  */
	global S_1 `return(N)'
	global S_3 `return(mean)'
	global S_4 `return(se)'
	global S_5 `return(lb)'
	global S_6 `return(ub)'

	di in smcl in gr _col(56) /*
	*/ "{hline 2} `txt' Prior {hline 2}"

	#delimit ;
	di in smcl in gr
"    Variable {c |} `tt1'    `ttl'    Std. Err.       [`level'% Conf. Interval]"
	_n "{hline 13}{c +}{hline 61}" ;
	di in smcl in gr "             {c |}" _col(14)
		in yel %8.0f return(N)
	 	_col(27) %9.0g return(mean)
	 	_col(39) %9.0g return(se)
	 	_col(55) %9.0g return(lb)
	 	_col(67) %9.0g return(ub) in gr  ;
	#delimit cr
end