Skip to content

Commit 157b86f

Browse files
Removed redundant example but added existing splitting example to examples.rst (#3)
* updated some docstrings * example documentation updates --------- Co-authored-by: Keith LeGrand <keithalegrand@gmail.com>
1 parent f25d59a commit 157b86f

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

docs/examples.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
Examples
22
========
33

4-
This section provides examples of using pyest for various applications.
4+
This section provides examples of using PyEst for various applications.
5+
6+
Splitting and Plotting PyEst Gaussian Mixtures
7+
---------------------------------------------------------------------
8+
9+
This example demonstrates using PyEst to split a mixand in a Gaussian mixture, and how to plot the resulting Gaussian mixture:
10+
11+
.. literalinclude:: ../examples/example_gm_2d_split.py
12+
:language: python
513

614
Gaussian Mixture Splitting for Field-of-View and Negative Information
715
---------------------------------------------------------------------
@@ -23,7 +31,13 @@ Cislunar Space Object Uncertainty Propagation
2331
---------------------------------------------
2432

2533
This example shows how to use pyest for propagating uncertainty in the circular restricted three-body problem
26-
(CR3BP):
34+
(CR3BP).
35+
36+
.. note::
37+
This example utilizes a cache of precomputed Monte Carlo samples to evaluate various performance measures.
38+
If the cache is not available, this example will generate new samples and store them in a cache for future use.
39+
On first run, this may take a few minutes to build the cache. These samples are only for performance evaluation
40+
and not required for any of the adaptive Gaussian splitting operations.
2741

2842
.. literalinclude:: ../examples/example_splitting_cislunar.py
2943
:language: python

pyest/gm/gm.py

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def equal_weights(L):
188188
----------
189189
L : int
190190
number of weights
191+
191192
Returns
192193
-------
193194
ndarray
@@ -200,8 +201,8 @@ def eval_gmpdf(x, w, m, P):
200201
"""
201202
evaluates Gaussian mixture at points x
202203
203-
Required
204-
--------
204+
Parameters
205+
----------
205206
x : array_like
206207
(n_samp, vect_length) points at which to evaluate the GM
207208
w : array_like
@@ -229,8 +230,8 @@ def eval_gmpdfchol(x, w, m, S):
229230
"""
230231
evaluates Gaussian mixture at points x
231232
232-
Required
233-
--------
233+
Parameters
234+
----------
234235
x : array_like
235236
(n_samp, vect_length) points at which to evaluate the GM
236237
w : array_like
@@ -258,8 +259,8 @@ def optimized_eval_gmpdf(x, w, m, Schol, log_pdet):
258259
"""
259260
Evaluates Gaussian mixture at points x
260261
261-
Required
262-
--------
262+
Parameters
263+
----------
263264
x : array_like
264265
(n_samp, vect_length) points at which to evaluate the GM
265266
w : array_like
@@ -416,8 +417,8 @@ def eig_sqrt_factor(P):
416417
def integral_gauss_product_chol(m1, S1, m2, S2):
417418
''' compute integral of product of two Gaussians
418419
419-
Required
420-
--------
420+
Parameters
421+
----------
421422
m1: np.ndarray
422423
mean of Gaussian 1
423424
S1: np.ndarray
@@ -442,8 +443,8 @@ def integral_gauss_product_chol(m1, S1, m2, S2):
442443
def integral_gauss_product(m1, P1, m2, P2, allow_singular=False):
443444
''' compute integral of product of two Gaussians
444445
445-
Required
446-
--------
446+
Parameters
447+
----------
447448
m1: np.ndarray
448449
mean of Gaussian 1
449450
P1: np.ndarray
@@ -498,8 +499,8 @@ def print_3d_mat(A):
498499
def gm_pdf_2d(w, m, P, dimensions=(0, 1), res=100, xbnd=None, ybnd=None):
499500
""" evaluate GM pdf in two dimensions
500501
501-
Required
502-
--------
502+
Parameters
503+
----------
503504
w: ndarray
504505
(nC,) weights
505506
m: ndarray
@@ -595,9 +596,22 @@ class GaussianMixture(object):
595596

596597
def __init__(self, w, m, cov, cov_type='full', Seig=None):
597598
"""
598-
w is a 1d array
599-
m is a 2d array (nC,nx)
600-
cov is a 3d array (nC,nx,nx)
599+
Parameters
600+
----------
601+
w: array_like
602+
(nC,) array of mixand weights
603+
m: array_like
604+
(nC,nx) array of mixand means
605+
cov: array_like
606+
(nC,nx,nx) array of covariances. Covariances can be specified in
607+
different ways, as specified by the optional cov_type argument
608+
609+
Optional
610+
--------
611+
cov_type: str
612+
type of covariance matrix. Defaults to 'full'.
613+
Seig: 3d array
614+
eigenvalues and eigenvectors of the covariance matrix. Defaults to None.
601615
"""
602616
self.set_w(w)
603617
self.set_m(m)
@@ -658,24 +672,42 @@ def _compute_eig_sqrt_factors(self):
658672
return np.array([eig_sqrt_factor(cov.covariance) for cov in self._cov])
659673

660674
def get_size(self):
675+
"""
676+
Returns the number of components in the Gaussian mixture model.
677+
"""
661678
return len(self.w)
662679

663680
# def _set_size(self):
664681
# self._size = len(self._w)
665682

666683
def get_w(self):
684+
"""
685+
Returns the weights (1d array) of the Gaussian mixture model.
686+
"""
667687
return self._w
668688

669689
def set_w(self, w):
690+
"""
691+
Sets the weights (1d array) of the Gaussian mixture model.
692+
"""
670693
self._w = np.atleast_1d(w)
671694

672695
def get_m(self):
696+
"""
697+
Returns the means (2d array (nC,nx)) of the Gaussian mixture model.
698+
"""
673699
return self._m
674700

675701
def set_m(self, m):
702+
"""
703+
Sets the means (2d array (nC,nx)) of the Gaussian mixture model.
704+
"""
676705
self._m = np.atleast_2d(m)
677706

678707
def get_P(self, ind=None):
708+
"""
709+
Returns the covariance matrices (3d array (nC,nx,nx)) of the Gaussian mixture model.
710+
"""
679711
if ind is not None:
680712
if np.isscalar(ind):
681713
return self._cov[ind].covariance
@@ -684,6 +716,9 @@ def get_P(self, ind=None):
684716
return np.array([P.covariance for P in self._cov])
685717

686718
def set_P(self, P):
719+
"""
720+
Sets the covariance matrices (3d array (nC,nx,nx)) of the Gaussian mixture model.
721+
"""
687722
self._set_cov(P, 'full')
688723

689724
def _set_cov(self, cov, cov_type):
@@ -713,6 +748,9 @@ def _set_cov(self, cov, cov_type):
713748
raise ValueError('cov_type must be one of "full", "cholesky", or "eigendecomposition"')
714749

715750
def set_Seig(self, S):
751+
"""
752+
Sets the covariance matrix eigenvalues and eigenvectors.
753+
"""
716754
S = np.atleast_2d(S)
717755
if S.ndim == 2:
718756
self._Seig = S[np.newaxis, :, :]
@@ -804,11 +842,23 @@ def eval(self, x):
804842
return _squeeze_output(optimized_eval_gmpdf(x, self.w, self.m, self.Schol, self.log_pdet))
805843

806844
def mean(self):
807-
""" return the mean of the Gaussian mixture """
845+
""" return the mean of the distribution
846+
847+
Returns
848+
-------
849+
ndarray
850+
(nx,) distribution mean
851+
"""
808852
return np.sum(self.w[:, np.newaxis] * self.m, axis=0)
809853

810854
def cov(self):
811-
""" return the conditional covariance """
855+
""" return covariance of the distribution
856+
857+
Returns
858+
-------
859+
ndarray
860+
(nx,nx) full covariance matrix of distribution
861+
"""
812862
wsum = np.sum(self.w)
813863
mean = self.mean()
814864
return np.sum([
@@ -860,7 +910,19 @@ def pdf_2d(self, dimensions=(0, 1), res=100, xbnd=None, ybnd=None):
860910
return gm_pdf_2d(self.w, self.m, self.P, dimensions, res, xbnd, ybnd)
861911

862912
def pop(self, idx):
863-
""" remove and return component by index """
913+
"""
914+
remove and return component by index
915+
916+
Required
917+
--------
918+
idx: int
919+
index of component to remove
920+
921+
Returns
922+
-------
923+
tuple
924+
(weight, mean, covariance) of removed component
925+
"""
864926
w, m, P = self[idx]
865927
self._w = np.delete(self._w, idx, 0)
866928
self._m = np.delete(self._m, idx, 0)

0 commit comments

Comments
 (0)