Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Complex Distributions (Mixture, Competing Risks, DSZI) quantile and inverse_SF functions error when input is array-like #49

Open
ctenold opened this issue May 2, 2024 · 0 comments

Comments

@ctenold
Copy link

ctenold commented May 2, 2024

Describe the bug
quantile() and inverse_SF() functions work fine with a single numeric input, but error out when provided an array or list

To Reproduce

from reliability.Distributions import Mixture_Model, Weibull_Distribution

one = Weibull_Distribution(alpha=0.04, beta=2.2)
two = Weibull_Distribution(alpha=0.18, beta=1.3)
p = 0.43
mixture = Mixture_Model([one, two], [p, 1-p] )
mixture.quantile([.1, .2])

Returns: ValueError: operands could not be broadcast together with shapes (1000000,) (2,)

Additional context
Here's an example of how I fixed it:

  • if q is a number, convert to list to avoid unpack_single_arrays() error
  • Vectorize the ppf function
    def quantile(self, q):
        """
        Quantile calculator

        Parameters
        ----------
        q : float, list, array
            Quantile to be calculated. Must be between 0 and 1.

        Returns
        -------
        x : float
            The inverse of the CDF at q. This is the probability that a random
            variable from the distribution is < q
        """
        if type(q) in [int, float, np.float64]:
            if q < 0 or q > 1:
                raise ValueError("Quantile must be between 0 and 1")
            else:
                q=[q]
        elif type(q) in [list, np.ndarray]:
            if min(q) < 0 or max(q) > 1:
                raise ValueError("Quantile must be between 0 and 1")
        else:
            raise ValueError("Quantile must be of type float, list, array")
        # ppf = self.__xvals_init[np.argmin(abs(self.__cdf_init - q))]
        def mixture_ppf(q):
            return self.__xvals_init[np.argmin(abs(self.__cdf_init - q))]
        mixture_ppf_vec = np.vectorize(mixture_ppf)
        ppf = mixture_ppf_vec(q)
        return unpack_single_arrays(ppf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant