-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathnormtrand.m
executable file
·55 lines (47 loc) · 1.87 KB
/
normtrand.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function result = normtrand(mu,sigma2,left,right)
%NORMTRAND random draws from a normal truncated to (left,right) interval
% ------------------------------------------------------
% USAGE: y = normtrand(mu,sigma2,left,right)
% where: mu = mean (nobs x 1)
% sigma2 = variance (nobs x 1)
% left = left truncation points (nobs x 1)
% right = right truncation points (nobs x 1)
% ------------------------------------------------------
% RETURNS: y = (nobs x 1) vector
% ------------------------------------------------------
% NOTES: use y = normtrand(mu,sigma2,left,mu+5*sigma2)
% to produce a left-truncated draw
% use y = normtrand(mu,sigma2,mu-5*sigma2,right)
% to produce a right-truncated draw
% ------------------------------------------------------
% SEE ALSO: normltrand (left truncated draws), normrtrand (right truncated)
%
% adopted from Bayes Toolbox by
% James P. LeSage, Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% Anyone is free to use these routines, no attribution (or blame)
% need be placed on the author/authors.
% For information on the Bayes Toolbox see:
% Ordinal Data Modeling by Valen Johnson and James Albert
% Springer-Verlag, New York, 1999.
% 2009-01-08 Aki Vehtari - Fixed Naming
if nargin ~= 4
error('normtrand: wrong # of input arguments');
end;
std = sqrt(sigma2);
% Calculate bounds on probabilities
lowerProb = Phi((left-mu)./std);
upperProb = Phi((right-mu)./std);
% Draw uniform from within (lowerProb,upperProb)
u = lowerProb+(upperProb-lowerProb).*rand(size(mu));
% Find needed quantiles
result = mu + Phiinv(u).*std;
function val=Phiinv(x)
% Computes the standard normal quantile function of the vector x, 0<x<1.
val=sqrt(2)*erfinv(2*x-1);
function y = Phi(x)
% Phi computes the standard normal distribution function value at x
y = .5*(1+erf(x/sqrt(2)));