26
26
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
=#
28
28
29
-
30
- # ################### TransformDistribution ####################
29
+ # ############
30
+ # a ≦ x ≦ b #
31
+ # ############
31
32
32
33
typealias TransformDistribution{T<: ContinuousUnivariateDistribution }
33
- Union{T, Truncated{T}}
34
+ Union{T, Truncated{T}}
34
35
35
- function link (d:: TransformDistribution , x:: Real )
36
+ link (d:: TransformDistribution , x:: Real ) = begin
36
37
a, b = minimum (d), maximum (d)
37
38
lowerbounded, upperbounded = isfinite (a), isfinite (b)
38
39
if lowerbounded && upperbounded
@@ -46,7 +47,7 @@ function link(d::TransformDistribution, x::Real)
46
47
end
47
48
end
48
49
49
- function invlink (d:: TransformDistribution , x:: Real )
50
+ invlink (d:: TransformDistribution , x:: Real ) = begin
50
51
a, b = minimum (d), maximum (d)
51
52
lowerbounded, upperbounded = isfinite (a), isfinite (b)
52
53
if lowerbounded && upperbounded
@@ -76,12 +77,13 @@ Distributions.logpdf(d::TransformDistribution, x::Real, transform::Bool) = begin
76
77
lp
77
78
end
78
79
79
-
80
- # ################### RealDistribution ####################
80
+ # ##############
81
+ # -∞ < x < -∞ #
82
+ # ##############
81
83
82
84
typealias RealDistribution
83
- Union{Cauchy, Gumbel, Laplace, Logistic, NoncentralT, Normal,
84
- NormalCanon, TDist}
85
+ Union{Cauchy, Gumbel, Laplace, Logistic,
86
+ NoncentralT, Normal, NormalCanon, TDist}
85
87
86
88
link (d:: RealDistribution , x:: Real ) = x
87
89
@@ -90,7 +92,9 @@ invlink(d::RealDistribution, x::Real) = x
90
92
Distributions. logpdf (d:: RealDistribution , x:: Real , transform:: Bool ) = logpdf (d, x)
91
93
92
94
93
- # ################### PositiveDistribution ####################
95
+ # ########
96
+ # 0 < x #
97
+ # ########
94
98
95
99
typealias PositiveDistribution
96
100
Union{BetaPrime, Chi, Chisq, Erlang, Exponential, FDist, Frechet,
@@ -107,7 +111,9 @@ Distributions.logpdf(d::PositiveDistribution, x::Real, transform::Bool) = begin
107
111
end
108
112
109
113
110
- # ################### UnitDistribution ####################
114
+ # ############
115
+ # 0 < x < 1 #
116
+ # ############
111
117
112
118
typealias UnitDistribution
113
119
Union{Beta, KSOneSided, NoncentralBeta}
@@ -118,46 +124,45 @@ invlink(d::UnitDistribution, x::Real) = invlogit(x)
118
124
119
125
Distributions. logpdf (d:: UnitDistribution , x:: Real , transform:: Bool ) = begin
120
126
lp = logpdf (d, x)
121
- transform ? lp + log (x * (1.0 - x)) : lp
127
+ transform ? lp + log (x * (one (x) - x)) : lp
122
128
end
123
129
124
- # ################## SimplexDistribution ###################
130
+ # ##########
131
+ # ∑xᵢ = 1 #
132
+ # ##########
125
133
126
134
typealias SimplexDistribution Union{Dirichlet}
127
135
128
- function link (d:: SimplexDistribution , x:: Vector )
136
+ link {T} (d:: SimplexDistribution , x:: Vector{T} ) = begin
129
137
K = length (x)
130
- T = typeof (x[1 ])
131
138
z = Vector {T} (K- 1 )
132
139
for k in 1 : K- 1
133
- z[k] = x[k] / (1 - sum (x[1 : k- 1 ]))
140
+ z[k] = x[k] / (one (T) - sum (x[1 : k- 1 ]))
134
141
end
135
- y = [logit (z[k]) - log (1 / (K- k)) for k in 1 : K- 1 ]
136
- push! (y, T ( 0 ))
142
+ y = [logit (z[k]) - log (one (T) / (K- k)) for k in 1 : K- 1 ]
143
+ push! (y, zero (T ))
137
144
end
138
145
139
- function invlink (d:: SimplexDistribution , y:: Vector )
146
+ invlink {T} (d:: SimplexDistribution , y:: Vector{T} ) = begin
140
147
K = length (y)
141
- T = typeof (y[1 ])
142
- z = [invlogit (y[k] + log (1 / (K - k))) for k in 1 : K- 1 ]
148
+ z = [invlogit (y[k] + log (one (T) / (K - k))) for k in 1 : K- 1 ]
143
149
x = Vector {T} (K)
144
150
for k in 1 : K- 1
145
- x[k] = (1 - sum (x[1 : k- 1 ])) * z[k]
151
+ x[k] = (one (T) - sum (x[1 : k- 1 ])) * z[k]
146
152
end
147
- x[K] = 1 - sum (x[1 : K- 1 ])
153
+ x[K] = one (T) - sum (x[1 : K- 1 ])
148
154
x
149
155
end
150
156
151
- Distributions. logpdf (d:: SimplexDistribution , x:: Vector , transform:: Bool ) = begin
157
+ Distributions. logpdf {T} (d:: SimplexDistribution , x:: Vector{T} , transform:: Bool ) = begin
152
158
lp = logpdf (d, x)
153
159
if transform
154
160
K = length (x)
155
- T = typeof (x[1 ])
156
161
z = Vector {T} (K- 1 )
157
162
for k in 1 : K- 1
158
- z[k] = x[k] / (1 - sum (x[1 : k- 1 ]))
163
+ z[k] = x[k] / (one (T) - sum (x[1 : k- 1 ]))
159
164
end
160
- lp += sum ([log (z[k]) + log (1 - z[k]) + log (1 - sum (x[1 : k- 1 ])) for k in 1 : K- 1 ])
165
+ lp += sum ([log (z[k]) + log (one (T) - z[k]) + log (one (T) - sum (x[1 : k- 1 ])) for k in 1 : K- 1 ])
161
166
end
162
167
lp
163
168
end
@@ -166,50 +171,54 @@ Distributions.logpdf(d::Categorical, x::Int) = begin
166
171
d. p[x] > 0.0 && insupport (d, x) ? log (d. p[x]) : eltype (d. p)(- Inf )
167
172
end
168
173
169
- # ############## PDMatDistribution ##############
174
+ # ####################
175
+ # Positive definite #
176
+ # ####################
170
177
171
178
typealias PDMatDistribution Union{InverseWishart, Wishart}
172
179
173
- function link {T} (d:: PDMatDistribution , x:: Array{T,2} )
180
+ link {T} (d:: PDMatDistribution , x:: Array{T,2} ) = begin
174
181
z = chol (x)'
175
182
dim = size (z)
176
183
for m in 1 : dim[1 ]
177
184
z[m, m] = log (z[m, m])
178
185
end
179
186
for m in 1 : dim[1 ], n in m+ 1 : dim[2 ]
180
- z[m, n] = 0
187
+ z[m, n] = zero (T)
181
188
end
182
189
Array {T,2} (z)
183
190
end
184
191
185
- function invlink {T} (d:: PDMatDistribution , z:: Array{T,2} )
192
+ invlink {T} (d:: PDMatDistribution , z:: Array{T,2} ) = begin
186
193
dim = size (z)
187
194
for m in 1 : dim[1 ]
188
195
z[m, m] = exp (z[m, m])
189
196
end
190
197
for m in 1 : dim[1 ], n in m+ 1 : dim[2 ]
191
- z[m, n] = 0
198
+ z[m, n] = zero (T)
192
199
end
193
200
Array {T,2} (z * z' )
194
201
end
195
202
196
- Distributions. logpdf (d:: PDMatDistribution , x:: Array , transform:: Bool ) = begin
203
+ Distributions. logpdf {T} (d:: PDMatDistribution , x:: Array{T,2} , transform:: Bool ) = begin
197
204
lp = logpdf (d, x)
198
205
if transform && isfinite (lp)
199
206
U = chol (x)
200
207
n = dim (d)
201
208
for i in 1 : n
202
- lp += (n - i + 2 ) * log (U[i, i])
209
+ lp += (n - i + one (T) + one (T) ) * log (U[i, i])
203
210
end
204
- lp += n * log (2 )
211
+ lp += n * log (one (T) + one (T) )
205
212
end
206
213
lp
207
214
end
208
215
209
- # ################# Callback functions ##################
216
+ # ############
217
+ # Callbacks #
218
+ # ############
210
219
211
- link (d:: Distribution , x) = x
220
+ link (d:: Distribution , x:: Any ) = x
212
221
213
- invlink (d:: Distribution , x) = x
222
+ invlink (d:: Distribution , x:: Any ) = x
214
223
215
- Distributions. logpdf (d:: Distribution , x, transform:: Bool ) = logpdf (d, x)
224
+ Distributions. logpdf (d:: Distribution , x:: Any , transform:: Bool ) = logpdf (d, x)
0 commit comments