-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruncnormrnd.m
59 lines (50 loc) · 1.31 KB
/
truncnormrnd.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
56
57
58
59
function z=truncnormrnd(N,mu,sig,xlo,xhi)
% truncnormrnd: truncated normal deviate generator
% usage:z=truncnormrnd(N,mu,sig,xlo,xhi)
%
% (assumes the statistics toolbox, its easy
% to do without that toolbox though)
%
% arguments: (input)
% N - size of the resulting array of deviates
% (note, if N is a scalar, then the result will be NxN.)
% mu - scalar - Mean of underlying normal distribution
% sig - scalar - Standard deviation of underlying normal distribution
% xlo - scalar - Low truncation point, if any
% xhi - scalar - High truncation point, if any
%
% arguments: (output)
% z - array of truncated normal deviates, size(z)==N
%
% see https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/_WXo4FhVP4g
% defaults
if (nargin<2)|isempty(mu)
mu=0;
end
if (nargin<3)|isempty(sig)
sig=0;
end
if (nargin<4)|isempty(xlo)
xlo=-inf;
plo=0;
else
plo=normcdf((xlo-mu)/sig);
end
if (nargin<5)|isempty(xhi)
xhi=inf;
phi=1;
else
phi=normcdf((xhi-mu)/sig);
end
% test if trunation points are reversed
if xlo>xhi
error 'Must have xlo <= xhi if both provided'
end
% generate uniform [0,1] random deviates
r=rand(N);
% scale to [plo,phi]
r=plo+(phi-plo)*r;
% Invert through standard normal
z=norminv(r);
% apply shift and scale
z=mu+z*sig;