简体   繁体   中英

Python truncated exponential distribution

I am trying to generate 3 random numbers that has truncated exponential distribution with distribution function of: F(x) = 1-(math.exp(-(x+100*math.log(1-((1-0.05)** (1/100))))/1.5)) , when x>-100*math.log(1-((1-0.05)**(1/100))) .

The problem is that I don't get the concept how to set x values when there is no upper boundary. Any ideas how to get those 3 numbers mathematically correct?

I am not sure about your distribution function, it looks a bit messy to me (note: (1-0.05)** (1/100) = (0.95**0.01)) but you can use the system max-bound as an upper bound to generate a random variable x using random.uniform() then you can compute F(x) to get the random value belonging to your distribution. This can be done like in the following:

import sys
import math 
import random 

def F(x):
    return 1-(math.exp(-(x + 100*math.log(1-(0.95**0.01)))/1.5))

def get_random_value_from_distribution():
    x = random.uniform(-100 * math.log(1- (0.95**0.01)), sys.maxsize)
    y = F(x)
    return y

print("x1:", get_random_value_from_distribution())
print("x2:", get_random_value_from_distribution())
print("x3:", get_random_value_from_distribution())

Note: Your distribution function is fixed to 1 in a certain interval, so you need to look into that: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM