-
Notifications
You must be signed in to change notification settings - Fork 0
/
emprand.m
105 lines (100 loc) · 3.17 KB
/
emprand.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function xr = emprand(x,dist,varargin)
%EMPRAND Generates random numbers from empirical distribution of data.
% This is useful when you do not know the distribution type (i.e. normal or
% uniform), but you have the data and you want to generate random
% numbers form the data. The idea is to first construct cumulative distribution
% function (cdf) from the given data. Then generate uniform random number and
% interpolate from cdf.
%
% USAGE:
% xr = EMPRAND(dist) - one random number
% xr = EMPRAND(dist,m) - m-by-m random numbers
% xr = EMPRAND(dist,m,n) - m-by-n random numbers
%
% INPUT:
% dist - vector of distribution i.e. data values
% m - generates m-by-m matrix of random numbers
% n - generates m-by-n matrix of random numbers
%
% OUTPUT:
% xr - generated random numbers
%
% EXAMPLES:
% % Generate 1000 normal random numbers
% mu = 0; sigma = 1; nr = 1000;
% givenDist = mu + sigma * randn(nr,1);
% generatedDist = emprand(givenDist,nr,1);
% %
% % % Plot histogram to check given and generated distribution
% [n,xout] = hist(givenDist);
% hist(givenDist);
% hold on
% hist(generatedDist,xout)
% %
% Plot cdf to check given and generated distribution
% figure
% x = sort(givenDist(:)); % Given distribution
% p = 1:length(x);
% p = p./length(x);
% plot(x,p,'color','r');
% hold on
%
% xr = sort(generatedDist(:)); % Generated distribution
% pr = 1:length(xr);
% pr = pr./length(xr);
%
% plot(xr,pr,'color','b');
% xlabel('x')
% ylabel('cdf')
% legend('Given Dist.','Generated Dist.')
% title('1000 random numbers generated from given normal distribution of data');
%
% HISTORY:
% version 1.0.0, Release 05-Jul-2005: Initial release
% version 1.1.0, Release 16-Oct-2007: Some bug fixes and improvement of help text
% 1. Can handle NaN values in dist
% 2. Extraplolate for out of range
% 3. Calling function EMPCDF is included within this function
%
% See also:
% Author: Durga Lal Shrestha
% UNESCO-IHE Institute for Water Education, Delft, The Netherlands
% eMail: [email protected]
% Website: http://www.hi.ihe.nl/durgalal/index.htm
% Copyright 2004-2007 Durga Lal Shrestha.
% $First created: 05-Jul-2005
% $Revision: 1.1.0 $ $Date: 16-Oct-2007 21:47:47 $
% ***********************************************************************
%% INPUT ARGUMENTS CHECK
%
% error(nargchk(1,3,nargin));
if ~isvector(dist)
error('Invalid data size: input data must be vector')
end
if nargin == 3
m = varargin{1};
n = m;
elseif nargin == 4
m = varargin{1};
n = varargin{2};
else
m = 1;
n = 1;
end
%% COMPUTATION
% % x = dist(:);
% % Remove missing observations indicated by NaN's.
% t = ~isnan(x);
% x = x(t);
% Compute empirical cumulative distribution function (cdf)
% xlen = length(x);
% x = sort(x);
% p = 1:xlen;
% p = p./xlen;
p = dist;
% Generate uniform random number between 0 and 1
ur = rand(m,n);
% keyboard
% Interpolate ur from empirical cdf and extraplolate for out of range
% values.
xr = interp1(x,p,ur,'linear','extrap');