@@ -4,39 +4,42 @@ using LinearAlgebra
4
4
par:: NamedTuple{N,T}
5
5
end
6
6
7
+ quoteof (f:: AffineTransform ) = :(AffineTransform ($ (quoteof (f. par))))
8
+
7
9
params (f:: AffineTransform ) = getfield (f, :par )
8
10
9
11
@inline Base. getproperty (d:: AffineTransform , s:: Symbol ) = getfield (getfield (d, :par ), s)
10
12
11
- Base. propertynames (d:: AffineTransform{N} ) where {N} = N
13
+ Base. propertynames (d:: AffineTransform{N} ) where {N} = N
12
14
13
- @inline Base. inv (f:: AffineTransform{(:μ,:σ)} ) = AffineTransform ((μ = - (f. σ \ f. μ), ω = f. σ))
14
- @inline Base. inv (f:: AffineTransform{(:μ,:ω)} ) = AffineTransform ((μ = - f. ω * f. μ, σ = f. ω))
15
+ @inline Base. inv (f:: AffineTransform{(:μ, :σ)} ) =
16
+ AffineTransform ((μ = - (f. σ \ f. μ), ω = f. σ))
17
+ @inline Base. inv (f:: AffineTransform{(:μ, :ω)} ) = AffineTransform ((μ = - f. ω * f. μ, σ = f. ω))
15
18
@inline Base. inv (f:: AffineTransform{(:σ,)} ) = AffineTransform ((ω = f. σ,))
16
19
@inline Base. inv (f:: AffineTransform{(:ω,)} ) = AffineTransform ((σ = f. ω,))
17
20
@inline Base. inv (f:: AffineTransform{(:μ,)} ) = AffineTransform ((μ = - f. μ,))
18
21
19
22
# `size(f) == (m,n)` means `f : ℝⁿ → ℝᵐ`
20
- Base. size (f:: AffineTransform{(:μ,:σ)} ) = size (f. σ)
21
- Base. size (f:: AffineTransform{(:μ,:ω)} ) = size (f. ω)
22
- Base. size (f:: AffineTransform{(:σ,)} ) = size (f. σ)
23
- Base. size (f:: AffineTransform{(:ω,)} ) = size (f. ω)
23
+ Base. size (f:: AffineTransform{(:μ, :σ)} ) = size (f. σ)
24
+ Base. size (f:: AffineTransform{(:μ, :ω)} ) = size (f. ω)
25
+ Base. size (f:: AffineTransform{(:σ,)} ) = size (f. σ)
26
+ Base. size (f:: AffineTransform{(:ω,)} ) = size (f. ω)
24
27
25
28
function Base. size (f:: AffineTransform{(:μ,)} )
26
29
(n,) = size (f. μ)
27
- return (n,n)
30
+ return (n, n)
28
31
end
29
32
30
33
Base. size (f:: AffineTransform , n:: Int ) = @inbounds size (f)[n]
31
34
32
35
(f:: AffineTransform{(:μ,)} )(x) = x + f. μ
33
36
(f:: AffineTransform{(:σ,)} )(x) = f. σ * x
34
37
(f:: AffineTransform{(:ω,)} )(x) = f. ω \ x
35
- (f:: AffineTransform{(:μ,:σ)} )(x) = f. σ * x + f. μ
36
- (f:: AffineTransform{(:μ,:ω)} )(x) = f. ω \ x + f. μ
38
+ (f:: AffineTransform{(:μ, :σ)} )(x) = f. σ * x + f. μ
39
+ (f:: AffineTransform{(:μ, :ω)} )(x) = f. ω \ x + f. μ
37
40
38
41
rowsize (x) = ()
39
- rowsize (x:: AbstractArray ) = (size (x,1 ),)
42
+ rowsize (x:: AbstractArray ) = (size (x, 1 ),)
40
43
41
44
function rowsize (f:: AffineTransform )
42
45
size_f = size (f)
@@ -46,7 +49,7 @@ function rowsize(f::AffineTransform)
46
49
end
47
50
48
51
colsize (x) = ()
49
- colsize (x:: AbstractArray ) = (size (x,2 ),)
52
+ colsize (x:: AbstractArray ) = (size (x, 2 ),)
50
53
51
54
function colsize (f:: AffineTransform )
52
55
size_f = size (f)
65
68
return x
66
69
end
67
70
68
- @inline function apply! (x, f:: AffineTransform{(:ω,), Tuple{F}} , z) where {F<: Factorization }
71
+ @inline function apply! (x, f:: AffineTransform{(:ω,),Tuple{F}} , z) where {F<: Factorization }
69
72
ldiv! (x, f. ω, z)
70
73
return x
71
74
end
75
78
return x
76
79
end
77
80
78
- @inline function apply! (x, f:: AffineTransform{(:μ,:σ)} , z)
81
+ @inline function apply! (x, f:: AffineTransform{(:μ, :σ)} , z)
79
82
apply! (x, AffineTransform ((σ = f. σ,)), z)
80
83
apply! (x, AffineTransform ((μ = f. μ,)), x)
81
84
return x
82
85
end
83
86
84
- @inline function apply! (x, f:: AffineTransform{(:μ,:ω)} , z)
87
+ @inline function apply! (x, f:: AffineTransform{(:μ, :ω)} , z)
85
88
apply! (x, AffineTransform ((ω = f. ω,)), z)
86
89
apply! (x, AffineTransform ((μ = f. μ,)), x)
87
90
return x
88
91
end
89
92
90
- function logjac (x:: AbstractMatrix )
91
- (m,n) = size (x)
93
+ function logjac (x:: AbstractMatrix )
94
+ (m, n) = size (x)
92
95
m == n && return first (logabsdet (x))
93
96
94
97
# Equivalent to sum(log, svdvals(x)), but much faster
99
102
logjac (x:: Number ) = log (abs (x))
100
103
101
104
# TODO : `log` doesn't work for the multivariate case, we need the log absolute determinant
102
- logjac (f:: AffineTransform{(:μ,:σ)} ) = logjac (f. σ)
103
- logjac (f:: AffineTransform{(:μ,:ω)} ) = - logjac (f. ω)
105
+ logjac (f:: AffineTransform{(:μ, :σ)} ) = logjac (f. σ)
106
+ logjac (f:: AffineTransform{(:μ, :ω)} ) = - logjac (f. ω)
104
107
logjac (f:: AffineTransform{(:σ,)} ) = logjac (f. σ)
105
108
logjac (f:: AffineTransform{(:ω,)} ) = - logjac (f. ω)
106
109
logjac (f:: AffineTransform{(:μ,)} ) = 0.0
@@ -130,16 +133,16 @@ function params(μ::Affine)
130
133
return merge (nt1, nt2)
131
134
end
132
135
133
- function paramnames (:: Type{A} ) where {N,M, A<: Affine{N,M} }
136
+ function paramnames (:: Type{A} ) where {N,M,A<: Affine{N,M} }
134
137
tuple (union (N, paramnames (M))... )
135
138
end
136
139
137
- Base. propertynames (d:: Affine{N} ) where {N} = N ∪ (:parent ,:f )
140
+ Base. propertynames (d:: Affine{N} ) where {N} = N ∪ (:parent , :f )
138
141
139
- @inline function Base. getproperty (d:: Affine , s:: Symbol )
142
+ @inline function Base. getproperty (d:: Affine , s:: Symbol )
140
143
if s === :parent
141
144
return getfield (d, :parent )
142
- elseif s === :f
145
+ elseif s === :f
143
146
return getfield (d, :f )
144
147
else
145
148
return getproperty (getfield (d, :f ), s)
@@ -166,18 +169,18 @@ end
166
169
167
170
function logdensity (d:: Affine{(:μ,)} , x)
168
171
z = x - d. μ
169
- logdensity (d. parent, z)
172
+ logdensity (d. parent, z)
170
173
end
171
174
172
- function logdensity (d:: Affine{(:μ,:σ)} , x)
175
+ function logdensity (d:: Affine{(:μ, :σ)} , x)
173
176
z = d. σ \ (x - d. μ)
174
- logdensity (d. parent, z)
177
+ logdensity (d. parent, z)
175
178
end
176
179
177
- function logdensity (d:: Affine{(:μ,:ω)} , x)
180
+ function logdensity (d:: Affine{(:μ, :ω)} , x)
178
181
z = d. ω * (x - d. μ)
179
182
logdensity (d. parent, z)
180
- end
183
+ end
181
184
182
185
# # logdensity(d::Affine{(:μ,:ω)}, x) = logdensity(d.parent, d.σ \ (x - d.μ))
183
186
# @inline function logdensity(d::Affine{(:μ,:σ), P, Tuple{V,M}}, x) where {P, V<:AbstractVector, M<:AbstractMatrix}
190
193
# end
191
194
# sum(zⱼ -> logdensity(d.parent, zⱼ), z)
192
195
# end
193
-
196
+
194
197
# # logdensity(d::Affine{(:μ,:ω)}, x) = logdensity(d.parent, d.ω * (x - d.μ))
195
198
# @inline function logdensity(d::Affine{(:μ,:ω), P,Tuple{V,M}}, x) where {P,V<:AbstractVector, M<:AbstractMatrix}
196
199
# z = x - d.μ
@@ -202,31 +205,35 @@ basemeasure(d::Affine) = affine(getfield(d, :f), basemeasure(d.parent))
202
205
203
206
# We can't do this until we know we're working with Lebesgue measure, since for
204
207
# example it wouldn't make sense to apply a log-Jacobian to a point measure
205
- basemeasure (d:: Affine{N,L} ) where {N, L<: Lebesgue } = weightedmeasure (- logjac (d), d. parent)
208
+ basemeasure (d:: Affine{N,L} ) where {N,L<: Lebesgue } = weightedmeasure (- logjac (d), d. parent)
206
209
207
- function basemeasure (d:: Affine{N,M} ) where {N,L<: Lebesgue , M<: ProductMeasure{Returns{L}} }
210
+ function basemeasure (d:: Affine{N,M} ) where {N,L<: Lebesgue ,M<: ProductMeasure{Returns{L}} }
208
211
weightedmeasure (- logjac (d), d. parent)
209
212
end
210
213
211
214
logjac (d:: Affine ) = logjac (getfield (d, :f ))
212
215
213
- function Random. rand! (rng:: Random.AbstractRNG , d:: Affine , x:: AbstractVector{T} , z= Vector {T} (undef, size (getfield (d,:f ),2 ))) where {T}
216
+ function Random. rand! (
217
+ rng:: Random.AbstractRNG ,
218
+ d:: Affine ,
219
+ x:: AbstractVector{T} ,
220
+ z = Vector {T} (undef, size (getfield (d, :f ), 2 ))
221
+ ) where {T}
214
222
rand! (rng, parent (d), z)
215
223
f = getfield (d, :f )
216
224
apply! (x, f, z)
217
225
return x
218
226
end
219
227
220
-
221
228
# function Base.rand(rng::Random.AbstractRNG, ::Type{T}, d::Affine) where {T}
222
229
# f = getfield(d, :f)
223
230
# z = rand(rng, T, parent(d))
224
231
# apply!(x, f, z)
225
232
# return z
226
233
# end
227
234
228
- supportdim (nt:: NamedTuple{(:μ,:σ)} ) = colsize (nt. σ)
229
- supportdim (nt:: NamedTuple{(:μ,:ω)} ) = rowsize (nt. ω)
230
- supportdim (nt:: NamedTuple{(:σ,)} ) = colsize (nt. σ)
231
- supportdim (nt:: NamedTuple{(:ω,)} ) = rowsize (nt. ω)
232
- supportdim (nt:: NamedTuple{(:μ,)} ) = size (nt. μ)
235
+ supportdim (nt:: NamedTuple{(:μ, :σ)} ) = colsize (nt. σ)
236
+ supportdim (nt:: NamedTuple{(:μ, :ω)} ) = rowsize (nt. ω)
237
+ supportdim (nt:: NamedTuple{(:σ,)} ) = colsize (nt. σ)
238
+ supportdim (nt:: NamedTuple{(:ω,)} ) = rowsize (nt. ω)
239
+ supportdim (nt:: NamedTuple{(:μ,)} ) = size (nt. μ)
0 commit comments