0

My question might come across as stupid or so simple, but I could not work towards finding a solution. Here is my question: I want to write an exponential power distribution function which is available in scipy. However, I don't want to use the scipy for this. How do I go about it?

Here are my efforts so far:

import math
import numpy as np

def ExpPowerFun(x,b, size=1000):
        distribution = b*x**(b-1)*math.exp(1+x**b-math.exp(x**b))
        return distribution

I used this equation based on this scipy doc. To be fair, using this equation and writing a function using it doesn't do much. As you can see, it returns only one value. I want to generate a distribution of random numbers based on scipy's exponential power distribution function without using scipy. I have looked at class exponpow_gefrom github code. However, it uses scipy.special(-sc), so it's kind of useless for me, unless there is any workaround and avoids the use of scipy.

I can't figure out how to go about it. Again, this might be a simple task, but I am stuck. Please help.

yoonghm
  • 4,198
  • 1
  • 32
  • 48
  • Does this answer your question? [Vectorizing a function (Python)](https://stackoverflow.com/questions/19139869/vectorizing-a-function-python) – mkrieger1 Dec 07 '21 at 00:03

1 Answers1

0

the simplest way to generate a random number for a given distribution is using the inverse of the CDF of that function, the PPF (Percent point function) will give you the distribution you need when you apply it on uniform distributed numbers.

for you case the PPF (taken directly from scipy source code with some modifications) is:

np.power(np.log(1-np.log(1-x)), 1.0/b)

hence you code should look like this

def ExpPowerFun(b, size=1000):
    x = np.random.rand(size)
    return np.power(np.log(1-np.log(1-x)), 1.0/b)

import matplotlib.pyplot as plt
plt.hist(ExpPowerFun(2.7,10000),20)
plt.show()

Edit: the uniform distribution has to be from 0 to 1 ofc since the probabilities are from 0% to 100% enter image description here

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • Thank you so much. I think, this will work for me. I have one more question: to create a function which will resemble scipy's exponential power distribution function, we first need to create a uniform distribution and apply the PPF? I want to understand how this works. Thanks – surviving-grad Dec 07 '21 at 00:48
  • for a continuous PDF the CDF(X) is the probability of getting a number smaller than or equal to X. most of the time when dealing with numerical methods we just drop the smaller than sign and say that the CDF(X) is the probability of getting X from that distribution. – Ahmed AEK Dec 07 '21 at 06:57
  • since PPF is the inverse, it's like saying, if i have the probability CDF(X), what is X ? so it will map between your probability and the value that you will obtain if you are that lucky. lastly the probability itself is uniformly distributed since if you have a 100 face die, you are equally likely to roll any number of that die, and hence we can use a uniform distribution between 0 and 1 to simply determine the probability, and once we know how lucky we are, we can simply plug it into the PPF and get the value that corresponds to that amount of luck. – Ahmed AEK Dec 07 '21 at 07:09
  • Awesome, thanks so much! – surviving-grad Dec 13 '21 at 01:02