LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
Binomial.m
1classdef Binomial < DiscreteDistribution
2 % A Binomial distribution
3 %
4 % Copyright (c) 2018-2022, Imperial College London
5 % All rights reserved.
6
7 methods
8 function self = Binomial(n, p)
9 % SELF = BINOMIAL(N, P)
10
11 self@DiscreteDistribution('Binomial',2,[0,Inf]);
12 setParam(self, 1, 'n', n);
13 setParam(self, 2, 'p', p);
14 end
15
16 function ex = getMean(self)
17 % EX = GETMEAN()
18
19 % Get distribution mean
20 n = self.getParam(1).paramValue;
21 p = self.getParam(2).paramValue;
22 ex = p*n;
23 end
24
25 function V = getVar(self)
26 % V = GETVAR()
27 n = self.getParam(1).paramValue;
28 p = self.getParam(2).paramValue;
29 V = n*p*(1-p);
30 end
31
32 function SCV = getSCV(self)
33 % SCV = GETSCV()
34
35 % Get distribution squared coefficient of variation (SCV = variance / mean^2)
36 p = self.getParam(2).paramValue;
37 n = self.getParam(1).paramValue;
38 SCV = (1-p) / (n*p);
39 end
40
41 function X = sample(self, nsamples)
42 % X = SAMPLE(N)
43
44 X = zeros(1, nsamples);
45 p = self.getParam(2).paramValue;
46 n = self.getParam(1).paramValue;
47
48 for k = 1:nsamples
49 acc = 0;
50 for i = 1:n
51 if rand <= p
52 acc = acc + 1;
53 end
54 end
55 X(k) = acc;
56 end
57 end
58
59 function Ft = evalCDF(self,t)
60 % FT = EVALCDF(SELF,T)
61
62 % Evaluate the cumulative distribution function at T
63
64 p = self.getParam(2).paramValue;
65 n = self.getParam(1).paramValue;
66 Ft = 0.0;
67 if t >= n
68 Ft = 1.0;
69 elseif t >= 0
70 Ft = 1.0 - betainc(p, t + 1.0, n - t);
71 end
72 end
73
74 function L = evalLST(self, s)
75 % L = EVALST(S)
76 % Evaluate the Laplace-Stieltjes transform of the distribution function at s
77 % For Binomial(n, p), LST(s) = (1 - p + p*e^(-s))^n
78
79 p = self.getParam(2).paramValue;
80 n = self.getParam(1).paramValue;
81 L = (1 - p + p * exp(-s))^n;
82 end
83
84 function P = evalPMF(self, k)
85 % P = EVALPMF(K)
86
87 % Evaluate the probability mass function at k
88
89 p = self.getParam(2).paramValue;
90 n = self.getParam(1).paramValue;
91 P = binopdf(k, n, p);
92 end
93
94 function proc = getProcess(self)
95 % PROC = GETPROCESS()
96
97 % Get process representation for non-Markovian distribution
98 % Returns [mean, SCV] pair for use in network analysis
99 proc = [self.getMean(), self.getSCV()];
100 end
101 end
102
103end
104