/
core.go
345 lines (270 loc) · 9 KB
/
core.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package prob
import (
"math"
"math/rand"
"github.com/nlandolfi/set"
)
// --- Types {{{
type (
// A Probability is a real number on the interval [0, 1]
Probability float64
// An Outcome is an element of a set, namely
// the Outcome Space (often denoted Ω)
Outcome set.Element
// Outcomes is a typed slice of set.Element
Outcomes []set.Element
// An OutcomeSpace is a set
OutcomeSpace set.Interface
// A Distribution is the interface for interacting with
// probability distribution. The domain is the universal set,
// for that distribution, the outcomes are the support.
Distribution interface {
// Domain is the set which defines the possible outcomes of
// a Distribution. This is the Outcome Space.
Domain() set.AbstractInterface
// Outcomes is the set of Outcomes in the Domain which occur
// with a non-zero probability.
Outcomes() set.Interface
// ProbabilityOf returns the probability of a given Outcome
//
// Note: ProbabilityOf should return a probability of 0 (Impossible)
// for any outcome in the domain, but without defined support
ProbabilityOf(Outcome) Probability
}
// A DiscreteDistribution is the interface for a distribution
// we can programmatically manipulate. It inherits the interface
// of a general Distribution
//
// Note: We can examine/add outcomes
DiscreteDistribution interface {
Distribution
Support() Outcomes
AddOutcome(Outcome, Probability)
}
// An Event is a set. As in probability theory, this set should
// be a subset of the outcome space. e.g., an event A ⊆ Ω
Event set.Interface
// A RandomVariable is defined to be a real valued function of an outcome
// of a random experiment. It is neither random, nor variable. It is a
// fixed function. The stochasic Outcome is the random component.
RandomVariable func(Outcome) float64
)
// --- }}}
// --- Probability {{{
const (
// Impossible represents the probability that an outcome or event
// will never occur.
Impossible Probability = 0.0
// Certain represents the probability that an outcome or event
// will certainly occur.
Certain Probability = 1.0
)
// Valid determines whether a Probability value is valid. All
// probabilities must be on the inverval: [0, 1]
func (p Probability) Valid() bool {
return p >= 0 && p <= 1
}
// epsilon is the acceptable floating point error
const epsilon = 0.00001
// equiv determines whether two float64s are equivalent to each
// other with respect to epsilon
func equiv(f1, f2 float64) bool {
return math.Abs(f1-f2) < epsilon
}
// --- }}}
// --- Discrete Distribution --- {{{
// NewDiscreteDistribution constructs a discrete distribution over
// the set d, provided as the domain of the distribution
func NewDiscreteDistribution(d set.AbstractInterface) DiscreteDistribution {
return &distribution{
domain: d,
outcomes: set.New(),
support: make(map[Outcome]Probability),
}
}
// NewUniformDiscrete constructs a discrete distribution over the
// set domain. Each element is assigned a probability 1/Cardinality(domain)
func NewUniformDiscrete(domain set.Interface) DiscreteDistribution {
d := NewDiscreteDistribution(domain)
individualSupport := Certain / Probability(domain.Cardinality())
for _, o := range domain.Elements() {
d.AddOutcome(o, individualSupport)
}
return d
}
// distribution structure serves as an implementation
// of the DiscreteDistribution (and therefore implicitly
// Distribution) interfaces
type distribution struct {
domain set.AbstractInterface
outcomes set.Interface
support map[Outcome]Probability
}
func (d *distribution) Domain() set.AbstractInterface {
return d.domain
}
func (d *distribution) Outcomes() set.Interface {
return d.outcomes
}
func (d *distribution) Support() Outcomes {
return d.outcomes.Elements()
}
func (d *distribution) AddOutcome(o Outcome, p Probability) {
assert(!equiv(float64(Support(d)), 1.0), "distribution already fully supported")
assert((float64(Support(d)+p) < 1.0+epsilon), "adding outcome would over-support")
assert(p.Valid(), "invalid probability")
assert(!equiv(float64(p), 0), "probability zero")
d.outcomes.Add(o)
d.support[o] = p
}
func (d *distribution) ProbabilityOf(o Outcome) Probability {
p, ok := d.support[o]
if ok {
return p
}
if d.domain.Contains(o) {
return Impossible
} else {
panic("outcome not in domain")
}
}
// --- }}}
// --- Distribution Properties {{{
// Support calculates the total portion of the probability mass
// we have defined. Recall that all distributions have a mass of
// 1.
//
// I.e., if the Support(d) = 1, d ∈ Distributions, then we say
// that d is _fully-supported_. Adding another outcome with
// a non-zero probability would invalidate the distribution.
func Support(d Distribution) Probability {
p := Probability(0.0)
for _, o := range d.Outcomes().Elements() {
p += d.ProbabilityOf(o)
}
return p
}
// FullySupported checks that a Distribution has assigned all
// of it's probability mass.
//
// True iff the sum over the probabilities of all outcomes is 1.
func FullySupported(d Distribution) bool {
return equiv(float64(Support(d)), float64(Certain))
}
// The cardinality of a discrete distribution is the number of
// potential outcomes
func Cardinality(d DiscreteDistribution) uint {
return d.Outcomes().Cardinality()
}
// Degenerate evaluates whether a DiscreteDistribution is degenerate.
//
// A degenerate distribution is fully supported, but with only one
// outcome.
func Degenerate(d DiscreteDistribution) bool {
return Cardinality(d) == 1 && FullySupported(d)
}
// --- }}}
// --- Events {{{
// ProbabilityOf calculates the probability of an event, A, given
// a Distribution, d
func ProbabilityOf(d Distribution, A Event) Probability {
sum := Impossible
for _, a := range A.Elements() {
sum += d.ProbabilityOf(a)
}
return sum
}
// IndependentEvents determines whether A and B are independent
// under the distribution d.
//
// Equivalently: P(A, B) = P(A)P(B)
func IndependentEvents(d Distribution, A, B Event) bool {
return equiv(float64(ProbabilityOf(d, set.Union(A, B))), float64(ProbabilityOf(d, A)*ProbabilityOf(d, B)))
}
// --- }}}
// --- Random Variables {{{
// Moment calculates the nth moment of a random variable
//
// Recally: the nth moment of a random variable X over a
// distribution d is the expectation of X^n
func Moment(d Distribution, X RandomVariable, n int) float64 {
moment := func(o Outcome) float64 {
return math.Pow(X(o), float64(n))
}
return Expectation(d, moment)
}
// Expectation computes the expected value of a random variable,
// X over a distribution d
func Expectation(d Distribution, X RandomVariable) float64 {
exp := 0.0
for _, o := range d.Outcomes().Elements() {
exp += X(o) * float64(d.ProbabilityOf(o))
}
return exp
}
// Variance computes the variance of a random variable, X,
// over a distribution d
//
// Recall: Var(X) = E(X^2) - E(X)^2
func Variance(d Distribution, X RandomVariable) float64 {
return Moment(d, X, 2) - math.Pow(Moment(d, X, 1), 2.0)
}
// Covariance computes the covariance of the random variables X and Y,
// over a distribution d.
//
// Recall: Cov(X, Y) = E(XY) - E(X)E(Y)
func Covariance(d Distribution, X, Y RandomVariable) float64 {
return Expectation(d, func(o Outcome) float64 { return X(o) * Y(o) }) - Expectation(d, X)*Expectation(d, Y)
}
// IndependentVariables determines whether two random variables X and Y are
// independent over the distribution d.
//
// Recall: X ind. Y iff Cov(X, Y) = 0
func IndependentVariables(d Distribution, X, Y RandomVariable) bool {
return Covariance(d, X, Y) == 0
}
// --- }}}
// --- Composition {{{
// Compose takes two distributions, p and q, and creates a third lottery,
// n which takes on each outcome in p with a probability alpha times that
// events previous probability and each event in q with a probability 1-alpha
// times that events previous probability.
//
// for o ∈ p.Domain() intersect q.Domain(); P(x in n) is alpha*P(x in p) + (1-alpha)P(x in q)
func Compose(p, q DiscreteDistribution, alpha Probability) DiscreteDistribution {
assert(FullySupported(p), "first distribution is not fully supported")
assert(FullySupported(q), "second distribution is not fully supported")
//assert(set.Equivalent(p.Domain(), q.Domain()), "domains of both distributions must be equivalent")
n := NewDiscreteDistribution(p.Domain())
for _, o := range n.Outcomes().Elements() {
cp := alpha*p.ProbabilityOf(o) + (1-alpha)*q.ProbabilityOf(o)
if cp == Impossible {
continue // don't bother supporting
}
n.AddOutcome(o, cp)
}
return n
}
// --- }}}
// --- Simulation {{{
// Simulate simulates an experiment with the distribution
// defined by the DiscreteDistribution
//
// s := set.WithElements(1, 2, 3)
// d := NewUniformDiscrete(s)
// Simulate(d) => 1 w.p. 1/3, 2 w.p. 1/3, 3 w.p. 1/3
func Simulate(d DiscreteDistribution) Outcome {
assert(FullySupported(d), "discrete distribution not fully supported")
f := Probability(rand.Float64())
p := Probability(0)
var last Outcome
for _, o := range d.Outcomes().Elements() {
p += d.ProbabilityOf(o)
last = o
if f < p {
return o
}
}
return last
}
// --- }}}