-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMarginalDist.jl
173 lines (130 loc) · 4.6 KB
/
MarginalDist.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
struct MarginalDist{N,D<:Distribution,VS<:AbstractValueShape}
dims::NTuple{N,Int}
dist::D
origvalshape::VS
end
# TODO: Remove
# function _get_edges(data::Tuple, nbins::Tuple{Vararg{<:Integer}}, closed::Symbol)
# return StatsBase.histrange(data, StatsBase._nbins_tuple(data, nbins), closed)
# end
#
# function _get_edges(data::Any, nbins::Integer, closed::Symbol)
# return StatsBase.histrange((data, ), StatsBase._nbins_tuple((data, ), (nbins,)), closed)[1]
# end
#
# function _get_edges(data::Any, nbins::Union{AbstractRange, Tuple{AbstractRange}}, closed::Symbol)
# return nbins
# end
function bat_marginalize(
maybe_shaped_samples::DensitySampleVector,
key::Union{Integer, Symbol, Expr};
bins::Union{Integer, AbstractRange, AbstractBinning} = FDBinning(),
closed::Symbol = :left,
filter::Bool = false,
normalize = true
)
samples = BAT.unshaped.(maybe_shaped_samples)
if filter
samples = BAT.drop_low_weight_samples(samples)
end
idx = asindex(maybe_shaped_samples, key)
s = flatview(samples.v)[idx, :]
edges = _bin_edges(samples, idx, bins; closed=closed)
hist = fit(Histogram,
s,
FrequencyWeights(samples.weight),
edges,
closed = closed)
normalize ? hist = StatsBase.normalize(hist) : nothing
uvbd = EmpiricalDistributions.UvBinnedDist(hist)
marg = MarginalDist((idx,), uvbd, varshape(maybe_shaped_samples))
return (result = marg, )
end
function bat_marginalize(
maybe_shaped_samples::DensitySampleVector,
key::Union{NTuple{n,Integer}, NTuple{n,Union{Symbol, Expr}}} where n;
bins = FDBinning(),
closed::Symbol = :left,
filter::Bool = false,
normalize = true
)
samples = unshaped.(maybe_shaped_samples)
if filter
samples = BAT.drop_low_weight_samples(samples)
end
idxs = asindex.(Ref(maybe_shaped_samples), key)
s = Tuple(BAT.flatview(samples.v)[i, :] for i in idxs)
bins_tuple = isa(bins, Tuple) ? bins : (bins, bins)
edges = Tuple(_bin_edges(samples, idxs[i], bins_tuple[i], closed=closed) for i in 1:length(bins_tuple))
hist = fit(Histogram,
s,
FrequencyWeights(samples.weight),
edges,
closed = closed)
normalize ? hist = StatsBase.normalize(hist) : nothing
mvbd = EmpiricalDistributions.MvBinnedDist(hist)
marg = MarginalDist(idxs, mvbd, varshape(maybe_shaped_samples))
return (result = marg, )
end
#for prior
function bat_marginalize(
prior::NamedTupleDist,
key::Union{Integer, Symbol};
bins = FDBinning(),
edges = nothing,
closed::Symbol = :left,
nsamples::Integer = 10^6,
normalize = true
)
idx = asindex(prior, key)
r = rand(prior, nsamples)
edges = _bin_edges(r[idx, :], bins, closed=closed)
hist = fit(Histogram, r[idx, :], edges, closed = closed)
normalize ? hist = StatsBase.normalize(hist) : nothing
uvbd = EmpiricalDistributions.UvBinnedDist(hist)
marg = MarginalDist((idx,), uvbd, varshape(prior))
return (result = marg, )
end
function bat_marginalize(
prior::NamedTupleDist,
key::Union{NTuple{2, Symbol}, NTuple{2, Integer}};
bins = FDBinning(),
closed::Symbol = :left,
nsamples::Integer = 10^6,
normalize=true
)
idxs = asindex.(Ref(prior), key)
r = rand(prior, nsamples)
s = Tuple(r[i, :] for i in idxs)
bins_tuple = isa(bins, Tuple) ? bins : (bins, bins)
edges = Tuple(_bin_edges(s[i], bins_tuple[i], closed=closed) for i in 1:length(bins_tuple))
hist = fit(Histogram,
s,
edges,
closed = closed)
normalize ? hist = StatsBase.normalize(hist) : nothing
mvbd = EmpiricalDistributions.MvBinnedDist(hist)
marg = MarginalDist(idxs, mvbd, varshape(prior))
return (result = marg, )
end
function bat_marginalize(
original::MarginalDist,
parsel::NTuple{n, Int} where n;
normalize=true
)
original_hist = original.dist.h
dims = collect(1:ndims(original_hist.weights))
parsel = Tuple(findfirst(x-> x == p, original.dims) for p in parsel)
weights = sum(original_hist.weights, dims=setdiff(dims, parsel))
weights = dropdims(weights, dims=Tuple(setdiff(dims, parsel)))
edges = Tuple([original_hist.edges[p] for p in parsel])
hist = StatsBase.Histogram(edges, weights, original_hist.closed)
normalize ? hist = StatsBase.normalize(hist) : nothing
bd = if length(parsel) == 1
EmpiricalDistributions.UvBinnedDist(hist)
else
EmpiricalDistributions.MvBinnedDist(hist)
end
marg = MarginalDist(parsel, bd, original.origvalshape)
return (result = marg, )
end