-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpower.jl
130 lines (103 loc) · 3.44 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
import Base
using FillArrays: Fill
# """
# 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 μ^x.
# """
# PowerMeasure{M,N,D} = ProductMeasure{Fill{M,N,D}}
export PowerMeasure
struct PowerMeasure{M,A} <: AbstractProductMeasure
parent::M
axes::A
end
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
function Base.rand(
rng::AbstractRNG,
::Type{T},
d::PowerMeasure{M},
) where {T,M<:AbstractMeasure}
map(CartesianIndices(d.axes)) do _
rand(rng, T, d.parent)
end
end
function Base.rand(rng::AbstractRNG, ::Type{T}, d::PowerMeasure) where {T}
map(CartesianIndices(d.axes)) do _
rand(rng, d.parent)
end
end
@inline function powermeasure(x::T, sz::Tuple{Vararg{<:Any,N}}) where {T,N}
a = axes(Fill{T,N}(x, sz))
A = typeof(a)
PowerMeasure{T,A}(x, a)
end
marginals(d::PowerMeasure) = Fill(d.parent, d.axes)
function Base.:^(μ::AbstractMeasure, dims::Tuple{Vararg{<:AbstractArray,N}}) where {N}
powermeasure(μ, dims)
end
Base.:^(μ::AbstractMeasure, dims::Tuple) = powermeasure(μ, Base.OneTo.(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
@inline function logdensity_def(d::PowerMeasure{M}, x) where {M}
parent = d.parent
sum(x) do xj
logdensity_def(parent, xj)
end
end
@inline function logdensity_def(
d::PowerMeasure{M,Tuple{Base.OneTo{StaticInt{N}}}},
x,
) where {M,N}
parent = d.parent
sum(1:N) do j
@inbounds logdensity_def(parent, x[j])
end
end
@inline function logdensity_def(
d::PowerMeasure{M,NTuple{N, Base.OneTo{StaticInt{0}}}},
x,
) where {M,N}
static(0.0)
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 getdof(::PowerMeasure{<:Any, NTuple{N,Base.OneTo{StaticInt{0}}}}) where N = static(0)
@propagate_inbounds function checked_var(μ::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_var(μ::PowerMeasure, x::Any)
throw(ArgumentError("Size of variate doesn't match size of power measure"))
end