-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.py
144 lines (113 loc) · 3.69 KB
/
transforms.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
def logit(x):
"""
Logit function
Args:
x (TYPE): x
Returns:
TYPE: transformed x
"""
return np.log(x / (1.0 - x))
def invLogit(x):
"""
Inverse logit function
Args:
x (TYPE): x
Returns:
TYPE: transformed x
"""
return 1.0 / (1.0 + np.exp(-x))
def lrBoundedBackward(x, lower, upper):
"""
Transform from physical parameters bounded in the interval [lower,upper] to transformed (unconstrained) ones
Args:
x (TYPE): vector of parameters
lower (float): vector with lower limits
upper (float): vector with upper limits
Returns:
TYPE: transformed vector of parameters
"""
return logit( (x-lower) / (upper - lower) )
def lrBoundedForward(x, lower, upper):
"""
Transform from transformed (unconstrained) parameters to physical ones bounded in the interval [lower,upper]
Args:
x (float): vector of transformed parameters
lower (float): vector with lower limits
upper (float): vector with upper limits
Returns:
Float: transformed variables and log Jacobian
"""
temp = invLogit(x)
return lower + (upper - lower) * temp, np.log(upper - lower) + np.log(temp) + np.log(1.0 - temp)
def lBoundedBackward(x, lower):
"""
Transform from physical parameters with lower limit to transformed (unconstrained) ones
Args:
x (float): vector of parameters
lower (float): vector with lower limits
Returns:
Float: transformed vector of parameters
"""
return np.log(x-lower)
def lBoundedForward(x, lower):
"""
Transform from transformed (unconstrained) parameters to physical ones with upper limit
Args:
x (float): vector of transformed parameters
lower (float): vector with lower limits
Returns:
Float: transformed variables and log Jacobian
"""
return np.exp(x) + lower, x
def rBoundedBackward(x, upper):
"""
Transform from physical parameters with upper limit to transformed (unconstrained) ones
Args:
x (float): vector of parameters
upper (float): vector with upper limits
Returns:
Float: transformed vector of parameters
"""
return np.log(upper-x)
def rBoundedForward(x, upper):
"""
Transform from transformed (unconstrained) parameters to physical ones with upper limit
Args:
x (float): vector of transformed parameters
upper (float): vector with upper limits
Returns:
Float: transformed variables and log Jacobian
"""
return upper - np.exp(x), x
def simplexBackward(x):
"""
Transform from a simplex of dimension K to transformed (unconstrained) parameters
Args:
x (float): vector of parameters of size K
Returns:
Float: transformed vector of parameters
"""
K = x.shape[0]
k = np.arange(K-1) + 1
zk = np.zeros(K-1)
for i in range(K-1):
zk[i] = x[i] / (1.0 - np.sum(x[0:i]))
return logit(zk) - np.log(1.0 / (K-k))
def simplexForward(x):
"""
Transform from transformed (unconstrained) parameters to physical ones within a simplex. For a simplex in K dimensions,
we only provide the K-1 dimensional array x
Args:
x (float): vector of transformed parameters in K-1 dimensions
Returns:
Float: transformed variables of size K and log Jacobian
"""
K = x.shape[0] + 1
k = np.arange(K - 1)+1
zk = invLogit(x + np.log(1.0 / (K-k)))
xk = np.zeros(K)
for i in range(K-1):
xk[i] = (1.0 - np.sum(xk[0:i])) * zk[i]
xk[K-1] = 1.0 - np.sum(xk[0:K-1])
return xk, np.log(np.sum(xk[0:K-1] * (1.0-zk[0:K-1])))