| 829 | # from scipy: https://github.com/scipy/scipy/blob/master/scipy/stats/kde.py |
| 830 | |
| 831 | def __init__(self, dataset, bw_method=None): |
| 832 | self.dataset = np.atleast_2d(dataset) |
| 833 | if not np.array(self.dataset).size > 1: |
| 834 | raise ValueError("`dataset` input should have multiple elements.") |
| 835 | |
| 836 | self.dim, self.num_dp = np.array(self.dataset).shape |
| 837 | |
| 838 | if bw_method is None: |
| 839 | pass |
| 840 | elif cbook._str_equal(bw_method, 'scott'): |
| 841 | self.covariance_factor = self.scotts_factor |
| 842 | elif cbook._str_equal(bw_method, 'silverman'): |
| 843 | self.covariance_factor = self.silverman_factor |
| 844 | elif isinstance(bw_method, Number): |
| 845 | self._bw_method = 'use constant' |
| 846 | self.covariance_factor = lambda: bw_method |
| 847 | elif callable(bw_method): |
| 848 | self._bw_method = bw_method |
| 849 | self.covariance_factor = lambda: self._bw_method(self) |
| 850 | else: |
| 851 | raise ValueError("`bw_method` should be 'scott', 'silverman', a " |
| 852 | "scalar or a callable") |
| 853 | |
| 854 | # Computes the covariance matrix for each Gaussian kernel using |
| 855 | # covariance_factor(). |
| 856 | |
| 857 | self.factor = self.covariance_factor() |
| 858 | # Cache covariance and inverse covariance of the data |
| 859 | if not hasattr(self, '_data_inv_cov'): |
| 860 | self.data_covariance = np.atleast_2d( |
| 861 | np.cov( |
| 862 | self.dataset, |
| 863 | rowvar=1, |
| 864 | bias=False)) |
| 865 | self.data_inv_cov = np.linalg.inv(self.data_covariance) |
| 866 | |
| 867 | self.covariance = self.data_covariance * self.factor ** 2 |
| 868 | self.inv_cov = self.data_inv_cov / self.factor ** 2 |
| 869 | self.norm_factor = (np.sqrt(np.linalg.det(2 * np.pi * self.covariance)) |
| 870 | * self.num_dp) |
| 871 | |
| 872 | def scotts_factor(self): |
| 873 | return np.power(self.num_dp, -1. / (self.dim + 4)) |