Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #49

Merged
merged 268 commits into from
May 9, 2022
Merged

Dev #49

Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
268 commits
Select commit Hold shift + click to select a range
c1679fd
drop some unneeded methods
cscherrer Nov 22, 2021
836fb19
update for DensityInterface
cscherrer Nov 23, 2021
512502b
Merge branch 'dev' into density-interface
cscherrer Nov 23, 2021
26aa0b9
updating densities to DensityInterface approach
cscherrer Nov 23, 2021
3a5cc8b
update domains
cscherrer Nov 23, 2021
05f5534
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Nov 23, 2021
cbf40d1
moving things around
cscherrer Nov 23, 2021
5491ba7
updates to IntegerBounds
cscherrer Nov 24, 2021
7207b35
more domain mucking
cscherrer Nov 24, 2021
af9e113
fix some exports
cscherrer Nov 24, 2021
e3f41cc
update deps
cscherrer Nov 24, 2021
7acbed4
update `using`
cscherrer Nov 24, 2021
7b1578d
Merge branch 'density-interface' into dev
cscherrer Nov 24, 2021
21b728e
working on tests
cscherrer Nov 24, 2021
b1b992d
Working toward tests passing
cscherrer Nov 25, 2021
16e66fa
some refactoring
cscherrer Nov 28, 2021
0c5527f
working on tests
cscherrer Nov 29, 2021
b611f62
cleaning up
cscherrer Nov 29, 2021
38f230b
get tests to pass
cscherrer Nov 29, 2021
a60a4fa
tests passing
cscherrer Nov 29, 2021
54adf0d
update logdensity_def for pointwise product
cscherrer Nov 29, 2021
2499883
Merge branch 'dev' into density-interface
cscherrer Nov 29, 2021
2b08bd8
PrettyPrinting + tests
cscherrer Nov 29, 2021
88777db
speed up `rootmeasure`
cscherrer Nov 30, 2021
3a66b60
oops didn't mean to include that
cscherrer Nov 30, 2021
71af957
tile(::FactoredBase)
cscherrer Nov 30, 2021
f3b3b96
Update Half and FactoredBase
cscherrer Nov 30, 2021
edd2f0b
drop exp.jl
cscherrer Nov 30, 2021
e5abb39
simplify basemeasure
cscherrer Nov 30, 2021
869158d
drop old `include`
cscherrer Nov 30, 2021
ee7cd06
drop redundant method
cscherrer Nov 30, 2021
913cb36
update compat Returns
cscherrer Nov 30, 2021
4291748
add AbstractDensity
cscherrer Nov 30, 2021
046dc2f
Move Affine to MeasureTheory
cscherrer Nov 30, 2021
80abcb6
drop some old `For` code
cscherrer Nov 30, 2021
1d09643
update counting measure
cscherrer Nov 30, 2021
b3c4d92
add testvalue(::Type{T})
cscherrer Dec 1, 2021
2920ab4
bugfix
cscherrer Dec 1, 2021
877e9a3
some dispatch adjustments
cscherrer Dec 1, 2021
3030cc0
simplify show
cscherrer Dec 1, 2021
1bade39
formatting
cscherrer Dec 1, 2021
c69b96e
transformed measures
cscherrer Dec 2, 2021
c8389f3
ZeroSets
cscherrer Dec 2, 2021
6c2aa8b
using LinearAlgebra, Statistics
cscherrer Dec 2, 2021
9364393
working on MeasureTheory tests
cscherrer Dec 2, 2021
398b001
updates
cscherrer Dec 3, 2021
b3ad3e9
updates
cscherrer Dec 3, 2021
a662ed5
typo
cscherrer Dec 5, 2021
0680126
update default to mimic Base
cscherrer Dec 5, 2021
af78f82
fix tile(::Lebesgue)
cscherrer Dec 5, 2021
264fa57
Add test_interface function
cscherrer Dec 5, 2021
cb5f5a7
small doc update
cscherrer Dec 6, 2021
f67c7c3
DensityKind(::Likelihood)
cscherrer Dec 6, 2021
cbaf917
fixing show(::Likelihood)
cscherrer Dec 6, 2021
49062c6
adding some docs
cscherrer Dec 6, 2021
31ad304
update `rand` method
cscherrer Dec 6, 2021
488e6a2
drop old integration code
cscherrer Dec 6, 2021
96c9f4f
add `rebase`
cscherrer Dec 6, 2021
4ffd5cf
export rebase
cscherrer Dec 6, 2021
3a4d90e
law for ⊗
cscherrer Dec 6, 2021
a159939
typo
cscherrer Dec 6, 2021
5997f52
Make Likelihood more flexible
cscherrer Dec 6, 2021
9596fd0
update kernel methods
cscherrer Dec 6, 2021
8c73fc9
add Likelihood method (avoid stack overflow)
cscherrer Dec 6, 2021
18238c0
refactoring
cscherrer Dec 9, 2021
77a574d
compat
cscherrer Dec 9, 2021
ff31603
Maybe Comat just works?
cscherrer Dec 9, 2021
3fce024
refactoring
cscherrer Dec 9, 2021
156c990
some new stuff
cscherrer Dec 9, 2021
931ecb0
working on tests
cscherrer Dec 9, 2021
6389858
update powermeasure combinator
cscherrer Dec 10, 2021
a1ee588
bugfix
cscherrer Dec 10, 2021
59a0e5f
comment out debugging lines
cscherrer Dec 10, 2021
0f51fae
more refactoring
cscherrer Dec 10, 2021
2735705
test @inferred basemeasure_depth(μ)
cscherrer Dec 11, 2021
94bda1b
drop `constructor` (just use ConstructionBase.constructorof`)
cscherrer Dec 11, 2021
340cc85
debugging
cscherrer Dec 11, 2021
1f5ffa9
update help
cscherrer Dec 11, 2021
ebcbba0
update interface
cscherrer Dec 11, 2021
9c2fe0c
make tests harder
cscherrer Dec 11, 2021
a3fda81
fixes
cscherrer Dec 12, 2021
8f996f2
Merge branch 'dev2' of https://github.com/cscherrer/MeasureBase.jl in…
cscherrer Dec 12, 2021
8a1411c
Dirac bugfix
cscherrer Dec 12, 2021
49165bc
formatting
cscherrer Dec 12, 2021
d0f4766
improve type inference
cscherrer Dec 12, 2021
49790b2
working on type inference
cscherrer Dec 12, 2021
5fc1eea
update interface
cscherrer Dec 12, 2021
971e5ab
udpates
cscherrer Dec 13, 2021
de10c90
get test passing
cscherrer Dec 13, 2021
9fc52d8
@test !isabstracttype(typejoin(...))
cscherrer Dec 13, 2021
071fdf7
work on show methods
cscherrer Dec 13, 2021
b7efac2
update CI
cscherrer Dec 13, 2021
61cf5b5
remove old code
cscherrer Dec 13, 2021
15cc2a5
update productmeasure
cscherrer Dec 13, 2021
2979095
prettyprinting stuff
cscherrer Dec 13, 2021
42e11a8
Drop te @constprop :aggressive stuff (maybe don't need it?)
cscherrer Dec 13, 2021
82d174d
nerline
cscherrer Dec 13, 2021
85277ec
dropping some old code
cscherrer Dec 14, 2021
08088d8
update tbasemeasure_type(::PowerMeasure)
cscherrer Dec 14, 2021
37b9567
moar tests
cscherrer Dec 14, 2021
61f6440
update SpikeMixture
cscherrer Dec 14, 2021
f09a179
update superpose type parameter name
cscherrer Dec 20, 2021
9b23149
drop old tests
cscherrer Dec 20, 2021
b12457f
Merge branch 'dev2' of github.com:cscherrer/MeasureBase.jl into dev2
cscherrer Dec 20, 2021
7916f6e
func_string
cscherrer Dec 20, 2021
56b9282
more updates
cscherrer Dec 21, 2021
bf46152
getting closer
cscherrer Dec 22, 2021
73eb70a
almost there!
cscherrer Dec 22, 2021
705b39a
generated function for type stability
cscherrer Dec 22, 2021
1f20b39
tests passing!
cscherrer Dec 22, 2021
f96b27f
newline
cscherrer Dec 22, 2021
ba39a2e
more fixes
cscherrer Dec 23, 2021
fca3f9d
exports and bugfix
cscherrer Dec 24, 2021
8ee4e48
insupport(μ::Counting{T}, x) where {T<:Type}
cscherrer Dec 24, 2021
a300103
working on MeasureTheory tests
cscherrer Dec 24, 2021
ff63c3f
MeasureTheory tests passing
cscherrer Dec 24, 2021
4c5cfa0
drop some old code
cscherrer Dec 27, 2021
a540874
inlining
cscherrer Dec 27, 2021
c200d31
improve inference
cscherrer Dec 28, 2021
effa931
update `tile(::For)`
cscherrer Dec 28, 2021
423d367
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Dec 28, 2021
8bead14
tighten down infrerence
cscherrer Dec 28, 2021
08ca84b
update basemeasure(::For) for generators
cscherrer Dec 28, 2021
953041e
loosen type bound on instance_type
cscherrer Dec 29, 2021
1d2b66c
drop debugging code
cscherrer Dec 30, 2021
b5891c5
small update for Likelihood, and a test
cscherrer Dec 30, 2021
21e2c19
fixing up likelihoods
cscherrer Dec 30, 2021
622fa32
improve `basemeasure_depth` dispatch
cscherrer Dec 30, 2021
630067b
still some trouble with inferred basemeasure_depth
cscherrer Dec 30, 2021
b0a0f66
clean up `For` dispatch
cscherrer Dec 30, 2021
8f28472
simplify _logdensityof
cscherrer Dec 30, 2021
80be728
optimize for Returns{True} case
cscherrer Dec 31, 2021
4de454c
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Dec 31, 2021
1b65a57
rework basemeasure_depth
cscherrer Dec 31, 2021
18e5605
aggressive tests passing!!
cscherrer Dec 31, 2021
9b7e8aa
drop type-level stuff
cscherrer Dec 31, 2021
f907b8f
drop help
cscherrer Dec 31, 2021
d047fc1
license
cscherrer Jan 3, 2022
7fb384a
affero
cscherrer Jan 5, 2022
68dd214
copyright notice
cscherrer Jan 5, 2022
143d7a7
merge
cscherrer Jan 22, 2022
7e3dab1
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Jan 22, 2022
040c815
Drop Create Commons
cscherrer Jan 22, 2022
d24550f
Merge branch 'master' into dev
cscherrer Jan 22, 2022
ae80dc1
cleanup after merge
cscherrer Jan 22, 2022
d6c12f5
update support computations
cscherrer Jan 24, 2022
2f5c31d
insupport(d::SuperpositionMeasure, x)
cscherrer Jan 24, 2022
35271fc
dorp ParamWeighted
cscherrer Jan 24, 2022
de9ccdd
insupport(d::FactoredBase, x)
cscherrer Jan 24, 2022
1e8615d
export unsafe_logdensityof
cscherrer Jan 24, 2022
305ed0e
call promote_type instead of promote_rule
cscherrer Jan 24, 2022
0d35552
logdensity_def for named tuple product measures
cscherrer Jan 24, 2022
cf45278
type annotation for now
cscherrer Jan 24, 2022
e6705af
debugging
cscherrer Jan 25, 2022
093282c
drop shows
cscherrer Jan 26, 2022
7843aea
speed up mapped arrays
cscherrer Feb 4, 2022
641d222
throw an error for `Union{}` types
cscherrer Feb 4, 2022
396405b
MT tests passing
cscherrer Feb 7, 2022
b880018
updates
cscherrer Feb 9, 2022
5afed52
get tests passing
cscherrer Mar 16, 2022
5139be2
MIT license for MeasureBase
cscherrer Mar 16, 2022
101a5fe
bump version
cscherrer Mar 16, 2022
191c5ef
cleanup
cscherrer Mar 29, 2022
636b734
spacing
cscherrer Mar 29, 2022
66ea65c
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Mar 30, 2022
6036ec2
Move ConditionalMeasure to MeasureBase
cscherrer Mar 31, 2022
03a7aba
add LogarithmicNumbers
cscherrer Apr 1, 2022
d45e91e
export basemeasure_sequence
cscherrer Apr 1, 2022
b6cf835
update superpose
cscherrer Apr 1, 2022
c483099
fix logdensity_rel
cscherrer Apr 1, 2022
9da96fa
remove FIXME (it's fixed!!)
cscherrer Apr 1, 2022
c8b8822
logdensityof(d::Density, x)
cscherrer Apr 1, 2022
66cee3c
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Apr 1, 2022
dc82f20
simplify insupport(::Lebesgue, ::Real)
cscherrer Apr 1, 2022
2f9e5f0
clean up
cscherrer Apr 1, 2022
4e1861f
assume insupport yields Bool
cscherrer Apr 1, 2022
e1b03de
change logdensity_rel fall-through to warning and return NaN
cscherrer Apr 1, 2022
08b40e2
update logdensity_rel
cscherrer Apr 1, 2022
3881dda
drop old code
cscherrer Apr 1, 2022
719be40
fix warning
cscherrer Apr 1, 2022
002bd57
export logdensity_rel
cscherrer Apr 1, 2022
6c00bae
logdensity_def(μ::Dirac, ν::Dirac, x)
cscherrer Apr 1, 2022
9168a2c
logdensity_def methods
cscherrer Apr 1, 2022
2524aa1
drop `static`
cscherrer Apr 1, 2022
b6f5292
]add StatsFuns
cscherrer Apr 1, 2022
c3e2f13
Fixing up superposition
cscherrer Apr 1, 2022
027bc12
[compat] entries
cscherrer Apr 1, 2022
91aa7be
trying to speed things up
cscherrer Apr 1, 2022
b069640
bugfixes
cscherrer Apr 1, 2022
b0078d2
logdensity_rel tests
cscherrer Apr 2, 2022
4b1b49e
logdensity_rel tests
cscherrer Apr 2, 2022
1c16e2c
drop qualifier, and add a test
cscherrer Apr 2, 2022
f469ae0
more tests
cscherrer Apr 2, 2022
aec6841
type constraint in "logdensityof(μ::AbstractMeasure, x)" (was piracy,…
cscherrer Apr 3, 2022
9e45cf2
add some docs
cscherrer Apr 3, 2022
f4ca677
docs
cscherrer Apr 3, 2022
a6cffe5
docs
cscherrer Apr 3, 2022
47ee084
typo
cscherrer Apr 3, 2022
46f8a9b
moar speed
cscherrer Apr 4, 2022
380d696
Merge branch 'dev' of github.com:cscherrer/MeasureBase.jl into dev
cscherrer Apr 5, 2022
b3703bd
don't export Test
cscherrer Apr 5, 2022
b3d3afc
some more updates
cscherrer Apr 5, 2022
1893ae3
logdensity_rel for products
cscherrer Apr 6, 2022
ec64718
`kleisli` docs
cscherrer Apr 6, 2022
09f1c40
update instance_type
cscherrer Apr 9, 2022
a355832
instance_type => Core.Typeof
cscherrer Apr 10, 2022
866b466
`powermeasure` bug fix
cscherrer Apr 11, 2022
74777dc
fix logdensity_rel bug
cscherrer Apr 11, 2022
5436d32
get `commonbase` to take x type into account
cscherrer Apr 11, 2022
fdbd12b
test powers
cscherrer Apr 11, 2022
c3ec497
commonbase docstring
cscherrer Apr 12, 2022
725d2dd
deprecate instance_type
cscherrer Apr 12, 2022
09d5196
avoid breakage
cscherrer Apr 13, 2022
f6f776e
switch || terms
cscherrer Apr 13, 2022
a316828
@ifelse macro
cscherrer Apr 13, 2022
2ca07e7
simplify logdensity_rel
cscherrer Apr 13, 2022
15d8ad4
give up on this @ifelse business
cscherrer Apr 13, 2022
535dcbc
bump version
cscherrer Apr 13, 2022
b30963b
Merge branch 'master' into dev
cscherrer Apr 13, 2022
77156ad
Make `instance` non-generated
cscherrer Apr 21, 2022
a733431
working on likelihoods
cscherrer Apr 26, 2022
f494086
update likelihood
cscherrer Apr 28, 2022
999df37
powerweightedmeasure
cscherrer Apr 28, 2022
5815d17
powerweighted update
cscherrer Apr 28, 2022
9016423
more powerweighted methods
cscherrer Apr 28, 2022
c3fff7e
bugfix
cscherrer Apr 28, 2022
a38da82
dropFactoredBase
cscherrer May 2, 2022
7199c22
drop FactoredBase
cscherrer May 2, 2022
a66c070
(::ProductMeasure) | constraint
cscherrer May 3, 2022
8eda66d
update conditional measure
cscherrer May 3, 2022
fa19d73
update Dirac
cscherrer May 3, 2022
9b86a8d
move conditional.jl down in the `include`s
cscherrer May 3, 2022
ceb310b
Kleisli => TransitionKernel
cscherrer May 4, 2022
e4bb7ff
simplify logdensity_def(::PowerMeasure, x)
cscherrer May 4, 2022
a4b3f44
rename kleisli.jl to kernel.jl
cscherrer May 4, 2022
ed5a61d
update Dirac tests
cscherrer May 4, 2022
1f9eeaa
update Half
cscherrer May 4, 2022
f98c7db
get tests passing
cscherrer May 4, 2022
3bb68fe
update kernel
cscherrer May 5, 2022
fafb3a2
Update Project.toml
cscherrer May 5, 2022
c9bafa5
Merge remote-tracking branch 'origin/master' into dev
cscherrer May 5, 2022
536992f
no call-site inlining
cscherrer May 5, 2022
3ae0f65
restrict single-arg `kernel` to <:ParameterizedMeasure
cscherrer May 5, 2022
7364006
export log_likelihood_ratio
cscherrer May 6, 2022
3eebcfb
Drop DensityKind(::Likelihood), at least for now
cscherrer May 6, 2022
38b26c7
isfinite(x) instead of x>-Inf
cscherrer May 6, 2022
ba1eb20
add `condition` constructor
cscherrer May 6, 2022
417b2d0
EOF newline
cscherrer May 6, 2022
c81ca07
simplify logdensity_def for power measures
cscherrer May 9, 2022
11adfcf
finishing up
cscherrer May 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MeasureBase"
uuid = "fa1605e6-acd5-459c-a1e6-7e635759db14"
authors = ["Chad Scherrer <[email protected]> and contributors"]
version = "0.7.0"
version = "0.8.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
11 changes: 6 additions & 5 deletions src/MeasureBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import DensityInterface: densityof
import DensityInterface: DensityKind
using DensityInterface

import Base.iterate
import ConstructionBase
using ConstructionBase: constructorof

Expand All @@ -31,7 +32,7 @@ export AbstractMeasure
import IfElse: ifelse
export logdensity_def
export basemeasure
export basekleisli
export basekernel

"""
inssupport(m, x)
Expand Down Expand Up @@ -97,23 +98,21 @@ using Compat
include("schema.jl")
include("splat.jl")
include("proxies.jl")
include("kleisli.jl")
include("kernel.jl")
include("parameterized.jl")
include("combinators/half.jl")
include("domains.jl")
include("primitive.jl")
include("utils.jl")
include("absolutecontinuity.jl")
# include("absolutecontinuity.jl")

include("primitives/counting.jl")
include("primitives/lebesgue.jl")
include("primitives/dirac.jl")
include("primitives/trivial.jl")

include("combinators/conditional.jl")
include("combinators/bind.jl")
include("combinators/transformedmeasure.jl")
include("combinators/factoredbase.jl")
include("combinators/weighted.jl")
include("combinators/superpose.jl")
include("combinators/product.jl")
Expand All @@ -123,6 +122,8 @@ include("combinators/likelihood.jl")
include("combinators/pointwise.jl")
include("combinators/restricted.jl")
include("combinators/smart-constructors.jl")
include("combinators/powerweighted.jl")
include("combinators/conditional.jl")

include("rand.jl")

Expand Down
14 changes: 13 additions & 1 deletion src/combinators/conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ upcoming changes.
"""
Base.:|(μ::AbstractMeasure, constraint) = ConditionalMeasure(μ, constraint)
cscherrer marked this conversation as resolved.
Show resolved Hide resolved

@inline basemeasure(cm::ConditionalMeasure) = basemeasure(cm.parent) | cm.constraint
@inline basemeasure(cm::ConditionalMeasure) = basemeasure(cm.parent) | cm.constraint

# @generated function Base.:|(μ::ProductMeasure{NamedTuple{M,T}}, constraint::NamedTuple{N}) where {M,T,N}
# newkeys = tuple(setdiff(M, N)...)
# quote
# mar = marginals(μ)
# productmeasure(NamedTuple{$newkeys}(mar))
# end
# end

function Base.:|(μ::ProductMeasure{NamedTuple{M,T}}, constraint::NamedTuple{N}) where {M,T,N}
productmeasure(merge(marginals(μ),rmap(Dirac, constraint)))
end
21 changes: 0 additions & 21 deletions src/combinators/factoredbase.jl

This file was deleted.

10 changes: 3 additions & 7 deletions src/combinators/half.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ end

unhalf(μ::Half) = μ.parent

isnonnegative(x) = x ≥ 0.0

@inline function basemeasure(μ::Half)
constℓ = static(logtwo)
varℓ = Returns(0.0)
base = basemeasure(unhalf(μ))
FactoredBase(constℓ, varℓ, base)
weightedmeasure(static(logtwo), basemeasure(unhalf(μ)))
end

function Base.rand(rng::AbstractRNG, ::Type{T}, μ::Half) where {T}
Expand All @@ -27,7 +22,8 @@ end
logdensity_def(μ::Half, x) = logdensity_def(unhalf(μ), x)

@inline function insupport(d::Half, x)
ifelse(isnonnegative(x), insupport(unhalf(d), x), false)
x ≥ 0 || return false
insupport(unhalf(d), x)
end

testvalue(::Half) = 1.0
53 changes: 43 additions & 10 deletions src/combinators/likelihood.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ export AbstractLikelihood, Likelihood

abstract type AbstractLikelihood end

@inline logdensityof(ℓ::AbstractLikelihood, par) = logdensity_def(ℓ, par)
# @inline function logdensityof(ℓ::AbstractLikelihood, p)
# t() = dynamic(unsafe_logdensityof(ℓ, p))
# f() = -Inf
# ifelse(insupport(ℓ, p), t, f)()
# end

# insupport(ℓ::AbstractLikelihood, p) = insupport(ℓ.k(p), ℓ.x)

@doc raw"""
Likelihood(k::AbstractKleisli, x)
Likelihood(k::AbstractTransitionKernel, x)

"Observe" a value `x`, yielding a function from the parameters to ℝ.

Expand Down Expand Up @@ -109,9 +115,9 @@ struct Likelihood{K,X} <: AbstractLikelihood
k::K
x::X

Likelihood(k::K, x::X) where {K<:AbstractKleisli,X} = new{K,X}(k,x)
Likelihood(k::K, x::X) where {K<:AbstractTransitionKernel,X} = new{K,X}(k,x)
Likelihood(k::K, x::X) where {K<:Function,X} = new{K,X}(k,x)
Likelihood(μ, x) = Likelihood(kleisli(μ), x)
Likelihood(μ, x) = Likelihood(kernel(μ), x)
cscherrer marked this conversation as resolved.
Show resolved Hide resolved
end

# Not really a density, but this makes the code work
Expand All @@ -128,10 +134,37 @@ function Base.show(io::IO, ℓ::Likelihood)
Pretty.pprint(io, ℓ)
end

@inline function logdensity_def(ℓ::Likelihood, p::Tuple)
return logdensity_def(ℓ.k(p), ℓ.x)
end
# @inline function logdensity_def(ℓ::Likelihood, p)
# return logdensity_def(ℓ.k(p), ℓ.x)
# end

@inline function logdensity_def(ℓ::Likelihood, p)
return logdensity_def(ℓ.k((p,)), ℓ.x)
end
# basemeasure(ℓ::Likelihood, p) = basemeasure(ℓ.k(p), ℓ.x)

# basemeasure(ℓ::Likelihood) = @error "Likelihood requires local base measure"

export likelihood

likelihood(k, x, ::NamedTuple{()}) = Likelihood(k, x)
cscherrer marked this conversation as resolved.
Show resolved Hide resolved

likelihood(k, x; kwargs...) = likelihood(k, x, NamedTuple(kwargs))

likelihood(k, x, pars::NamedTuple) = likelihood(kernel(k, pars), x)

likelihood(k::AbstractTransitionKernel, x) = Likelihood(k, x)

"""
log_likelihood_ratio(ℓ::Likelihood, p, q)

Compute the log of the likelihood ratio, in order to compare two choices for
parameters. This is computed as

logdensity_rel(ℓ.k(p), ℓ.k(q), ℓ.x)

Since `logdensity_rel` can leave common base measure unevaluated, this can be
more efficient than

logdensityof(ℓ.k(p), ℓ.x) - logdensityof(ℓ.k(q), ℓ.x)
"""
log_likelihood_ratio(ℓ::Likelihood, p, q) = logdensity_rel(ℓ.k(p), ℓ.k(q), ℓ.x)
theogf marked this conversation as resolved.
Show resolved Hide resolved

# likelihood(k, x; kwargs...) = likelihood(k, x, NamedTuple(kwargs))
31 changes: 19 additions & 12 deletions src/combinators/pointwise.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
export ⊙

struct PointwiseProductMeasure{M,L} <: AbstractMeasure
measure::M
struct PointwiseProductMeasure{P,L} <: AbstractMeasure
prior::P
likelihood::L

function PointwiseProductMeasure(μ::M, ℓ::L) where {M,L}
@assert static_hasmethod(logdensity_def, Tuple{L, gentype(μ)})
return new{M,L}(μ, ℓ)
end
end



iterate(p::PointwiseProductMeasure, i=1) = iterate((p.prior, p.likelihood), i)

function Base.show(io::IO, μ::PointwiseProductMeasure)
io = IOContext(io, :compact => true)
print(io, μ.measure, " ⊙ ", μ.likelihood)
print(io, μ.prior, " ⊙ ", μ.likelihood)
end

function Base.show_unquoted(io::IO, μ::PointwiseProductMeasure, indent::Int, prec::Int)
Expand All @@ -29,12 +28,20 @@ end

⊙(μ, ℓ) = pointwiseproduct(μ, ℓ)

@inline function logdensity_def(d::PointwiseProductMeasure, x)
logdensity_def(d.measure, x) + logdensity_def(d.likelihood, x)
@inline function logdensity_def(d::PointwiseProductMeasure, p)
μ, ℓ = d
logdensityof(ℓ.k(p), ℓ.x)
end

function gentype(d::PointwiseProductMeasure)
@inbounds gentype(d.measure)
gentype(d.prior)
end

@inbounds function insupport(d::PointwiseProductMeasure, p)
μ, ℓ = d
insupport(μ, p) && insupport(ℓ.k(p), ℓ.x)
end

basemeasure(d::PointwiseProductMeasure) = @inbounds basemeasure(d.measure)
basemeasure(d::PointwiseProductMeasure, x) = d.prior

basemeasure(d::PointwiseProductMeasure) = basemeasure(d.prior)
8 changes: 2 additions & 6 deletions src/combinators/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,10 @@ params(d::PowerMeasure) = params(first(marginals(d)))
end

@inline function logdensity_def(d::PowerMeasure{M}, x) where {M}
ℓ = 0.0
# ℓ = zero(typeintersect(AbstractFloat,Core.Compiler.return_type(logdensity_def, Tuple{M,T})))
parent = d.parent
@simd for xj in x
Δℓ = logdensity_def(parent, xj)
ℓ += Δℓ
sum(x) do xj
logdensity_def(parent, xj)
end
end

@generated function logdensity_def(d::PowerMeasure{M, Tuple{Base.OneTo{StaticInt{N}}}}, x) where {M,N}
Expand Down
33 changes: 33 additions & 0 deletions src/combinators/powerweighted.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export ↑

struct PowerWeightedMeasure{M,A} <: AbstractMeasure
parent::M
exponent::A
end

logdensity_def(d::PowerWeightedMeasure, x) = d.exponent * logdensity_def(d.parent, x)

basemeasure(d::PowerWeightedMeasure, x) = basemeasure(d.parent, x) ↑ d.exponent

basemeasure(d::PowerWeightedMeasure) = basemeasure(d.parent) ↑ d.exponent

function powerweightedmeasure(d, α)
isone(α) && return d
PowerWeightedMeasure(d, α)
end

(d::AbstractMeasure) ↑ α = powerweightedmeasure(d, α)

insupport(d::PowerWeightedMeasure, x) = insupport(d.parent, x)

function Base.show(io::IO, d::PowerWeightedMeasure)
print(io, d.parent, " ↑ ", d.exponent)
end

function powerweightedmeasure(d::PowerWeightedMeasure, α)
powerweightedmeasure(d.parent, α * d.exponent)
end

function powerweightedmeasure(d::WeightedMeasure, α)
weightedmeasure(α*d.logweight, powerweightedmeasure(d.base, α))
end
34 changes: 17 additions & 17 deletions src/combinators/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ end
return q
end

@generated function basemeasure(d::ProductMeasure{NamedTuple{N,T}}, x) where {N,T}
q = quote
m = marginals(d)
end
for k in N
qk = QuoteNode(k)
push!(q.args, :($k = basemeasure(getproperty(m, $qk))))
end

vals = map(x -> Expr(:(=), x,x), N)
push!(q.args, Expr(:tuple, vals...))
return q
end
# @generated function basemeasure(d::ProductMeasure{NamedTuple{N,T}}, x) where {N,T}
# q = quote
# m = marginals(d)
# end
# for k in N
# qk = QuoteNode(k)
# push!(q.args, :($k = basemeasure(getproperty(m, $qk))))
# end

# vals = map(x -> Expr(:(=), x,x), N)
# push!(q.args, Expr(:tuple, vals...))
# return q
# end

function basemeasure(μ::ProductMeasure{Base.Generator{I,F}}) where {I,F}
mar = marginals(μ)
Expand All @@ -133,7 +133,7 @@ end

function _basemeasure(μ::ProductMeasure{Base.Generator{I,F}}, ::Type{B}, ::False) where {I,F,B}
mar = marginals(μ)
productmeasure(Base.Generator(basekleisli(mar.f), mar.iter))
productmeasure(Base.Generator(basekernel(mar.f), mar.iter))
end

marginals(μ::ProductMeasure) = μ.marginals
Expand Down Expand Up @@ -174,7 +174,7 @@ using Random: rand!, GLOBAL_RNG, AbstractRNG
end

export rand!
using Random: rand!, GLOBAL_RNG, AbstractRNG
using Random: rand!, GLOBAL_RNG

function _rand(rng::AbstractRNG, ::Type{T}, d::ProductMeasure, mar::AbstractArray) where {T}
elT = typeof(rand(rng, T, first(mar)))
Expand All @@ -187,8 +187,8 @@ end

@inline function insupport(d::AbstractProductMeasure, x::AbstractArray)
mar = marginals(d)
for j in eachindex(x)
@inbounds dynamic(insupport(mar[j], x[j])) || return false
for (j,mj) in enumerate(mar)
dynamic(insupport(mj, x[j])) || return false
end
return true
end
Expand Down
Loading