Search code examples
pythonnumpystatisticsexponential-distribution

How to generate N numbers from 0-1 that follows exponential decay pattern (in Python)?


I would like to generate 320 numbers (X1, X2.... X320) ranging from 0-1 across 320 days:

Date | Value
2020-03-18 X1
2020-03-19 X2

...
2021-01-31 X320

So that when I plot "Value" against "Date", the numbers follow exponential decay pattern across time.

What is the right & efficient way to do that in Python? Have been struggling to figure this out.
The easy part is to generate 320 numbers that all together fit exponential decay pattern, but now I would want those 320 numbers to have that pattern across time - that's the difficult part.

Thanks so much for your help!


Solution

  • Use numpy and the exponential decay equation. Refer to the code.

    def exponential_decay(a, b, N):
        # a, b: exponential decay parameter
        # N: number of samples 
        return a * (1-b) ** np.arange(N)
    
    print(exponential_decay(1, 0.5, 320))