-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpower.jl
151 lines (118 loc) · 4.2 KB
/
power.jl
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
145
146
147
148
149
150
151
import Base
export PowerMeasure
"""
struct PowerMeasure{M,...} <: AbstractProductMeasure
A power measure is a product of a measure with itself. The number of elements in
the product determines the dimensionality of the resulting support.
Note that power measures are only well-defined for integer powers.
The nth power of a measure μ can be written μ^n.
"""
struct PowerMeasure{M,A} <: AbstractProductMeasure
parent::M
axes::A
end
maybestatic_length(μ::PowerMeasure) = prod(maybestatic_size(μ))
maybestatic_size(μ::PowerMeasure) = map(maybestatic_length, μ.axes)
function Pretty.tile(μ::PowerMeasure)
sz = length.(μ.axes)
arg1 = Pretty.tile(μ.parent)
arg2 = Pretty.tile(length(sz) == 1 ? only(sz) : sz)
return Pretty.pair_layout(arg1, arg2; sep = " ^ ")
end
# ToDo: Make rand return static arrays for statically-sized power measures.
function _cartidxs(axs::Tuple{Vararg{AbstractUnitRange,N}}) where {N}
CartesianIndices(map(_dynamic, axs))
end
function Base.rand(
rng::AbstractRNG,
::Type{T},
d::PowerMeasure{M},
) where {T,M<:AbstractMeasure}
map(_cartidxs(d.axes)) do _
rand(rng, T, d.parent)
end
end
function Base.rand(rng::AbstractRNG, ::Type{T}, d::PowerMeasure) where {T}
map(_cartidxs(d.axes)) do _
rand(rng, d.parent)
end
end
@inline _pm_axes(sz::Tuple{Vararg{IntegerLike,N}}) where {N} = map(one_to, sz)
@inline _pm_axes(axs::Tuple{Vararg{AbstractUnitRange,N}}) where {N} = axs
@inline function powermeasure(x::T, sz::Tuple{Vararg{Any,N}}) where {T,N}
PowerMeasure(x, _pm_axes(sz))
end
marginals(d::PowerMeasure) = fill_with(d.parent, d.axes)
function Base.:^(μ::AbstractMeasure, dims::Tuple{Vararg{AbstractArray,N}}) where {N}
powermeasure(μ, dims)
end
Base.:^(μ::AbstractMeasure, dims::Tuple) = powermeasure(μ, one_to.(dims))
Base.:^(μ::AbstractMeasure, n) = powermeasure(μ, (n,))
# Base.show(io::IO, d::PowerMeasure) = print(io, d.parent, " ^ ", size(d.xs))
# Base.show(io::IO, d::PowerMeasure{M,1}) where {M} = print(io, d.parent, " ^ ", length(d.xs))
# gentype(d::PowerMeasure{M,N}) where {M,N} = @inbounds Array{gentype(first(marginals(d))), N}
params(d::PowerMeasure) = params(first(marginals(d)))
# basemeasure(μ::PowerMeasure) = @inbounds basemeasure(first(μ.data))^size(μ.data)
@inline function basemeasure(d::PowerMeasure)
basemeasure(d.parent)^d.axes
end
for func in [:strict_logdensityof, :logdensity_def]
@eval @inline function $func(d::PowerMeasure{M}, x) where {M}
parent = d.parent
sum(x) do xj
$func(parent, xj)
end
end
@eval @inline function $func(d::PowerMeasure{M,Tuple{Static.SOneTo{N}}}, x) where {M,N}
parent = d.parent
sum(1:N) do j
@inbounds $func(parent, x[j])
end
end
@eval @inline function $func(
d::PowerMeasure{M,NTuple{N,Static.SOneTo{0}}},
x,
) where {M,N}
static(0.0)
end
end
@inline function insupport(μ::PowerMeasure, x)
p = μ.parent
all(x) do xj
# https://github.com/SciML/Static.jl/issues/36
dynamic(insupport(p, xj))
end
end
@inline function insupport(μ::PowerMeasure, x::AbstractArray)
p = μ.parent
all(x) do xj
# https://github.com/SciML/Static.jl/issues/36
dynamic(insupport(p, xj))
end
end
@inline getdof(μ::PowerMeasure) = getdof(μ.parent) * prod(map(length, μ.axes))
@inline function getdof(::PowerMeasure{<:Any,NTuple{N,Static.SOneTo{0}}}) where {N}
static(0)
end
@propagate_inbounds function checked_arg(μ::PowerMeasure, x::AbstractArray{<:Any})
@boundscheck begin
sz_μ = map(length, μ.axes)
sz_x = size(x)
if sz_μ != sz_x
throw(ArgumentError("Size of variate doesn't match size of power measure"))
end
end
return x
end
function checked_arg(μ::PowerMeasure, x::Any)
throw(ArgumentError("Size of variate doesn't match size of power measure"))
end
massof(m::PowerMeasure) = massof(m.parent)^prod(m.axes)
logdensity_def(::PowerMeasure{P}, x) where {P<:PrimitiveMeasure} = static(0.0)
# To avoid ambiguities
function logdensity_def(
::PowerMeasure{P,Tuple{Vararg{Static.SOneTo{0},N}}},
x,
) where {P<:PrimitiveMeasure,N}
static(0.0)
end