Search code examples
pythoncurve-fittingprobability-distributionweibull

Generate random sample for exponentiated Weibull distribution using Python


For a distribution for running this scipy function to detect the best fit as Exponentiated Weibull distribution and the function outputs 4 parameter values. But how to generate a sample list of data of size n that honours this kind of distribution.

I don't want to re-write function. Any python package which does this, would be helpful.

enter image description here


Solution

  • Usually you will use a ppf to generate from a rando seed.

    For a simple completely fake example, let's say we fit a uniform random variable (with values from 0 to 15) to a Weibull distribution. Then, create a seed random variable (from 0 to 1 because it's the value of the quantiles that you will get) and put it in the ppf function.

    import scipy.stats as st
    import numpy as np
    # fitting part
    samples = np.random.rand(10000)*15
    dist = st.exponweib.fit(samples)
    # generation part
    sample_seed = np.random.rand(10000)
    random_exponweib_samples = st.exponweib.ppf(sample_seed, *dist)
    # plotting
    import matplotlib.pyplot as plt
    plt.hist(samples, label="uniform", alpha=.5)
    plt.hist(random_exponweib_samples, label="exponweib", alpha=.5)
    plt.legend()
    plt.show()
    

    You'll have something like the following. enter image description here

    Please be careful and check for the documentation of the ppf concerning the weibull distrubiton. In my function st.exponweib.ppf(sample_seed, *dist) I just use *dist but it might be the case that the parameters should be sent differently, you can see it with the form of the orange plot which might not be correct.