• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dsavransky / EXOSIMS / 11294204788

11 Oct 2024 02:30PM UTC coverage: 66.032%. First build
11294204788

Pull #395

github

web-flow
Merge 79a05365c into 18ce85989
Pull Request #395: np.Inf and np.array(obj, copy = False) changed

86 of 104 new or added lines in 26 files covered. (82.69%)

9543 of 14452 relevant lines covered (66.03%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/EXOSIMS/util/InverseTransformSampler.py
1
import numpy as np
1✔
2
from scipy.interpolate import interp1d
1✔
3
import numbers
1✔
4
from EXOSIMS.util._numpy_compat import copy_if_needed
1✔
5

6

7
class InverseTransformSampler:
1✔
8
    """
9
    Approximate Inverse Transform Sampler for arbitrary distributions
10
    defined via a PDF encoded as a function (or lambda function)
11

12
    Args:
13
        f (function):
14
            Probability density function.  Must be able to operate
15
            on numpy ndarrays.  Function does not need to be
16
            normalized over the sampling interval.
17
        xMin (float):
18
            Minimum of interval to sample (inclusive).
19
        xMax (float):
20
            Maximum of interval to sample (inclusive).
21
        nints (int):
22
            Number of intervals to use in approximating CDF.
23
            Defaults to 10000
24

25
    Attributes:
26
        f, xMin, xMax
27
            As above
28

29

30
    Notes:
31
        If xMin == xMax, return values will all exactly equal xMin.
32
        To sample call the object with the desired number of samples.
33
    """
34

35
    def __init__(self, f, xMin, xMax, nints=10000):
1✔
36

37
        # validate inputs
38
        assert (
1✔
39
            isinstance(xMin, numbers.Number)
40
            and isinstance(xMax, numbers.Number)
41
            and isinstance(nints, numbers.Number)
42
        ), "xMin, xMax, and nints must be numbers."
43
        self.xMin = float(xMin)
1✔
44
        self.xMax = float(xMax)
1✔
45
        nints = int(nints)
1✔
46

47
        assert hasattr(f, "__call__"), "f must be callable."
1✔
48

49
        if self.xMin != self.xMax:
1✔
50
            ints = np.linspace(self.xMin, self.xMax, nints + 1)  # interval edges
1✔
51
            x = np.diff(ints) / 2.0 + ints[:-1]  # interval midpoints
1✔
52
            fX = f(x)
1✔
53
            if not isinstance(fX, np.ndarray):
1✔
NEW
54
                fX = np.array(fX, copy=copy_if_needed, ndmin=1)
×
55

56
            if len(fX) == 1:
1✔
57
                fX = float(fX) * np.ones(x.shape)
1✔
58
            F = np.hstack([0, np.cumsum(fX)])
1✔
59
            F /= F[-1]
1✔
60

61
            self.Finv = interp1d(F, ints)
1✔
62

63
    def __call__(self, numTest=1):
1✔
64
        """
65
        A call to the object with the number of samples will
66
        return the sampled distribution.
67
        """
68

69
        assert isinstance(numTest, numbers.Number), "numTest must be an integer."
1✔
70
        numTest = int(numTest)
1✔
71

72
        if self.xMin == self.xMax:
1✔
73
            return np.zeros(numTest) + self.xMin
1✔
74

75
        return self.Finv(np.random.uniform(size=numTest))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc