LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
Bernoulli.m
1classdef Bernoulli < DiscreteDistribution
2 % Bernoulli Binary outcome distribution for success/failure modeling
3 %
4 % Bernoulli represents a Bernoulli distribution with probability parameter p.
5 % This is the simplest discrete distribution, modeling a single trial with
6 % two possible outcomes: success (1) with probability p, and failure (0)
7 % with probability 1-p. It forms the foundation for binomial distributions.
8 %
9 % @brief Bernoulli distribution for binary success/failure outcomes
10 %
11 % Key characteristics:
12 % - Single parameter: success probability p ∈ [0,1]
13 % - Mean = p, Variance = p(1-p)
14 % - SCV = (1-p)/(p)
15 % - Support: {0, 1}
16 % - Building block for binomial and geometric distributions
17 %
18 % The Bernoulli distribution is used for:
19 % - Modeling binary outcomes (success/failure, yes/no)
20 % - Cache hit/miss modeling
21 % - Component reliability analysis
22 % - Binary decision processes
23 % - Foundation for more complex discrete distributions
24 %
25 % Example:
26 % @code
27 % coin_flip = Bernoulli(0.5); % Fair coin (50% success)
28 % biased_coin = Bernoulli(0.7); % Biased coin (70% success)
29 % samples = coin_flip.sample(1000);
30 % @endcode
31 %
32 % Copyright (c) 2018-2022, Imperial College London
33 % All rights reserved.
34
35 methods
36 function self = Bernoulli(p)
37 % BERNOULLI Create a Bernoulli distribution instance
38 %
39 % @brief Creates a Bernoulli distribution with success probability p
40 % @param p Success probability (must be in [0,1])
41 % @return self Bernoulli distribution instance
42
43 self@DiscreteDistribution('Bernoulli',1,[0,Inf]);
44 setParam(self, 1, 'p', p);
45 end
46
47 function ex = getMean(self)
48 % EX = GETMEAN()
49
50 % Get distribution mean
51 n = 1;
52 p = self.getParam(1).paramValue;
53 ex = p*n;
54 end
55
56 function V = getVar(self)
57 % V = GETVAR()
58 n = 1;
59 p = self.getParam(1).paramValue;
60 V = n*p*(1-p);
61 end
62
63 function SCV = getSCV(self)
64 % SCV = GETSCV()
65
66 % Get distribution squared coefficient of variation (SCV = variance / mean^2)
67 p = self.getParam(1).paramValue;
68 n = 1;
69 SCV = (1-p) / (n*p);
70 end
71
72 function X = sample(self, nsamples)
73 % X = SAMPLE(N)
74
75 X = zeros(1, nsamples);
76 p = self.getParam(1).paramValue;
77 n = 1;
78
79 for k = 1:nsamples
80 acc = 0;
81 for i = 1:n
82 if rand <= p
83 acc = acc + 1;
84 end
85 end
86 X(k) = acc;
87 end
88 end
89
90 function Ft = evalCDF(self,t)
91 % FT = EVALCDF(SELF,T)
92
93 % Evaluate the cumulative distribution function at T
94
95 p = self.getParam(1).paramValue;
96 n = 1;
97 Ft = 0.0;
98 if t >= n
99 Ft = 1.0;
100 elseif t >= 0
101 Ft = 1.0 - betainc(p, t + 1.0, n - t);
102 end
103 end
104
105 function L = evalLST(self, s)
106 % L = EVALST(S)
107 % Evaluate the Laplace-Stieltjes transform of the distribution function at s
108 % For Bernoulli(p), LST(s) = (1 - p + p*e^(-s))
109
110 p = self.getParam(1).paramValue;
111 L = 1 - p + p * exp(-s);
112 end
113
114 function P = evalPMF(self, k)
115 % P = EVALPMF(K)
116
117 % Evaluate the probability mass function at k
118
119 p = self.getParam(1).paramValue;
120 n = 1;
121 P = binopdf(k, n, p);
122 end
123
124 function proc = getProcess(self)
125 % PROC = GETPROCESS()
126
127 % Get process representation for non-Markovian distribution
128 % Returns [mean, SCV] pair for use in network analysis
129 proc = [self.getMean(), self.getSCV()];
130 end
131 end
132
133end
134