def test_exponentiated_weibull_distribution_cdf():
    """
    Tests the CDF of the exponentiated Weibull distribution.
    """
    # Define dist with parameters from the distribution fitted to
    # dataset A with the MLE in https://arxiv.org/pdf/1911.12835.pdf .
    dist = ExponentiatedWeibullDistribution(alpha=0.0373,
                                            beta=0.4743,
                                            delta=46.6078)

    # CDF(1) should be roughly 0.7, see Figure 12 in
    # https://arxiv.org/pdf/1911.12835.pdf .
    p = dist.cdf(1)
    np.testing.assert_allclose(p, 0.7, atol=0.1)

    # CDF(4) should be roughly 0.993, see Figure 12 in
    # https://arxiv.org/pdf/1911.12835.pdf .
    p = dist.cdf(4)
    assert p > 0.99
    assert p < 0.999

    # CDF(negative value) should be 0
    p = dist.cdf(-1)
    assert p == 0
def test_ExponentiatedWeibull_pdf_cdf_icdf(exp_weibull_reference_data):
    OMAE2020_param = {"alpha": 10.0, "beta": 2.42, "delta": 0.761}
    x = np.linspace(2, 15, num=100)
    p = np.linspace(0.01, 0.99, num=100)
    my_expweibull = ExponentiatedWeibullDistribution(**OMAE2020_param)
    my_pdf = my_expweibull.pdf(x)
    my_cdf = my_expweibull.cdf(x)
    my_icdf = my_expweibull.icdf(p)

    ref_pdf = exp_weibull_reference_data["ref_pdf"]
    ref_cdf = exp_weibull_reference_data["ref_cdf"]
    ref_icdf = exp_weibull_reference_data["ref_icdf"]

    np.testing.assert_allclose(my_pdf, ref_pdf)
    np.testing.assert_allclose(my_cdf, ref_cdf)
    np.testing.assert_allclose(my_icdf, ref_icdf)
def test_fitting_exponentiated_weibull():
    """
    Tests estimating the parameters of the  exponentiated Weibull distribution.
    """

    dist = ExponentiatedWeibullDistribution()

    # Draw 1000 samples from a Weibull distribution with shape=1.5 and scale=3,
    # which represents significant wave height.
    hs = sts.weibull_min.rvs(1.5, loc=0, scale=3, size=1000, random_state=42)

    dist.fit(hs, method="wlsq", weights="quadratic")

    # shape parameter/ beta should be about 1.5.
    assert dist.parameters["beta"] > 1
    assert dist.parameters["beta"] < 2

    # scale parameter / alpha should be about 3.
    assert dist.parameters["alpha"] > 2
    assert dist.parameters["alpha"] < 4

    # shape2 parameter / delta should be about 1.
    assert dist.parameters["delta"] > 0.5
    assert dist.parameters["delta"] < 2

    dist = ExponentiatedWeibullDistribution(f_delta=1)

    dist.fit(hs, method="wlsq", weights="quadratic")

    # shape parameter/ beta should be about 1.5.
    assert dist.parameters["beta"] > 1
    assert dist.parameters["beta"] < 2

    # scale parameter / alpha should be about 3.
    assert dist.parameters["alpha"] > 2
    assert dist.parameters["alpha"] < 4

    # shape2 parameter / delta should be 1.
    assert dist.parameters["delta"] == 1

    # Check whether the fitted distribution has a working CDF and PDF.
    assert dist.cdf(2) > 0
    assert dist.pdf(2) > 0
def test_ExponentiatedWeibull_wlsq_fit(exp_weibull_reference_data_wlsq_fit):
    x = np.linspace(2, 15, num=100)
    p = np.linspace(0.01, 0.99, num=100)

    true_alpha = 10
    true_beta = 2.42
    true_delta = 0.761
    expweibull_samples = sts.exponweib.rvs(a=true_delta,
                                           c=true_beta,
                                           loc=0,
                                           scale=true_alpha,
                                           size=100,
                                           random_state=42)

    my_expweibull = ExponentiatedWeibullDistribution()

    my_expweibull.fit(expweibull_samples, method="lsq", weights="quadratic")

    my_pdf = my_expweibull.pdf(x)
    my_cdf = my_expweibull.cdf(x)
    my_icdf = my_expweibull.icdf(p)
    my_alpha = my_expweibull.alpha
    my_beta = my_expweibull.beta
    my_delta = my_expweibull.delta

    ref_pdf = exp_weibull_reference_data_wlsq_fit["ref_pdf"]
    ref_cdf = exp_weibull_reference_data_wlsq_fit["ref_cdf"]
    ref_icdf = exp_weibull_reference_data_wlsq_fit["ref_icdf"]
    ref_alpha = exp_weibull_reference_data_wlsq_fit["ref_alpha"]
    ref_beta = exp_weibull_reference_data_wlsq_fit["ref_beta"]
    ref_delta = exp_weibull_reference_data_wlsq_fit["ref_delta"]

    np.testing.assert_allclose(my_alpha, ref_alpha)
    np.testing.assert_allclose(my_beta, ref_beta)
    np.testing.assert_allclose(my_delta, ref_delta)
    np.testing.assert_allclose(my_pdf, ref_pdf)
    np.testing.assert_allclose(my_cdf, ref_cdf)
    np.testing.assert_allclose(my_icdf, ref_icdf)
true_alpha = 10
true_beta = 2.42
true_delta = 0.761
expweibull_samples = sts.exponweib.rvs(
    a=true_delta, c=true_beta, loc=0, scale=true_alpha, size=100, random_state=42
)

# %%
# my_expweibull = ExponentiatedWeibullDistribution(**OMAE2020_param)
my_expweibull = ExponentiatedWeibullDistribution()

my_expweibull.fit(expweibull_samples, method="lsq", weights="quadratic")

my_pdf = my_expweibull.pdf(x)
my_cdf = my_expweibull.cdf(x)
my_icdf = my_expweibull.icdf(p)

my_alpha = my_expweibull.alpha
my_beta = my_expweibull.beta
my_delta = my_expweibull.delta

# %%
import sys

sys.path.append("../viroconcom")
from viroconcom.distributions import ExponentiatedWeibullDistribution

# ref_expweibull = ExponentiatedWeibullDistribution(shape=OMAE2020_param["beta"],
#                                                   scale=OMAE2020_param["alpha"],
#                                                   shape2=OMAE2020_param["delta"])