Skip to content

Commit 1868f0b

Browse files
committed
update
1 parent 05b345d commit 1868f0b

28 files changed

+838
-555
lines changed

Project.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ authors = ["Chad Scherrer <[email protected]> and contributors"]
44
version = "0.1.0"
55

66
[deps]
7-
BinaryTraits = "190e46ec-f771-4705-b939-984896f7be0e"
8-
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
97
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
10-
DynamicIterators = "6c76993d-992e-5bf1-9e63-34920a5a5a38"
11-
KeywordCalls = "4d827475-d3e4-43d6-abe3-9688362ede9f"
8+
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
9+
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
1210
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1311
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
1412
MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"
1513
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
1614
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1715
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
18-
TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff"
16+
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
1917

2018
[compat]
21-
Compat = "3.28"
2219
ConcreteStructs = "0.2"
23-
KeywordCalls = "0.1.5"
2420
MLStyle = "0.4"
2521
MappedArrays = "0.4"
2622
NamedTupleTools = "0.13"
27-
TransformVariables = "0.4"
2823
julia = "1.3"
2924

3025
[extras]

src/MeasureBase.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ using Random
55

66
using ConcreteStructs
77
using MLStyle
8-
using KeywordCalls
9-
using SimpleTraits
10-
using Compat
118

129
export
1310
export sampletype
@@ -33,25 +30,33 @@ Methods for computing density relative to other measures will be
3330
"""
3431
function logdensity end
3532

33+
3634
include("exp.jl")
3735
include("domains.jl")
3836
include("utils.jl")
3937
include("absolutecontinuity.jl")
40-
include("primitives.jl")
4138
include("parameterized.jl")
4239
include("macros.jl")
40+
include("resettablerng.jl")
41+
42+
include("primitive.jl")
43+
include("primitives/counting.jl")
44+
include("primitives/lebesgue.jl")
45+
include("primitives/dirac.jl")
46+
include("primitives/trivial.jl")
47+
4348
include("combinators/weighted.jl")
4449
include("combinators/superpose.jl")
4550
include("combinators/product.jl")
4651
include("combinators/for.jl")
4752
include("combinators/power.jl")
53+
include("combinators/spikemixture.jl")
54+
include("kernel.jl")
4855
include("combinators/likelihood.jl")
4956
include("combinators/pointwise.jl")
50-
include("combinators/spikemixture.jl")
51-
include("combinators/chain.jl")
52-
include("combinators/transforms.jl")
57+
5358
include("rand.jl")
59+
5460
include("density.jl")
55-
include("kernel.jl")
5661

5762
end

src/absolutecontinuity.jl

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,56 @@
11

2-
"""
3-
≪(μ,ν)
2+
# """
3+
# ≪(μ,ν)
44

5-
# Absolute continuity
5+
# # Absolute continuity
66

7-
A measure μ is _absolutely continuous_ with respect to ν, written μ ≪ ν, if
8-
ν(A)==0 implies μ(A)==0 for every ν-measurable set A.
7+
# A measure μ is _absolutely continuous_ with respect to ν, written μ ≪ ν, if
8+
# ν(A)==0 implies μ(A)==0 for every ν-measurable set A.
99

10-
Less formally, suppose we have a set A with ν(A)==0. If μ(A)≠0, then there can
11-
be no way to "reweight" ν to get to μ. We can't make something from nothing.
10+
# Less formally, suppose we have a set A with ν(A)==0. If μ(A)≠0, then there can
11+
# be no way to "reweight" ν to get to μ. We can't make something from nothing.
1212

13-
This "reweighting" is really a density function. If μ≪ν, then there is some
14-
function f that makes `μ == ∫(f,ν)` (see the help section for `∫`).
13+
# This "reweighting" is really a density function. If μ≪ν, then there is some
14+
# function f that makes `μ == ∫(f,ν)` (see the help section for `∫`).
1515

16-
We can get this f directly via the Radon-Nikodym derivative, `f == 𝒹(μ,ν)` (see
17-
the help section for `𝒹`).
16+
# We can get this f directly via the Radon-Nikodym derivative, `f == 𝒹(μ,ν)` (see
17+
# the help section for `𝒹`).
1818

19-
Note that `≪` is not a partial order, because it is not antisymmetric. That is
20-
to say, it's possible (in fact, common) to have two different measures `μ` and
21-
`ν` with `μ ≪ ν` and `ν ≪ μ`. A simple example of this is
22-
```
23-
μ = Normal()
24-
ν = Lebesgue(ℝ)
25-
```
19+
# Note that `≪` is not a partial order, because it is not antisymmetric. That is
20+
# to say, it's possible (in fact, common) to have two different measures `μ` and
21+
# `ν` with `μ ≪ ν` and `ν ≪ μ`. A simple example of this is
22+
# ```
23+
# μ = Normal()
24+
# ν = Lebesgue(ℝ)
25+
# ```
2626

27-
When `≪` holds in both directions, the measures μ and ν are _equivalent_,
28-
written `μ ≃ ν`. See the help section for `≃` for more information.
29-
"""
30-
function end
27+
# When `≪` holds in both directions, the measures μ and ν are _equivalent_,
28+
# written `μ ≃ ν`. See the help section for `≃` for more information.
29+
# """
30+
# function ≪ end
3131

3232

33-
export
33+
# export ≃
3434

35-
"""
36-
≃(μ,ν)
35+
# """
36+
# ≃(μ,ν)
3737

38-
# Equivalence of Measure
38+
# # Equivalence of Measure
3939

40-
Measures μ and ν on the same space X are equivalent, written `μ ≃ ν`, if `μ ≪ ν`
41-
and `ν ≪ μ`. Note that this is often written `~` in the literature, but this is
42-
overloaded in probabilistic programming, so we use this alternate notation.
40+
# Measures μ and ν on the same space X are equivalent, written `μ ≃ ν`, if `μ ≪ ν`
41+
# and `ν ≪ μ`. Note that this is often written `~` in the literature, but this is
42+
# overloaded in probabilistic programming, so we use this alternate notation.
4343

44-
Also note that equivalence is very different from equality. For two equivalent
45-
measures, the sets of non-zero measure will be identical, but what that measure
46-
is in each case can be very different.
47-
"""
48-
function (μ,ν)
49-
returnν && νμ)
50-
end
44+
# Also note that equivalence is very different from equality. For two equivalent
45+
# measures, the sets of non-zero measure will be identical, but what that measure
46+
# is in each case can be very different.
47+
# """
48+
# function ≃(μ,ν)
49+
# return (μ≪ν && ν≪μ)
50+
# end
5151

52-
export representative
53-
54-
"""
55-
representative(μ::AbstractMeasure) -> AbstractMeasure
56-
57-
We need to be able to compute `μ ≪ ν` for each `μ` and `ν`. To do this directly
58-
would require a huge number of methods (quadratic in the number of defined
59-
measures).
60-
61-
This function is a way around that. When defining a new measure `μ`, you should
62-
also find some equivalent measure `ρ` that's "as primitive as possible".
63-
64-
If possible, `ρ` should be a `PrimitiveMeasure`, or a `Product` of these. If
65-
not, it should be a transform (`Pushforward` or `Pullback`) of a
66-
`PrimitiveMeasure` (or `Product` of these).
67-
"""
68-
function representative(μ)
69-
function f(μ)
70-
# Check if we're done
71-
isprimtype(μ) && return μ
72-
ν = basemeasure(μ)
73-
return ν
74-
end
75-
76-
fix(f, μ)
77-
end
78-
79-
function (μ, ν)
80-
μ == ν && return true
81-
representative(μ) representative(ν) && return true
82-
return false
83-
end
52+
# function ≪(μ, ν)
53+
# μ == ν && return true
54+
# representative(μ) ≪ representative(ν) && return true
55+
# return false
56+
# end

src/combinators/chain.jl

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/combinators/for.jl

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,18 @@ julia> For(eachrow(rand(4,2))) do x Normal(x[1], x[2]) end |> marginals |> colle
7575
```
7676
7777
"""
78-
function For(f, base...) end
78+
For(f, dims...) = ProductMeasure(i -> f(i...), zip(dims...))
7979

80+
For(f, inds::AbstractArray) = ProductMeasure(f, inds)
8081

82+
For(f, n::Int) = ProductMeasure(f, 1:n)
83+
For(f, dims::Int...) = ProductMeasure(i -> f(Tuple(i)...), CartesianIndices(dims))
8184

82-
# ForArray
8385

84-
ForArray{D,N,T,F} = ProductMeasure{ReadonlyMappedArray{D, N, T, F}}
85-
86-
function Base.show(io::IO, d::ForArray{D,N,T,F}) where {D,N,T,F}
87-
print(io, "For(")
88-
print(io, d.data.f, ", ")
89-
print(io, d.data.data, ")")
90-
end
91-
92-
function Base.show(io::IO, d::ForArray{D,N,T,F}) where {D,N,T <: CartesianIndices,F}
93-
print(io, "For(")
94-
print(io, d.data.f, ", ")
95-
join(io, size(d.data), ", ")
96-
print(io, ")")
97-
end
98-
99-
For(f, dims::AbstractArray...) = ProductMeasure(mappedarray(f, dims...))
100-
101-
For(f, dims::Int...) = ProductMeasure(mappedarray(i -> f(Tuple(i)...), CartesianIndices(dims)))
102-
103-
function Base.eltype(::ForArray{D,N,T,F}) where {D,N,T,F}
104-
return eltype(D)
86+
function Base.eltype(d::ProductMeasure{F,I}) where {F,I<:AbstractArray}
87+
return eltype(d.f(first(d.pars)))
10588
end
10689

107-
basemeasure::ForArray) = @inbounds basemeasure.data[1])^size.data)
108-
10990
# """
11091
# indexstyle(a::AbstractArray, b::AbstractArray)
11192

@@ -140,20 +121,3 @@ basemeasure(μ::ForArray) = @inbounds basemeasure(μ.data[1])^size(μ.data)
140121
# function basemeasure(μ::ForArray{D,N,T,F}) where {F,T<:AbstractArray,D,X}
141122

142123
# ForGenerator
143-
144-
ForGenerator{G} = ProductMeasure{G} where {G <: Base.Generator}
145-
146-
For(f, dims::Base.Generator) = ProductMeasure(Base.Generator(f dims.f, dims.iter))
147-
148-
sampletype(::ForGenerator) = Base.Generator
149-
150-
function Base.rand(rng::AbstractRNG, T::Type, d::ForGenerator)
151-
r(x) = rand(rng, T, x)
152-
Base.Generator(r d.data.f, d.data.iter)
153-
end
154-
155-
function logdensity(d::ForGenerator, x)
156-
sum((logdensity(dj, xj) for (dj, xj) in zip(d.data, x)))
157-
end
158-
159-
testvalue::ProductMeasure) = mappedarray(testvalue, μ.data)

0 commit comments

Comments
 (0)