stata v 13 Probability Distribution Calculators

Stata version 13
Probability Distribution Calculators
Stata version 13
Probability Distribution Calculators
February 2014
Probability Calculations (p-values and such) ………………..………..
(a) Binomial ………………………………………………………….
(b) Chi Square ………………………………………………………..
(c) F …………………………………………………………………..
(d) Hypergeometric (Central) ………..………………………………..
(e) Normal ……………………………………………………………
(f) Poisson …………………………………………………………….
(g) Student t ……………………………………………………………
2
2
5
6
7
8
10
13
Preliminary: Download the module probcalc
This user-created module is used for the following distributions: binomial, poisson, and normal.
Type the following in the command window.
. ssc install probcalc
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 1 of 13
Stata version 13
Probability Distribution Calculators
(a) Binomial Distribution
Binomial(n, pi): Probability of exactly k events, Pr[X = k]
probcalc b ntrials pi exactly k
. * Binomial(n=20, pi=.03) Prob[X=2]
. probcalc b 20 .03 exactly 2
Distribution: Binomial
n=20
p= .03
option:exactly
x=2
P(X=2)=.09882967
Binomial(n, pi): Probability of at most k events, Pr[X < k]
probcalc b ntrials pi atmost k
. * Binomial(n=20, pi=.03) Prob[X <= 2]
. probcalc b 20 .03 atmost 2
Distribution: Binomial
n=20
p= .03
option:atmost
x=2
P(X=0)=.54379434
P(X=1)=.33636763
P(X=2)=.09882967
pmf Method 1: P(X<=2)=.97899164
cdf Method 2: P(X<=2)=.97899164
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 2 of 13
Stata version 13
Probability Distribution Calculators
Binomial(n, pi): Probability of less than k events, Pr[X < k]
probcalc b ntrials pi atmost k-1
. * Binomial(n=20, pi=.03) Prob[X < 2]
. probcalc b 20 .03 atmost 1
Distribution: Binomial
n=20
p= .03
option:atmost
x=1
P(X=0)=.54379434
P(X=1)=.33636763
pmf Method 1: P(X<=1)=.88016198
cdf Method 2: P(X<=1)=.88016198
Binomial(n, pi): Probability of at least k events, Pr[X > k]
probcalc b ntrials pi atleast k
. * Binomial(n=20, pi=.03) Prob[X >= 2]
. probcalc b 20 .03 atleast 2
Distribution: Binomial
n=20
p= .03
option:atleast
x=2
P(X=2)=.09882967
--
output omitted --
P(X=20)=3.487e-31
pmf Method 1: P(X>=2)=.11983802
cdf Method 2: P(X>=2)=.11983802
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 3 of 13
Stata version 13
Probability Distribution Calculators
Binomial(n, pi): Probability of more than k events, Pr[X > k]
probcalc b ntrials pi atleast k+1
. *
. * Binomial(n=20, pi=.03) Prob[X > 2]
. probcalc b 20 .03 atleast 3
Distribution: Binomial
n=20
p= .03
option:atleast
x=3
P(X=3)=.01833953
-- output omitted -P(X=20)=3.487e-31
pmf Method 1: P(X>=3)=.02100836
cdf Method 2: P(X>=3)=.02100836
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 4 of 13
Stata version 13
Probability Distribution Calculators
(b) Chi Square Distribution
Chi Square (degrees of freedom = df): Probability [Y < y ] is the same as Probability [Y < y ]
display chi2(df,y)
. * Pr[Chi square df=2 <= 1.5]
. display chi2(2,1.5)
.52763345
Chi Square (degrees of freedom = df): Probability [Y > y ] is the same as Probability [Y > y ]
display chi2tail(df,y)
. * Pr[Chi square df=2 >= 1.5]
. display chi2tail(2,1.5)
.47236655
Chi Square (degrees of freedom = df): Solution for pth quantile
display invchi2(df,p)
. * Chi Square df=2: Solution for 97.5th percentile
. display invchi2(2,.975)
7.3777589
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 5 of 13
Stata version 13
Probability Distribution Calculators
(c) F Distribution
F (degrees of freedom = df1 and df2): Probability [Y < y ] is the same as Probability [Y < y ]
display F(df1, df2,y)
. * Pr[F(df=2,6) < 2.3]
. display F(2,6,2.3)
.81864223
F (degrees of freedom = df1 and df2): Probability [Y > y ] is the same as Probability [Y > y ]
display Ftail(df1,df2,y)
. * Pr[F(df=2,6) > 2.3]
. display Ftail(2,6,2.3)
.18135777
F (degrees of freedom = df1 and df2): Solution for pth quantile
display invFtail(df1,df2,1-p)
. * F with df=2,6: Solution for 95th percentile
. display invFtail(2,6,.05)
5.1432528
. * F with df=2,6: Solution for 5th percentile
. display invFtail(2,6,.95)
.0517343
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 6 of 13
Stata version 13
Probability Distribution Calculators
(d) Hypergeometric Distribution (Central)
Disease
Exposure
Yes
No
Yes
a
c
K = total # with disease
No
b
d
n = total # exposed
N=grand total
Hypergeometric (N total, K disease, n exposed): Probability [Exactly a with exposure AND disease ]
display hypergeometricp(N,K,n,a)
. * Pr[Hypergeometric N=259, K=4, n=23, a=2]
. display hypergeometricp(259,4,23,2)
.03829914
Hypergeometric (N total, K disease, n exposed): Probability [a or less with exposure AND disease ]
display hypergeometric(N,K,n,a)
. * Pr[Hypergeometric N=259, K=4, n=23, a<=2]
. display hypergeometric(259,4,23,2)
.99767001
Hypergeometric (N total, K disease, n exposed): Probability [a or more with exposure AND disease ]
Tips: (1) Use this for p-values; and (2) Note that that “a” needs to be reduced by 1.
display 1 - hypergeometric(N,K,n,a-1)
. * Pr[Hypergeometric N=259, K=4, n=23, a>=2]
. display 1-hypergeometric(259,4,23,1)
.04062914
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 7 of 13
Stata version 13
Probability Distribution Calculators
(e) Normal Distribution
Normal(mu, sigma), between: Probability[a < X < b] is the same as Probability[a < X < b]
probcalc n mu sigma between a b
. * Pr[Normal(mu=100, sigma=15) is between 85 and 115
. probcalc n 100 15 between 85 115
Distribution: Normal
mean:100
s.d.:15
option:between
x=85
115
cdf Method: P(85<=X<115)=.68268949
Normal(mu, sigma), at most: Probability[ X < b] is the same as Probability[X < b]
probcalc n mu sigma atmost b
. * Pr[Normal(mu=100, sigma=15) is at most 115
. probcalc n 100 15 atmost 115
Distribution: Normal
mean:100
s.d.:15
option:atmost
x=115
cdf Method: P(X<=115)=.84134475
Normal(mu, sigma), at least: Probability[ X > a] is the same as Probability[X > a]
probcalc n mu sigma atleast a
. * Pr[Normal(mu=100, sigma=15) is at least 85
. probcalc n 100 15 atleast 85
Distribution: Normal
mean:100
s.d.:15
option:atleast
x=85
cdf Method: P(X>=85)=.84134475
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 8 of 13
Stata version 13
Probability Distribution Calculators
Normal(mu, sigma): Solution for pth quantile
display mu+sigma*invnormal(p)
. * Normal(mu=100, sigma=15): Solution for 95th percentile
. display 100+15*invnormal(.95)
124.6728
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 9 of 13
Stata version 13
Probability Distribution Calculators
(f) Poisson Distribution
Poisson(mu): Probability of exactly k events, Pr[X = k]
probcalc p mu exactly k
. * Pr[Poisson(mu=1.8) = 6]
. probcalc p 1.8 exactly 6
Distribution: Poisson
mu=1.8
option:exactly
x=6
P(X=6)=.00780859
Poisson(mu): Probability of at most k events, Pr[X < k]
probcalc p mu atmost k
. * Pr[Poisson(mu=1.8) <= 6]
. probcalc p 1.8 atmost 6
Distribution: Poisson
mu=1.8
option:atmost
x=6
P(X=0)=.16529889
-- output omitted -P(X=6)=.00780859
pmf Method 1: P(X<=6)=.99743055
cdf Method 2: P(X<=6)=.99743055
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 10 of 13
Stata version 13
Probability Distribution Calculators
Poisson(mu): Probability of less than k events, Pr[X < k]
probcalc p mu atmost k-1
. * Pr[Poisson(mu=1.8) < 6]
. probcalc p 1.8 atmost 5
Distribution: Poisson
mu=1.8
option:atmost
x=5
P(X=0)=.16529889
-- output omitted -P(X=5)=.02602862
pmf Method 1: P(X<=5)=.98962196
cdf Method 2: P(X<=5)=.98962196
Poisson(mu): Probability of at least k events, Pr[X > k]
probcalc p mu atleast k
. * Pr[Poisson(mu=1.8) >= 6]
. probcalc p 1.8 atleast 6
Distribution: Poisson
mu=1.8
option:atleast
x=6
P(X=0)=.16529889
-- output omitted -pmf Method 1: P(X>=6)=.01037804
cdf Method 2: P(X>=6)=.01037804
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 11 of 13
Stata version 13
Probability Distribution Calculators
Poisson(mu): Probability of more than k events, Pr[X > k]
probcalc p mu atleast k+1
. * Pr[Poisson(mu=1.8) > 6]
. probcalc p 1.8 atleast 7
Distribution: Poisson
mu=1.8
option:atleast
x=7
Note: For Poisson ''at least'' questions, the sum of the lower tail pmf's is subtracted
from one.
So only variates less than x are reported below.
P(X=0)=.16529889
-- output omitted -P(X=6)=.00780859
pmf Method 1: P(X>=7)=.00256945
cdf Method 2: P(X>=7)=.00256945
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 12 of 13
Stata version 13
Probability Distribution Calculators
(g) Student-t Distribution
Student-t (degrees of freedom = df): Probability [T < t ] is the same as Probability [T < t ]
display 1 - ttail(df,t)
. * Pr[Student-t(df=12) < 2.1]
. display 1-ttail(12,2.1)
.97122753
Student-t (degrees of freedom = df): Probability [T > t ] is the same as Probability [T > t ]
display ttail(df,t)
. * Pr[Student-t(df=12) > 2.1]
. display ttail(12,2.1)
.02877247
Student-t (degrees of freedom = df): Solution for pth quantile
display invttail(df,1-p)
. * Student-t(df=12): Solution for 97.5th percentile
. display invttail(12,.025)
2.1788128
(mac)\teaching\stata\stata version 13\stata v 13 probability distribution calculators.doc
2/27/2014
Page 13 of 13