*! 2.0.0 NJC 30 Oct 2003
* 1.0.0 NJC 13 October 1998
program pexp, sort
	version 8
	syntax varname [fweight aweight iweight/] [if] [in] ///
	[, Grid GENerate(namelist min=2 max=2) param(numlist max=1) show(str) * ]

	if "`generate'" != "" {
		capture confirm new var `generate'
		if _rc {
			di as err "generate() must name new variables"
			exit 198
		}
	}

	marksample touse
	qui count if `touse'
	if r(N) == 0 error 2000

	if "`exp'" == "" local exp = 1

	if "`param'" != "" local M `param'
	else {
		su `varlist' [w=`exp'] if `touse', meanonly
		local M = r(mean)
	}

	_get_gropts , graphopts(`options') getallowed(rlopts plot)
	local options `"`s(graphopts)'"'
	local rlopts `"`s(rlopts)'"'
	local plot `"`s(plot)'"'
	_check4gropts rlopts, opt(`rlopts')

	tempvar F Psubi
	quietly {
		if `"`show'"' != ""  {
			capture count if `show'
			if _rc {
				di as err "invalid show() option"
				exit 198
			}
			else {
				count if (`show') & `touse'
				if r(N) == 0 error 2000
			}

			local show "& (`show')"
		}

		sort `touse' `varlist'
		gen float `Psubi' = sum(`touse' * `exp') - 0.5 * `exp'
		su `touse' [w = `exp'], meanonly
		replace `Psubi' = `Psubi' / r(sum) if `touse'
		gen float `F' = 1 - exp(-`varlist' / `M') if `touse'
	}

	local yttl "exponential F[`varlist']"
	label var `F' "`yttl'"
	local xttl "Empirical P[i]"
	format `F' `Psubi' %9.2f
	if `"`plot'"' == "" local legend legend(nodraw)

	graph twoway		        	///
	(scatter `F' `Psubi' if `touse' `show', ///
		sort				///
		ylabel(0(.25)1, nogrid `grid')	///
		xlabel(0(.25)1, nogrid `grid')	///
		ytitle(`"`yttl'"')		///
		xtitle(`"`xttl'"')		///
		`legend'			///
		`options'			///
	)					///
	(function y=x if `touse' `show',	///
		clstyle(refline)		///
		range(`Psubi')			///
		n(2)				///
		yvarlabel("Reference")		///
		`rlopts'			///
	)					///
	|| `plot'				///
	// blank

	// user will see messages about missing values
	if "`generate'" != "" {
		tokenize `generate'
		gen `1' = `F' if `touse'
		label var `1' "exponential probabilities"
		gen `2' = `Psubi' if `touse'
		label var `2' "empirical probabilities"
	}
end