help mata mm_rbinomial()-------------------------------------------------------------------------------

Title

mm_rbinomial() -- Generate binomial distributed random numbers------------------------------------------------------------------------------- Note: An official

rbinomial()function was introduced in Stata 10.1 (see help[M-5] runiform()). Officialrbinomial()is much faster thanmm_rbinomial(). -------------------------------------------------------------------------------

Syntax

real matrixmm_rbinomial(n,p)

real matrixmm_cebinomial(n,k,p)where

n:real matrix nk:real matrix kp:real matrix p

Description

mm_rbinomial()returns a random draw from the binomial distribution wherenis the number of trials andpis the success probability.When

nandpare not scalar,mm_rbinomial()returns element-by-element results.nandpare required to be r-conformable (see help[M-6]glossary).If you are serious about generating random numbers, you should first set the random number seed; see help

generateor help for[M-5] uniform().

mm_cebinomial()returns the expected value of a binomial distributed random variable conditional on the variable being equal tokor larger. That is,mm_cebinomial()returnsE(

X|X>=k)where

X~ B(n,p)and

nis the number of trials andpis the success probability.When

n,k, andpare not scalar,mm_cebinomial()returns element-by-element results.n,k, andpare required to be r-conformable (see help[M-6] glossary).

Remarks

mm_rbinomial(): Ifn<50 orp>.03, the random numbers are generated using the rejection technique. Ifn>=50 andp>=.03, the geometric distribution technique is applied (see Evans et al. 2000).

mm_cebinomial(): The expectation ofX~ B(n,p) conditional onX>=kmay be written asE(

X|X>=k) =k+ [ P(X>=k+1) + ... + P(X=n) ] / P(X>=k)where P(

X>=k) is the probability ofkor more successes, which is computed asBinomial(n,k,p)(see[M-5] normal()).

ConformabilityBoth functions require arguments be r-conformable (see help

[M-6]glossary). Returned is a matrix of max(argument rows) rows and max(argument columns) columns containing element-by-element calculated results.

DiagnosticsMissing value is returned if arguments are out of range (

p>1,p<0,n<=0,k<0, ork>n) or contain missing value or ifnandkare non-integer.

Source codemm_rbinomial.mata, mm_cebinomial.mata

ReferencesEvans, M., N. Hastings, B. Peacock (2000): Statistical Distributions. Third Edition. New York: Wiley.

AuthorBen Jann, ETH Zurich, jann@soz.gess.ethz.ch

Also seeOnline: help for

[M-5] normal(),moremata