ajrlewis
ajrlewis

Reputation: 3058

Split-normal distribution

What's the best way to compute a split-normal distribution given a mean value with an upper and lower error?

So far I have:

from random import choice, gauss
def random_split_normal(mu: float, upper_sigma: float, lower_sigma:int) -> float:
    return abs(gauss(0.0, 1.0)) * choice([upper_sigma, -lower_sigma]) + mu

which I call lots of times to generate an array:

random_array = []
for _ in range(1000):
    random_array.append(random_split_normal(1.0, 2.0, 1.0))

which yields the following result when making a histogram of random_array:

enter image description here

I'm wondering if randomly using either upper_sigma or lower_sigma is the right way to do this?

Upvotes: 2

Views: 1104

Answers (1)

Alex F
Alex F

Reputation: 955

Your solution is mathematically equivalent, but less efficient since you're unnecessarily applying abs() and choice() where you can just look at the gauss()'s sign.

This should be close to the definition. I've also reoredred the sigmas to coincide with our usual numberline orientation (negative infinity on the left)

def random_split_normal(mu: float, lower_sigma: float, upper_sigma: float) -> float:
    z = gauss(0, 1)
    return mu + z * (lower_sigma if z < 0 else upper_sigma)

Upvotes: 2

Related Questions