1classdef DiscreteSampler < DiscreteDistribution
2 % A
class for discrete distributions specified from the probability mass function
4 % Copyright (c) 2012-2026, Imperial College London
8 function self = DiscreteSampler(p,x)
9 % SELF = DISCRETESAMPLER(
P,X)
11 % Construct a discrete distribution from a finite probability
12 % vector p at the points specificied in vector x
14 % p(i)
is the probability of item i
15 % x(i)
is the value of item i
17 if nargin<2 %~exist(
'x',
'var')
20 self@DiscreteDistribution('DiscreteSampler',3,[min(x),max(x)]);
21 setParam(self, 1, 'p', p(:)');
22 setParam(self, 2, 'x', x(:)');
23 setParam(self, 3, 'f', cumsum(p(:)')/sum(p));
26 function ex = getMean(self)
29 % Get distribution mean
30 p = self.getParam(1).paramValue;
35 function SCV = getSCV(self)
38 % Get distribution squared coefficient of variation (SCV = variance / mean^2)
41 p = self.getParam(1).paramValue;
43 e2 = sum(p*(1:n).^2');
48 function X = sample(self, n)
51 x = self.getParam(2).paramValue;
52 f = self.getParam(3).paramValue;
54 X = x(maxpos(min(repmat(r,1,size(f,2))<=f,2)')');
57 function Ft = evalCDF(self,k)
58 % FT = EVALCDF(SELF,K)
60 f = self.getParam(3).paramValue;
61 if k>=1 && k<length(f)
68 function pk = evalPMF(self, v)
71 p = self.getParam(1).paramValue;
72 x = self.getParam(2).paramValue;
73 if nargin<2 %~exist('v','var')
78 pk(i) = p(find(x==v(i),1));
82 function
bool = isDisabled(self)
85 p = self.getParam(1).paramValue;