Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to generate a censored random variable


From   Joerg Luedicke <joerg.luedicke@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to generate a censored random variable
Date   Thu, 6 Feb 2014 14:22:24 -0500

It seems to me that you are interested in drawing random numbers from
a _truncated_ normal distribution. Truncation is not the same as
censoring. Also, if you refer to a certain R function, you should
mention which package that function is from. Anyway, an inverted CDF
approach is shown in a WIkipedia article (see link below). Here is
some Stata code:

*-----------------------------------------------
* http://en.wikipedia.org/wiki/Truncated_normal_distribution

clear
set obs 1000
set seed 1234

local a = 0
local b = 2
local sigma = 1
local mu = 0

gen u = runiform()

gen x = invnormal(normal(`a') + u * (normal(`b') - normal(`a'))) *
`sigma' + `mu'

drop u
sum x
*-----------------------------------------------

You might also want to have a look at Christian Robert's paper which
is referred to in the Wikipedia article. He proposes an accept-reject
algorithm for some situations. Below is an implementation of this
algorithm for the case where zero is an element of the truncation
range:

*-----------------------------------------------
* Robert, Christian P. (1995). "Simulation of truncated normal variables".
* Statistics and Computing 5 (2): 121-125.
* (section 2.2, page 123)

clear
set obs 1000
set seed 1234

gen x = .

local N 0

while `N' < 1000 {
cap drop z rho_z u
gen z = 2*runiform()
gen rho_z = exp( -z^2 / 2 )
gen u = runiform()
replace x = z if u <= rho_z & mi(x)
qui cou if !mi(x)
local N = r(N)
}

drop z rho_z u
sum x
*-----------------------------------------------

Joerg
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index