/
distributions.py
1391 lines (1133 loc) · 49.8 KB
/
distributions.py
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# distributions.py: module for probability distributions.
##
# © 2017, Chris Ferrie (csferrie@gmail.com) and
# Christopher Granade (cgranade@cgranade.com).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
##
## IMPORTS ###################################################################
from __future__ import division
from __future__ import absolute_import
from builtins import range
from future.utils import with_metaclass
import numpy as np
import scipy.stats as st
import scipy.linalg as la
from scipy.interpolate import interp1d
from scipy.integrate import cumtrapz
from scipy.spatial import ConvexHull, Delaunay
from functools import partial
import abc
from qinfer import utils as u
from qinfer.metrics import rescaled_distance_mtx
from qinfer.clustering import particle_clusters
from qinfer._exceptions import ApproximationWarning
import warnings
## EXPORTS ###################################################################
__all__ = [
'Distribution',
'SingleSampleMixin',
'MixtureDistribution',
'ParticleDistribution',
'ProductDistribution',
'UniformDistribution',
'DiscreteUniformDistribution',
'MVUniformDistribution',
'ConstantDistribution',
'NormalDistribution',
'MultivariateNormalDistribution',
'SlantedNormalDistribution',
'LogNormalDistribution',
'BetaDistribution',
'BetaBinomialDistribution',
'GammaDistribution',
'GinibreUniform',
'HaarUniform',
'HilbertSchmidtUniform',
'PostselectedDistribution',
'ConstrainedSumDistribution',
'InterpolatedUnivariateDistribution'
]
## FUNCTIONS #################################################################
def scipy_dist(name, *args, **kwargs):
"""
Wraps calling a scipy.stats distribution to allow for pickling.
See https://github.com/scipy/scipy/issues/3125.
"""
return getattr(st, name)(*args, **kwargs)
## ABSTRACT CLASSES AND MIXINS ###############################################
class Distribution(with_metaclass(abc.ABCMeta, object)):
"""
Abstract base class for probability distributions on one or more random
variables.
"""
@abc.abstractproperty
def n_rvs(self):
"""
The number of random variables that this distribution is over.
:type: `int`
"""
pass
@abc.abstractmethod
def sample(self, n=1):
"""
Returns one or more samples from this probability distribution.
:param int n: Number of samples to return.
:rtype: numpy.ndarray
:return: An array containing samples from the
distribution of shape ``(n, d)``, where ``d`` is the number of
random variables.
"""
pass
class SingleSampleMixin(with_metaclass(abc.ABCMeta, object)):
"""
Mixin class that extends a class so as to generate multiple samples
correctly, given a method ``_sample`` that generates one sample at a time.
"""
@abc.abstractmethod
def _sample(self):
pass
def sample(self, n=1):
samples = np.zeros((n, self.n_rvs))
for idx in range(n):
samples[idx, :] = self._sample()
return samples
## CLASSES ###################################################################
class MixtureDistribution(Distribution):
r"""
Samples from a weighted list of distributions.
:param weights: Length ``n_dist`` list or ``np.ndarray``
of probabilites summing to 1.
:param dist: Either a length ``n_dist`` list of ``Distribution`` instances,
or a ``Distribution`` class, for example, ``NormalDistribution``.
It is assumed that a list of ``Distribution``s all
have the same ``n_rvs``.
:param dist_args: If ``dist`` is a class, an array
of shape ``(n_dist, n_rvs)`` where ``dist_args[k,:]`` defines
the arguments of the k'th distribution. Use ``None`` if the distribution
has no arguments.
:param dist_kw_args: If ``dist`` is a class, a dictionary
where each key's value is an array
of shape ``(n_dist, n_rvs)`` where ``dist_kw_args[key][k,:]`` defines
the keyword argument corresponding to ``key`` of the k'th distribution.
Use ``None`` if the distribution needs no keyword arguments.
:param bool shuffle: Whether or not to shuffle result after sampling. Not shuffling
will result in variates being in the same order as
the distributions. Default is ``True``.
"""
def __init__(self, weights, dist, dist_args=None, dist_kw_args=None, shuffle=True):
super(MixtureDistribution, self).__init__()
self._weights = weights
self._n_dist = len(weights)
self._shuffle = shuffle
try:
self._example_dist = dist[0]
self._is_dist_list = True
self._dist_list = dist
assert(self._n_dist == len(self._dist_list))
except:
self._is_dist_list = False
self._dist = dist
self._dist_args = dist_args
self._dist_kw_args = dist_kw_args
assert(self._n_dist == self._dist_args.shape[0])
self._example_dist = self._dist(
*self._dist_arg(0),
**self._dist_kw_arg(0)
)
def _dist_arg(self, k):
"""
Returns the arguments for the k'th distribution.
:param int k: Index of distribution in question.
:rtype: ``np.ndarary``
"""
if self._dist_args is not None:
return self._dist_args[k,:]
else:
return []
def _dist_kw_arg(self, k):
"""
Returns a dictionary of keyword arguments
for the k'th distribution.
:param int k: Index of the distribution in question.
:rtype: ``dict``
"""
if self._dist_kw_args is not None:
return {
key:self._dist_kw_args[key][k,:]
for key in self._dist_kw_args.keys()
}
else:
return {}
@property
def n_rvs(self):
return self._example_dist.n_rvs
@property
def n_dist(self):
"""
The number of distributions in the mixture distribution.
"""
return self._n_dist
def sample(self, n=1):
# how many samples to take from each dist
ns = np.random.multinomial(n, self._weights)
idxs = np.arange(self.n_dist)[ns > 0]
if self._is_dist_list:
# sample from each distribution
samples = np.concatenate([
self._dist_list[k].sample(n=ns[k])
for k in idxs
])
else:
# instantiate each distribution and then sample
samples = np.concatenate([
self._dist(
*self._dist_arg(k),
**self._dist_kw_arg(k)
).sample(n=ns[k])
for k in idxs
])
# in-place shuffling
if self._shuffle:
np.random.shuffle(samples)
return samples
class ParticleDistribution(Distribution):
r"""
A distribution consisting of a list of weighted vectors.
Note that either `n_mps` or both (`particle_locations`, `particle_weights`)
must be specified, or an error will be raised.
:param numpy.ndarray particle_weights: Length ``n_particles`` list
of particle weights.
:param particle_locations: Shape ``(n_particles, n_mps)`` array of
particle locations.
:param int n_mps: Dimension of parameter space. This parameter should
only be set when `particle_weights` and `particle_locations` are
not set (and vice versa).
"""
def __init__(self, n_mps=None, particle_locations=None, particle_weights=None):
super(ParticleDistribution, self).__init__()
if particle_locations is None or particle_weights is None:
# Initialize with single particle at origin.
self.particle_locations = np.zeros((1, n_mps))
self.particle_weights = np.ones((1,))
elif n_mps is None:
self.particle_locations = particle_locations
self.particle_weights = np.abs(particle_weights)
self.particle_weights = self.particle_weights / np.sum(self.particle_weights)
else:
raise ValueError('Either the dimension of parameter space, `n_mps`, or the particles, `particle_locations` and `particle_weights` must be specified.')
@property
def n_particles(self):
"""
Returns the number of particles in the distribution
:type: `int`
"""
return self.particle_locations.shape[0]
@property
def n_ess(self):
"""
Returns the effective sample size (ESS) of the current particle
distribution.
:type: `float`
:return: The effective sample size, given by :math:`1/\sum_i w_i^2`.
"""
return 1 / (np.sum(self.particle_weights**2))
## DISTRIBUTION CONTRACT ##
@property
def n_rvs(self):
"""
Returns the dimension of each particle.
:type: `int`
"""
return self.particle_locations.shape[1]
def sample(self, n=1):
"""
Returns random samples from the current particle distribution according
to particle weights.
:param int n: The number of samples to draw.
:return: The sampled model parameter vectors.
:rtype: `~numpy.ndarray` of shape ``(n, updater.n_rvs)``.
"""
cumsum_weights = np.cumsum(self.particle_weights)
return self.particle_locations[np.minimum(cumsum_weights.searchsorted(
np.random.random((n,)),
side='right'
), len(cumsum_weights) - 1)]
## MOMENT FUNCTIONS ##
@staticmethod
def particle_mean(weights, locations):
r"""
Returns the arithmetic mean of the `locations` weighted by `weights`
:param numpy.ndarray weights: Weights of each particle in array of
shape ``(n_particles,)``.
:param numpy.ndarray locations: Locations of each particle in array
of shape ``(n_particles, n_modelparams)``
:rtype: :class:`numpy.ndarray`, shape ``(n_modelparams,)``.
:returns: An array containing the mean
"""
return np.dot(weights, locations)
@classmethod
def particle_covariance_mtx(cls, weights, locations):
"""
Returns an estimate of the covariance of a distribution
represented by a given set of SMC particle.
:param weights: An array of shape ``(n_particles,)`` containing
the weights of each particle.
:param location: An array of shape ``(n_particles, n_modelparams)``
containing the locations of each particle.
:rtype: :class:`numpy.ndarray`, shape
``(n_modelparams, n_modelparams)``.
:returns: An array containing the estimated covariance matrix.
"""
# Find the mean model vector, shape (n_modelparams, ).
mu = cls.particle_mean(weights, locations)
# Transpose the particle locations to have shape
# (n_modelparams, n_particles).
xs = locations.transpose([1, 0])
# Give a shorter name to the particle weights, shape (n_particles, ).
ws = weights
cov = (
# This sum is a reduction over the particle index, chosen to be
# axis=2. Thus, the sum represents an expectation value over the
# outer product $x . x^T$.
#
# All three factors have the particle index as the rightmost
# index, axis=2. Using the Einstein summation convention (ESC),
# we can reduce over the particle index easily while leaving
# the model parameter index to vary between the two factors
# of xs.
#
# This corresponds to evaluating A_{m,n} = w_{i} x_{m,i} x_{n,i}
# using the ESC, where A_{m,n} is the temporary array created.
np.einsum('i,mi,ni', ws, xs, xs)
# We finish by subracting from the above expectation value
# the outer product $mu . mu^T$.
- np.dot(mu[..., np.newaxis], mu[np.newaxis, ...])
)
# The SMC approximation is not guaranteed to produce a
# positive-semidefinite covariance matrix. If a negative eigenvalue
# is produced, we should warn the caller of this.
assert np.all(np.isfinite(cov))
if not np.all(la.eig(cov)[0] >= 0):
warnings.warn('Numerical error in covariance estimation causing positive semidefinite violation.', ApproximationWarning)
return cov
def est_mean(self):
"""
Returns the mean value of the current particle distribution.
:rtype: :class:`numpy.ndarray`, shape ``(n_mps,)``.
:returns: An array containing the an estimate of the mean model vector.
"""
return self.particle_mean(self.particle_weights,
self.particle_locations)
def est_meanfn(self, fn):
"""
Returns an the expectation value of a given function
:math:`f` over the current particle distribution.
Here, :math:`f` is represented by a function ``fn`` that is vectorized
over particles, such that ``f(modelparams)`` has shape
``(n_particles, k)``, where ``n_particles = modelparams.shape[0]``, and
where ``k`` is a positive integer.
:param callable fn: Function implementing :math:`f` in a vectorized
manner. (See above.)
:rtype: :class:`numpy.ndarray`, shape ``(k, )``.
:returns: An array containing the an estimate of the mean of :math:`f`.
"""
return np.einsum('i...,i...',
self.particle_weights, fn(self.particle_locations)
)
def est_covariance_mtx(self, corr=False):
"""
Returns the full-rank covariance matrix of the current particle
distribution.
:param bool corr: If `True`, the covariance matrix is normalized
by the outer product of the square root diagonal of the covariance matrix,
i.e. the correlation matrix is returned instead.
:rtype: :class:`numpy.ndarray`, shape
``(n_modelparams, n_modelparams)``.
:returns: An array containing the estimated covariance matrix.
"""
cov = self.particle_covariance_mtx(self.particle_weights,
self.particle_locations)
if corr:
dstd = np.sqrt(np.diag(cov))
cov /= (np.outer(dstd, dstd))
return cov
## INFORMATION QUANTITIES ##
def est_entropy(self):
r"""
Estimates the entropy of the current particle distribution
as :math:`-\sum_i w_i \log w_i` where :math:`\{w_i\}`
is the set of particles with nonzero weight.
"""
nz_weights = self.particle_weights[self.particle_weights > 0]
return -np.sum(np.log(nz_weights) * nz_weights)
def _kl_divergence(self, other_locs, other_weights, kernel=None, delta=1e-2):
"""
Finds the KL divergence between this and another particle
distribution by using a kernel density estimator to smooth over the
other distribution's particles.
"""
if kernel is None:
kernel = st.norm(loc=0, scale=1).pdf
dist = rescaled_distance_mtx(self, other_locs) / delta
K = kernel(dist)
return -self.est_entropy() - (1 / delta) * np.sum(
self.particle_weights *
np.log(
np.sum(
other_weights * K,
axis=1 # Sum over the particles of ``other``.
)
),
axis=0 # Sum over the particles of ``self``.
)
def est_kl_divergence(self, other, kernel=None, delta=1e-2):
"""
Finds the KL divergence between this and another particle
distribution by using a kernel density estimator to smooth over the
other distribution's particles.
:param SMCUpdater other:
"""
return self._kl_divergence(
other.particle_locations,
other.particle_weights,
kernel, delta
)
## CLUSTER ESTIMATION METHODS #############################################
def est_cluster_moments(self, cluster_opts=None):
# TODO: document
if cluster_opts is None:
cluster_opts = {}
for cluster_label, cluster_particles in particle_clusters(
self.particle_locations, self.particle_weights,
**cluster_opts
):
w = self.particle_weights[cluster_particles]
l = self.particle_locations[cluster_particles]
yield (
cluster_label,
sum(w), # The zeroth moment is very useful here!
self.particle_mean(w, l),
self.particle_covariance_mtx(w, l)
)
def est_cluster_covs(self, cluster_opts=None):
# TODO: document
cluster_moments = np.array(
list(self.est_cluster_moments(cluster_opts=cluster_opts)),
dtype=[
('label', 'int'),
('weight', 'float64'),
('mean', '{}float64'.format(self.n_rvs)),
('cov', '{0},{0}float64'.format(self.n_rvs)),
])
ws = cluster_moments['weight'][:, np.newaxis, np.newaxis]
within_cluster_var = np.sum(ws * cluster_moments['cov'], axis=0)
between_cluster_var = self.particle_covariance_mtx(
# Treat the cluster means as a new very small particle cloud.
cluster_moments['weight'], cluster_moments['mean']
)
total_var = within_cluster_var + between_cluster_var
return within_cluster_var, between_cluster_var, total_var
def est_cluster_metric(self, cluster_opts=None):
"""
Returns an estimate of how much of the variance in the current posterior
can be explained by a separation between *clusters*.
"""
wcv, bcv, tv = self.est_cluster_covs(cluster_opts)
return np.diag(bcv) / np.diag(tv)
## REGION ESTIMATION METHODS ##############################################
def est_credible_region(self, level=0.95, return_outside=False, modelparam_slice=None):
"""
Returns an array containing particles inside a credible region of a
given level, such that the described region has probability mass
no less than the desired level.
Particles in the returned region are selected by including the highest-
weight particles first until the desired credibility level is reached.
:param float level: Crediblity level to report.
:param bool return_outside: If `True`, the return value is a tuple
of the those particles within the credible region, and the rest
of the posterior particle cloud.
:param slice modelparam_slice: Slice over which model parameters
to consider.
:rtype: :class:`numpy.ndarray`, shape ``(n_credible, n_mps)``,
where ``n_credible`` is the number of particles in the credible
region and ``n_mps`` corresponds to the size of ``modelparam_slice``.
If ``return_outside`` is ``True``, this method instead
returns tuple ``(inside, outside)`` where ``inside`` is as
described above, and ``outside`` has shape ``(n_particles-n_credible, n_mps)``.
:return: An array of particles inside the estimated credible region. Or,
if ``return_outside`` is ``True``, both the particles inside and the
particles outside, as a tuple.
"""
# which slice of modelparams to take
s_ = np.s_[modelparam_slice] if modelparam_slice is not None else np.s_[:]
mps = self.particle_locations[:, s_]
# Start by sorting the particles by weight.
# We do so by obtaining an array of indices `id_sort` such that
# `particle_weights[id_sort]` is in descending order.
id_sort = np.argsort(self.particle_weights)[::-1]
# Find the cummulative sum of the sorted weights.
cumsum_weights = np.cumsum(self.particle_weights[id_sort])
# Find all the indices where the sum is less than level.
# We first find id_cred such that
# `all(cumsum_weights[id_cred] <= level)`.
id_cred = cumsum_weights <= level
# By construction, by adding the next particle to id_cred, it must be
# true that `cumsum_weights[id_cred] >= level`, as required.
id_cred[np.sum(id_cred)] = True
# We now return a slice onto the particle_locations by first permuting
# the particles according to the sort order, then by selecting the
# credible particles.
if return_outside:
return (
mps[id_sort][id_cred],
mps[id_sort][np.logical_not(id_cred)]
)
else:
return mps[id_sort][id_cred]
def region_est_hull(self, level=0.95, modelparam_slice=None):
"""
Estimates a credible region over models by taking the convex hull of
a credible subset of particles.
:param float level: The desired crediblity level (see
:meth:`SMCUpdater.est_credible_region`).
:param slice modelparam_slice: Slice over which model parameters
to consider.
:return: The tuple ``(faces, vertices)`` where ``faces`` describes all the
vertices of all of the faces on the exterior of the convex hull, and
``vertices`` is a list of all vertices on the exterior of the
convex hull.
:rtype: ``faces`` is a ``numpy.ndarray`` with shape
``(n_face, n_mps, n_mps)`` and indeces ``(idx_face, idx_vertex, idx_mps)``
where ``n_mps`` corresponds to the size of ``modelparam_slice``.
``vertices`` is an ``numpy.ndarray`` of shape ``(n_vertices, n_mps)``.
"""
points = self.est_credible_region(
level=level,
modelparam_slice=modelparam_slice
)
hull = ConvexHull(points)
return points[hull.simplices], points[u.uniquify(hull.vertices.flatten())]
def region_est_ellipsoid(self, level=0.95, tol=0.0001, modelparam_slice=None):
r"""
Estimates a credible region over models by finding the minimum volume
enclosing ellipse (MVEE) of a credible subset of particles.
:param float level: The desired crediblity level (see
:meth:`SMCUpdater.est_credible_region`).
:param float tol: The allowed error tolerance in the MVEE optimization
(see :meth:`~qinfer.utils.mvee`).
:param slice modelparam_slice: Slice over which model parameters
to consider.
:return: A tuple ``(A, c)`` where ``A`` is the covariance
matrix of the ellipsoid and ``c`` is the center.
A point :math:`\vec{x}` is in the ellipsoid whenever
:math:`(\vec{x}-\vec{c})^{T}A^{-1}(\vec{x}-\vec{c})\leq 1`.
:rtype: ``A`` is ``np.ndarray`` of shape ``(n_mps,n_mps)`` and
``centroid`` is ``np.ndarray`` of shape ``(n_mps)``.
``n_mps`` corresponds to the size of ``param_slice``.
"""
_, vertices = self.region_est_hull(level=level, modelparam_slice=modelparam_slice)
A, centroid = u.mvee(vertices, tol)
return A, centroid
def in_credible_region(self, points, level=0.95, modelparam_slice=None, method='hpd-hull', tol=0.0001):
"""
Decides whether each of the points lie within a credible region
of the current distribution.
If ``tol`` is ``None``, the particles are tested directly against
the convex hull object. If ``tol`` is a positive ``float``,
particles are tested to be in the interior of the smallest
enclosing ellipsoid of this convex hull, see
:meth:`SMCUpdater.region_est_ellipsoid`.
:param np.ndarray points: An ``np.ndarray`` of shape ``(n_mps)`` for
a single point, or of shape ``(n_points, n_mps)`` for multiple points,
where ``n_mps`` corresponds to the same dimensionality as ``param_slice``.
:param float level: The desired crediblity level (see
:meth:`SMCUpdater.est_credible_region`).
:param str method: A string specifying which credible region estimator to
use. One of ``'pce'``, ``'hpd-hull'`` or ``'hpd-mvee'`` (see below).
:param float tol: The allowed error tolerance for those methods
which require a tolerance (see :meth:`~qinfer.utils.mvee`).
:param slice modelparam_slice: A slice describing which model parameters
to consider in the credible region, effectively marginizing out the
remaining parameters. By default, all model parameters are included.
:return: A boolean array of shape ``(n_points, )`` specifying whether
each of the points lies inside the confidence region.
Methods
~~~~~~~
The following values are valid for the ``method`` argument.
- ``'pce'``: Posterior Covariance Ellipsoid.
Computes the covariance
matrix of the particle distribution marginalized over the excluded
slices and uses the :math:`\chi^2` distribution to determine
how to rescale it such the the corresponding ellipsoid has
the correct size. The ellipsoid is translated by the
mean of the particle distribution. It is determined which
of the ``points`` are on the interior.
- ``'hpd-hull'``: High Posterior Density Convex Hull.
See :meth:`SMCUpdater.region_est_hull`. Computes the
HPD region resulting from the particle approximation, computes
the convex hull of this, and it is determined which
of the ``points`` are on the interior.
- ``'hpd-mvee'``: High Posterior Density Minimum Volume Enclosing Ellipsoid.
See :meth:`SMCUpdater.region_est_ellipsoid`
and :meth:`~qinfer.utils.mvee`. Computes the
HPD region resulting from the particle approximation, computes
the convex hull of this, and determines the minimum enclosing
ellipsoid. Deterimines which
of the ``points`` are on the interior.
"""
if method == 'pce':
s_ = np.s_[modelparam_slice] if modelparam_slice is not None else np.s_[:]
A = self.est_covariance_mtx()[s_, s_]
c = self.est_mean()[s_]
# chi-squared distribution gives correct level curve conversion
mult = st.chi2.ppf(level, c.size)
results = u.in_ellipsoid(points, mult * A, c)
elif method == 'hpd-mvee':
tol = 0.0001 if tol is None else tol
A, c = self.region_est_ellipsoid(level=level, tol=tol, modelparam_slice=modelparam_slice)
results = u.in_ellipsoid(points, np.linalg.inv(A), c)
elif method == 'hpd-hull':
# it would be more natural to call region_est_hull,
# but that function uses ConvexHull which has no
# easy way of determining if a point is interior.
# Here, Delaunay gives us access to all of the
# necessary simplices.
# this fills the convex hull with (n_mps+1)-dimensional
# simplices; the convex hull is an almost-everywhere
# disjoint union of these simplices
hull = Delaunay(self.est_credible_region(level=level, modelparam_slice=modelparam_slice))
# now we just check whether each of the given points are in
# any of the simplices. (http://stackoverflow.com/a/16898636/1082565)
results = hull.find_simplex(points) >= 0
return results
class ProductDistribution(Distribution):
r"""
Takes a non-zero number of QInfer distributions :math:`D_k` as input
and returns their Cartesian product.
In other words, the returned distribution is
:math:`\Pr(D_1, \dots, D_N) = \prod_k \Pr(D_k)`.
:param Distribution factors:
Distribution objects representing :math:`D_k`.
Alternatively, one iterable argument can be given,
in which case the factors are the values drawn from that iterator.
"""
def __init__(self, *factors):
if len(factors) == 1:
try:
self._factors = list(factors[0])
except:
self._factors = factors
else:
self._factors = factors
@property
def n_rvs(self):
return sum([f.n_rvs for f in self._factors])
def sample(self, n=1):
return np.hstack([f.sample(n) for f in self._factors])
_DEFAULT_RANGES = np.array([[0, 1]])
_DEFAULT_RANGES.flags.writeable = False # Prevent anyone from modifying the
# default ranges.
## CLASSES ###################################################################
class UniformDistribution(Distribution):
"""
Uniform distribution on a given rectangular region.
:param numpy.ndarray ranges: Array of shape ``(n_rvs, 2)``, where ``n_rvs``
is the number of random variables, specifying the upper and lower limits
for each variable.
"""
def __init__(self, ranges=_DEFAULT_RANGES):
if not isinstance(ranges, np.ndarray):
ranges = np.array(ranges)
if len(ranges.shape) == 1:
ranges = ranges[np.newaxis, ...]
self._ranges = ranges
self._n_rvs = ranges.shape[0]
self._delta = ranges[:, 1] - ranges[:, 0]
@property
def n_rvs(self):
return self._n_rvs
def sample(self, n=1):
shape = (n, self._n_rvs)# if n == 1 else (self._n_rvs, n)
z = np.random.random(shape)
return self._ranges[:, 0] + z * self._delta
def grad_log_pdf(self, var):
# THIS IS NOT TECHNICALLY LEGIT; BCRB doesn't technically work with a
# prior that doesn't go to 0 at its end points. But we do it anyway.
if var.shape[0] == 1:
return 12/(self._delta)**2
else:
return np.zeros(var.shape)
class ConstantDistribution(Distribution):
"""
Represents a determinstic variable; useful for combining with other
distributions, marginalizing, etc.
:param values: Shape ``(n,)`` array or list of values :math:`X_0` such that
:math:`\Pr(X) = \delta(X - X_0)`.
"""
def __init__(self, values):
self._values = np.array(values)[np.newaxis, :]
@property
def n_rvs(self):
return self._values.shape[1]
def sample(self, n=1):
return np.repeat(self._values, n, axis=0)
class NormalDistribution(Distribution):
"""
Normal or truncated normal distribution over a single random
variable.
:param float mean: Mean of the represented random variable.
:param float var: Variance of the represented random variable.
:param tuple trunc: Limits at which the PDF of this
distribution should be truncated, or ``None`` if
the distribution is to have infinite support.
"""
def __init__(self, mean, var, trunc=None):
self.mean = mean
self.var = var
if trunc is not None:
low, high = trunc
sigma = np.sqrt(var)
a = (low - mean) / sigma
b = (high - mean) / sigma
self.dist = partial(scipy_dist, 'truncnorm', a, b, loc=mean, scale=np.sqrt(var))
else:
self.dist = partial(scipy_dist, 'norm', mean, np.sqrt(var))
@property
def n_rvs(self):
return 1
def sample(self, n=1):
return self.dist().rvs(size=n)[:, np.newaxis]
def grad_log_pdf(self, x):
return -(x - self.mean) / self.var
class MultivariateNormalDistribution(Distribution):
"""
Multivariate (vector-valued) normal distribution.
:param np.ndarray mean: Array of shape ``(n_rvs, )``
representing the mean of the distribution.
:param np.ndarray cov: Array of shape ``(n_rvs, n_rvs)``
representing the covariance matrix of the distribution.
"""
def __init__(self, mean, cov):
# Flatten the mean first, so we have a strong guarantee about its
# shape.
self.mean = np.array(mean).flatten()
self.cov = cov
self.invcov = la.inv(cov)
@property
def n_rvs(self):
return self.mean.shape[0]
def sample(self, n=1):
return np.einsum("ij,nj->ni", la.sqrtm(self.cov), np.random.randn(n, self.n_rvs)) + self.mean
def grad_log_pdf(self, x):
return -np.dot(self.invcov, (x - self.mean).transpose()).transpose()
class SlantedNormalDistribution(Distribution):
r"""
Uniform distribution on a given rectangular region with
additive noise. Random variates from this distribution
follow :math:`X+Y` where :math:`X` is drawn uniformly
with respect to the rectangular region defined by ranges, and
:math:`Y` is normally distributed about 0 with variance
``weight**2``.
:param numpy.ndarray ranges: Array of shape ``(n_rvs, 2)``, where ``n_rvs``
is the number of random variables, specifying the upper and lower limits
for each variable.
:param float weight: Number specifying the inverse variance
of the additive noise term.
"""
def __init__(self, ranges=_DEFAULT_RANGES, weight=0.01):
if not isinstance(ranges, np.ndarray):
ranges = np.array(ranges)
if len(ranges.shape) == 1:
ranges = ranges[np.newaxis, ...]
self._ranges = ranges
self._n_rvs = ranges.shape[0]
self._delta = ranges[:, 1] - ranges[:, 0]
self._weight = weight
@property
def n_rvs(self):
return self._n_rvs
def sample(self, n=1):
shape = (n, self._n_rvs)# if n == 1 else (self._n_rvs, n)
z = np.random.randn(n, self._n_rvs)
return self._ranges[:, 0] + \
self._weight*z + \
np.random.rand(n, self._n_rvs)*self._delta[np.newaxis,:]
class LogNormalDistribution(Distribution):
"""
Log-normal distribution.
:param mu: Location parameter (numeric), set to 0 by default.
:param sigma: Scale parameter (numeric), set to 1 by default.
Must be strictly greater than zero.
"""
def __init__(self, mu=0, sigma=1):
self.mu = mu # lognormal location parameter
self.sigma = sigma # lognormal scale parameter
self.dist = partial(scipy_dist, 'lognorm', 1, mu, sigma) # scipy distribution location = 0
@property
def n_rvs(self):
return 1
def sample(self, n=1):
return self.dist().rvs(size=n)[:, np.newaxis]
class BetaDistribution(Distribution):
r"""
The beta distribution, whose pdf at :math:`x` is proportional to
:math:`x^{\alpha-1}(1-x)^{\beta-1}`.
Note that either ``alpha`` and ``beta``, or ``mean`` and ``var``, must be
specified as inputs;
either case uniquely determines the distribution.
:param float alpha: The alpha shape parameter of the beta distribution.
:param float beta: The beta shape parameter of the beta distribution.
:param float mean: The desired mean value of the beta distribution.
:param float var: The desired variance of the beta distribution.
"""
def __init__(self, alpha=None, beta=None, mean=None, var=None):
if alpha is not None and beta is not None:
self.alpha = alpha
self.beta = beta
self.mean = alpha / (alpha + beta)
self.var = alpha * beta / ((alpha + beta) ** 2 * (alpha + beta + 1))
elif mean is not None and var is not None:
self.mean = mean
self.var = var
self.alpha = mean ** 2 * (1 - mean) / var - mean
self.beta = (1 - mean) ** 2 * mean / var - (1 - mean)
else:
raise ValueError(
"BetaDistribution requires either (alpha and beta) "
"or (mean and var)."
)