LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
EmpiricalCDF.m
1classdef EmpiricalCDF < Distribution
2 % Empirical Cdf for a distribution
3 %
4 % Copyright (c) 2012-2026, Imperial College London
5 % All rights reserved.
6
7 properties
8 data;
9 end
10
11 methods
12 %Constructor
13 function self = EmpiricalCDF(xdata,cdfdata)
14 % SELF = EMPIRICAL(data)
15 self@Distribution('EmpiricalCdf',2,[-Inf,Inf]);
16 if nargin ==1
17 self.data = xdata;
18 else
19 self.data = unique([cdfdata,xdata],'rows');
20 end
21 end
22
23 function X = sample(self, n)
24 % X = SAMPLE(n)
25 % Get n samples from the distribution
26 Y = rand(n,1);
27 xset = find(self.data(:,1)<1); % remove duplicates when numerically F(x)=1
28 X = interp1(self.data(xset,1),self.data(xset,2),Y(:),'spline','extrap');
29 end
30
31 function RATE = getRate(self)
32 % RATE = GETRATE()
33 % Get distribution rate
34 RATE = 1 / getMean(self);
35 end
36
37 function MEAN = getMean(self)
38 % MEAN = GETMEAN()
39 % Get distribution mean
40 MEAN = getMoments(self);
41 end
42
43 function SCV = getSCV(self)
44 % SCV = GETSCV()
45 % Get distribution squared coefficient of variation (SCV = variance / mean^2)
46 [~,~,~,SCV,~] = getMoments(self);
47 end
48
49 function VAR = getVar(self)
50 % VAR = GETVAR()
51 % Get distribution variance
52 VAR = getSCV(self)*getMean(self)^2;
53 end
54
55 function SKEW = getSkewness(self)
56 % SKEW = GETSKEWNESS()
57 % Get distribution skewness
58 [~,~,~,~,SKEW] = getMoments(self);
59 end
60
61 function Ft = evalCDF(self,t)
62 % FT = EVALCDF(SELF,T)
63 % Evaluate the cumulative distribution function at t
64 xset = find(self.data(:,1)<1); % remove duplicates when numerically F(x)=1
65 self.data(xset,1),self.data(xset,2)
66 Ft = interp1(self.data(xset,1),self.data(xset,2),t,'spline','extrap');
67 end
68
69 function L = evalLST(self, s)
70 % L = EVALLST(S)
71 % Evaluate the Laplace transform of the distribution function at t
72 cdfdata = self.data;
73 row = size(cdfdata,1);
74 L = 0; % the first moment
75 for i = 1:1:row-1
76 x = ((cdfdata(i+1,2)-cdfdata(i,2))/2+cdfdata(i,2));
77 bin1 = exp(-s*x)*((cdfdata(i+1,1)-cdfdata(i,1)));
78 L = L+bin1;
79 end
80 end
81
82 function [m1,m2,m3,SCV,SKEW] = getMoments(self)
83 cdfdata = self.data;
84 row = size(cdfdata,1);
85 m1 = 0; % the first moment
86 m2 = 0; % the second moment
87 m3 = 0; % the third moment
88 for i = 1:1:row-1
89 x = ((cdfdata(i+1,2)-cdfdata(i,2))/2+cdfdata(i,2));
90 bin1 = x*((cdfdata(i+1,1)-cdfdata(i,1)));
91 bin2 = x^2*((cdfdata(i+1,1)-cdfdata(i,1)));
92 bin3 = x^3*((cdfdata(i+1,1)-cdfdata(i,1)));
93 m1 = m1+bin1;
94 m2 = m2+bin2;
95 m3 = m3+bin3;
96 end
97 SCV = (m2/m1^2)-1;
98 SKEW = (m3-3*m1*(m2-m1^2)-m1^3)/((m2-m1^2)^(3/2));
99 end
100 end
101end
102