forked from fonnesbeck/pymc_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangular.py
33 lines (22 loc) · 1.02 KB
/
triangular.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from numpy import log, random, sqrt, zeros, atleast_1d
def triangular_like(x, mode, minval, maxval):
"""Log-likelihood of triangular distribution"""
x = atleast_1d(x)
# Check for support
if any(x<minval) or any(x>maxval): return -inf
# Likelihood of left values
like = sum(log(2*(x[x<=mode] - minval)) - log(mode - minval) - log(maxval - minval))
# Likelihood of right values
like += sum(log(2*(maxval - x[x>mode])) - log(maxval - minval) - log(maxval - mode))
return like
def rtriangular(mode, minval, maxval, size=1):
"""Generate triangular random numbers"""
# Uniform random numbers
z = atleast_1d(random.random(size))
# Threshold for transformation
threshold = (mode - minval)/(maxval - minval)
# Transform uniforms
u = atleast_1d(zeros(size))
u[z<=threshold] = minval + sqrt(z[z<=threshold]*(maxval - minval)*(mode - minval))
u[z>threshold] = maxval - sqrt((1 - z[z>threshold])*(maxval - minval)*(maxval - mode))
return u