|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# (C) British Crown Copyright 2017-2021 Met Office. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of the copyright holder nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +# POSSIBILITY OF SUCH DAMAGE. |
| 31 | +""" |
| 32 | +This module defines the truncnorm as per scipy v1.3.3 to overcome performance |
| 33 | +issue introduced in later versions: |
| 34 | +- https://github.com/scipy/scipy/issues/12370 |
| 35 | +- https://github.com/scipy/scipy/issues/12733 |
| 36 | +
|
| 37 | +""" |
| 38 | +import numpy as np |
| 39 | +import scipy.special as sc |
| 40 | +from scipy.stats._distn_infrastructure import rv_continuous |
| 41 | + |
| 42 | +# ============================================================================ |
| 43 | +# | Copyright SciPy | |
| 44 | +# | Code from this point unto the termination banner is copyright SciPy. | |
| 45 | +# | | |
| 46 | +# | Copyright © 2001, 2002 Enthought, Inc. | |
| 47 | +# | All rights reserved. | |
| 48 | +# | | |
| 49 | +# | Copyright © 2003-2019 SciPy Developers. | |
| 50 | +# | All rights reserved. | |
| 51 | +# | | |
| 52 | +# | Redistribution and use in source and binary forms, with or without | |
| 53 | +# | modification, are permitted provided that the following conditions are | |
| 54 | +# | met: | |
| 55 | +# | | |
| 56 | +# | Redistributions of source code must retain the above copyright notice, | |
| 57 | +# | this list of conditions and the following disclaimer. | |
| 58 | +# | | |
| 59 | +# | - Redistributions in binary form must reproduce the above copyright | |
| 60 | +# | notice, this list of conditions and the following disclaimer in the | |
| 61 | +# | documentation and/or other materials provided with the distribution. | |
| 62 | +# | - Neither the name of Enthought nor the names of the SciPy Developers | |
| 63 | +# | may be used to endorse or promote products derived from this software | |
| 64 | +# | without specific prior written permission. | |
| 65 | +# | | |
| 66 | +# | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 67 | +# | “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 68 | +# | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
| 69 | +# | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR | |
| 70 | +# | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 71 | +# | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 72 | +# | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 73 | +# | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 74 | +# | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 75 | +# | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 76 | +# | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 77 | +# | | |
| 78 | +# | Further details can be found at scipy.org/scipylib/license.html | |
| 79 | +# ============================================================================ |
| 80 | + |
| 81 | +# Source: https://github.com/scipy/scipy/blob/v1.3.3/scipy/stats/_continuous_\ |
| 82 | +# distns.py |
| 83 | + |
| 84 | + |
| 85 | +_norm_pdf_C = np.sqrt(2 * np.pi) |
| 86 | +_norm_pdf_logC = np.log(_norm_pdf_C) |
| 87 | + |
| 88 | + |
| 89 | +def _norm_pdf(x): |
| 90 | + return np.exp(-(x ** 2) / 2.0) / _norm_pdf_C |
| 91 | + |
| 92 | + |
| 93 | +def _norm_logpdf(x): |
| 94 | + return -(x ** 2) / 2.0 - _norm_pdf_logC |
| 95 | + |
| 96 | + |
| 97 | +def _norm_cdf(x): |
| 98 | + return sc.ndtr(x) |
| 99 | + |
| 100 | + |
| 101 | +def _norm_ppf(q): |
| 102 | + return sc.ndtri(q) |
| 103 | + |
| 104 | + |
| 105 | +def _norm_sf(x): |
| 106 | + return _norm_cdf(-x) |
| 107 | + |
| 108 | + |
| 109 | +def _norm_isf(q): |
| 110 | + return -_norm_ppf(q) |
| 111 | + |
| 112 | + |
| 113 | +class truncnorm_gen(rv_continuous): |
| 114 | + r"""A truncated normal continuous random variable. |
| 115 | +
|
| 116 | + %(before_notes)s |
| 117 | +
|
| 118 | + Notes |
| 119 | + ----- |
| 120 | + The standard form of this distribution is a standard normal truncated to |
| 121 | + the range [a, b] --- notice that a and b are defined over the domain of the |
| 122 | + standard normal. To convert clip values for a specific mean and standard |
| 123 | + deviation, use:: |
| 124 | +
|
| 125 | + a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std |
| 126 | +
|
| 127 | + `truncnorm` takes :math:`a` and :math:`b` as shape parameters. |
| 128 | +
|
| 129 | + %(after_notes)s |
| 130 | +
|
| 131 | + %(example)s |
| 132 | +
|
| 133 | + """ |
| 134 | + |
| 135 | + def _argcheck(self, a, b): |
| 136 | + return a < b |
| 137 | + |
| 138 | + def _get_support(self, a, b): |
| 139 | + return a, b |
| 140 | + |
| 141 | + def _get_norms(self, a, b): |
| 142 | + _nb = _norm_cdf(b) |
| 143 | + _na = _norm_cdf(a) |
| 144 | + _sb = _norm_sf(b) |
| 145 | + _sa = _norm_sf(a) |
| 146 | + _delta = np.where(a > 0, _sa - _sb, _nb - _na) |
| 147 | + with np.errstate(divide="ignore"): |
| 148 | + return _na, _nb, _sa, _sb, _delta, np.log(_delta) |
| 149 | + |
| 150 | + def _pdf(self, x, a, b): |
| 151 | + ans = self._get_norms(a, b) |
| 152 | + _delta = ans[4] |
| 153 | + return _norm_pdf(x) / _delta |
| 154 | + |
| 155 | + def _logpdf(self, x, a, b): |
| 156 | + ans = self._get_norms(a, b) |
| 157 | + _logdelta = ans[5] |
| 158 | + return _norm_logpdf(x) - _logdelta |
| 159 | + |
| 160 | + def _cdf(self, x, a, b): |
| 161 | + ans = self._get_norms(a, b) |
| 162 | + _na, _delta = ans[0], ans[4] |
| 163 | + return (_norm_cdf(x) - _na) / _delta |
| 164 | + |
| 165 | + def _ppf(self, q, a, b): |
| 166 | + # XXX Use _lazywhere... |
| 167 | + ans = self._get_norms(a, b) |
| 168 | + _na, _nb, _sa, _sb = ans[:4] |
| 169 | + ppf = np.where( |
| 170 | + a > 0, |
| 171 | + _norm_isf(q * _sb + _sa * (1.0 - q)), |
| 172 | + _norm_ppf(q * _nb + _na * (1.0 - q)), |
| 173 | + ) |
| 174 | + return ppf |
| 175 | + |
| 176 | + |
| 177 | +truncnorm = truncnorm_gen(name="truncnorm") |
| 178 | + |
| 179 | + |
| 180 | +# ============================================================================ |
| 181 | +# | END SciPy copyright | |
| 182 | +# ============================================================================ |
0 commit comments