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 #88

Merged
merged 392 commits into from
Oct 15, 2022
Merged

Dev #88

Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
392 commits
Select commit Hold shift + click to select a range
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
a76a9ed
Merge branch 'master' into dev
cscherrer May 12, 2022
4998111
updates
cscherrer May 18, 2022
7408e6b
kernel stuff
cscherrer May 18, 2022
2210d3d
kernel stuff
cscherrer May 23, 2022
c4ea293
update showe methods
cscherrer May 23, 2022
a7ba363
ass a TODO
cscherrer May 23, 2022
bc08f16
use `dot` instead of `sum`
cscherrer May 23, 2022
20dafa9
drop old code
cscherrer May 23, 2022
11f78c4
typo
cscherrer May 23, 2022
0db846c
Merge branch 'kernels' into dev
cscherrer May 23, 2022
8f9ab6b
formatting
cscherrer May 23, 2022
0e1ecf1
cleanup
cscherrer May 23, 2022
e2a1d80
kernel updates
cscherrer May 23, 2022
2392d8a
uncomment
cscherrer May 23, 2022
90d4908
bugfix
cscherrer May 23, 2022
dc74704
drop old code
cscherrer May 23, 2022
71be3c5
pretty printing
cscherrer May 23, 2022
51d15a9
exports, cleanup
cscherrer May 24, 2022
79dd234
drop old for.jl
cscherrer May 24, 2022
370c8d1
Make DensityKind(::AbstractLikelihood) = IsDensity()
cscherrer May 24, 2022
c2fa848
update Compat version
cscherrer May 24, 2022
e7ec78d
Merge remote-tracking branch 'origin/master' into dev
cscherrer May 24, 2022
5b32581
Make likelihoods work with Distributions
cscherrer May 24, 2022
4cb7e16
_map(f, x::MappedArrays.ReadonlyMappedArray)
cscherrer Jun 1, 2022
8a6ef5c
export productmeasure
cscherrer Jun 1, 2022
407a8f7
Merge branch 'master' into dev
cscherrer Aug 4, 2022
e8a1be5
AbstractMeasure(::AbstractMeasure)
cscherrer Aug 4, 2022
c1b52cd
Merge branch 'master' into dev
cscherrer Sep 2, 2022
a08119f
Merge branch 'master' into dev
cscherrer Sep 11, 2022
d260e61
fixedrng
cscherrer Sep 11, 2022
3bd74a0
StdNormal
cscherrer Sep 11, 2022
d309637
add SpecialFunctions
cscherrer Sep 11, 2022
1b9cec9
no need to qualify
cscherrer Sep 11, 2022
aa22bef
update basemeasure
cscherrer Sep 11, 2022
249b74e
include stdnormal
cscherrer Sep 11, 2022
ed83e12
include fixedrng
cscherrer Sep 11, 2022
f60f85e
update tests
cscherrer Sep 11, 2022
4765887
using SpecialFunctions
cscherrer Sep 12, 2022
88e1ced
fixing transport_def
cscherrer Sep 12, 2022
023f3a4
transport_def bugfix
cscherrer Sep 12, 2022
7c6e430
StdMeasure(::typeof(randn))
cscherrer Sep 12, 2022
d3f0076
checked_arg for LebesgueMeasure
cscherrer Sep 12, 2022
7bfc189
NoTransformOrigin => NoTransportOrigin
cscherrer Sep 13, 2022
07d2de8
transport interface for pushforwards
cscherrer Sep 13, 2022
1d52b13
transporting pushforwards
cscherrer Sep 13, 2022
af88a14
Use LebesgueMeasure for basemeasure
cscherrer Sep 13, 2022
03fa9fa
updates
cscherrer Sep 13, 2022
0ee3c5a
make testvalue fall back on FixedRNG approach
cscherrer Sep 13, 2022
853013c
un-break testvalue
cscherrer Sep 13, 2022
00fdfd7
CI for Juila 1.8
cscherrer Sep 13, 2022
3132af5
fixes
cscherrer Sep 13, 2022
9bbf833
`rand` on a pushforward calls rand on its parent
cscherrer Sep 13, 2022
d8b52c5
LebesgueMeasure => LebesgueBase
cscherrer Sep 14, 2022
9b6e066
tests passing!
cscherrer Sep 14, 2022
ad279ce
change `invoke` type
cscherrer Sep 14, 2022
0bf97b5
Change `test_interface` to check for 2-arg testvalue
cscherrer Sep 14, 2022
4720e21
manually-specifed inverses
cscherrer Sep 14, 2022
d2c3858
more pushfwd stuff
cscherrer Sep 16, 2022
beb7618
A little less wrong
cscherrer Sep 16, 2022
806f314
add mass interface
cscherrer Sep 19, 2022
d05c044
pullback
cscherrer Sep 19, 2022
c0d5ba3
mass interface
cscherrer Sep 19, 2022
c3e8a22
working on mass interface
cscherrer Sep 19, 2022
617ce41
add some `massof` methods
cscherrer Sep 19, 2022
0490a25
Maybe <:Number is better for invalidations?
cscherrer Sep 19, 2022
f68a932
float instead of Int
cscherrer Sep 19, 2022
c05b5c4
logmassof
cscherrer Sep 19, 2022
a2d3a3b
transports for proxies
cscherrer Sep 20, 2022
ff7af02
drop latent-joint.jl
cscherrer Sep 20, 2022
8482cf3
drop exports
cscherrer Sep 21, 2022
cdeb2a0
Drop `logmassof` for now
cscherrer Sep 21, 2022
b15190c
reorganize Lebesgue measure
cscherrer Sep 21, 2022
cccdaf7
IntervalSets
cscherrer Sep 21, 2022
dffa2ec
proxy(::Lebesgue{MeasureBase.RealNumbers}) = LebesgueBase()
cscherrer Sep 21, 2022
44a1512
calling a "useproxy" measure calls its proxy
cscherrer Sep 21, 2022
590c933
StdUniform()(s::Interval)
cscherrer Sep 21, 2022
d46afbf
typo
cscherrer Sep 21, 2022
0838fa2
(m::AbstractMeasure)(s::Interval)
cscherrer Sep 21, 2022
78cf303
bugfix
cscherrer Sep 21, 2022
20a8378
comment
cscherrer Sep 22, 2022
a16d487
IntervalSets version constraint
cscherrer Sep 22, 2022
fcca309
update dynamic_basemeasure_depth
cscherrer Sep 22, 2022
c6c2bff
format
cscherrer Sep 22, 2022
ef7f1ab
Calling a measure calls `massof`
cscherrer Sep 23, 2022
0cfacfd
work on massof
cscherrer Sep 23, 2022
504936c
AbstractSuperpositionMeasure
cscherrer Sep 23, 2022
0e9a563
fix typo
cscherrer Sep 26, 2022
e4147cf
typo
cscherrer Sep 26, 2022
3eba519
format
cscherrer Sep 26, 2022
2393222
docstrings
cscherrer Sep 27, 2022
8ba4f5b
remove massof(::PowerWeightedMeasure) method
cscherrer Sep 27, 2022
d413544
make `massof` better
cscherrer Sep 27, 2022
2164d71
update testvalue
cscherrer Sep 28, 2022
8e63526
formatting
cscherrer Sep 28, 2022
4bc9d93
update _massof
cscherrer Sep 28, 2022
782c412
Update transports for weighted measures
cscherrer Sep 28, 2022
bb092d5
add chain rules
cscherrer Sep 29, 2022
be8251a
invariant mass under transport
cscherrer Sep 29, 2022
38f0fe2
typo
cscherrer Sep 29, 2022
93804cf
bugfix
cscherrer Sep 30, 2022
4aa2b44
hasmethod => Tricks.static_hasmethod
cscherrer Sep 30, 2022
6e5526f
`massof` methods
cscherrer Oct 1, 2022
93ad7d4
Merge remote-tracking branch 'origin/master' into dev
cscherrer Oct 2, 2022
d0530c3
roll back tranports for WeightedMeasure
cscherrer Oct 3, 2022
e9cffcd
Improve transport implementation and add product support (#97)
oschulz Oct 3, 2022
57ecece
`@useproxy` delegates `massof`
cscherrer Oct 3, 2022
2201dc9
drop CI for nightly
cscherrer Oct 4, 2022
f58dfd4
callable densities (#85)
cscherrer Oct 4, 2022
304163d
Pushfwd-inverses (#98)
cscherrer Oct 6, 2022
5d1807d
remove duplicate method
cscherrer Oct 10, 2022
29e0040
remove duplicate `include`
cscherrer Oct 10, 2022
d44ead0
simplify getdof(::PushforwardMeasure)
cscherrer Oct 11, 2022
6d59555
Stieltjes measure function (#100)
cscherrer Oct 14, 2022
aa50a0d
drop redundant `transport_def`s
cscherrer Oct 15, 2022
3dbbddf
update `pushfwd`
cscherrer Oct 15, 2022
d245978
change name
cscherrer Oct 15, 2022
868c348
add type
cscherrer Oct 15, 2022
00b128a
formatting
cscherrer Oct 15, 2022
97cbb33
fix docstring
cscherrer Oct 15, 2022
b6c039f
depend on FunctinoChains
cscherrer Oct 15, 2022
f96b546
Use fchain
cscherrer Oct 15, 2022
0a21672
simplify transport_def for StdLogistic
cscherrer Oct 15, 2022
046b48c
simplify transport_def for StdNormal
cscherrer Oct 15, 2022
18a9f99
drop redundant method
cscherrer Oct 15, 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
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
version:
- '1.6'
- '1.7'
- '1.8'
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand All @@ -42,6 +43,7 @@ MappedArrays = "0.4"
NaNMath = "0.3, 1"
PrettyPrinting = "0.3, 0.4"
Reexport = "1"
SpecialFunctions = "2"
Static = "0.5, 0.6"
Tricks = "0.1"
julia = "1.3"
Expand Down
2 changes: 2 additions & 0 deletions src/MeasureBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ include("standard/stdmeasure.jl")
include("standard/stduniform.jl")
include("standard/stdexponential.jl")
include("standard/stdlogistic.jl")
include("standard/stdnormal.jl")
include("latent-joint.jl")

include("rand.jl")
include("fixedrng.jl")

include("density.jl")

Expand Down
2 changes: 1 addition & 1 deletion src/combinators/half.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ logdensity_def(μ::Half, x) = logdensity_def(unhalf(μ), x)
insupport(unhalf(d), x)
end

testvalue(::Half) = 1.0
testvalue(::Type{T}, ::Half) where {T} = one(T)
2 changes: 1 addition & 1 deletion src/combinators/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ marginals(μ::ProductMeasure) = μ.marginals
_map(f, args...) = map(f, args...)
_map(f, x::MappedArrays.ReadonlyMappedArray) = mappedarray(f ∘ x.f, x.data)

testvalue(d::AbstractProductMeasure) = _map(testvalue, marginals(d))
testvalue(::Type{T}, d::AbstractProductMeasure) where {T} = _map(m -> testvalue(T, m), marginals(d))

export ⊗

Expand Down
2 changes: 1 addition & 1 deletion src/combinators/spikemixture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ function Base.rand(rng::AbstractRNG, T::Type, μ::SpikeMixture)
return (rand(rng, T) < μ.w) * rand(rng, T, μ.m)
end

testvalue(μ::SpikeMixture) = testvalue(μ.m)
testvalue(::Type{T}, μ::SpikeMixture) where {T} = zero(T)

insupport(μ::SpikeMixture, x) = dynamic(insupport(μ.m, x)) || iszero(x)
2 changes: 1 addition & 1 deletion src/combinators/superpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Pretty.tile(d::SuperpositionMeasure)
result *= Pretty.literal(")")
end

testvalue(μ::SuperpositionMeasure) = testvalue(first(μ.components))
testvalue(::Type{T}, μ::SuperpositionMeasure) where {T} = testvalue(T, first(μ.components))

# SuperpositionMeasure(ms :: AbstractMeasure...) = SuperpositionMeasure{X,length(ms)}(ms)

Expand Down
52 changes: 46 additions & 6 deletions src/combinators/transformedmeasure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,26 @@ struct PushforwardMeasure{FF,IF,M,VC<:TransformVolCorr} <: AbstractPushforward
volcorr::VC
end


gettransform(ν::PushforwardMeasure) = ν.f
parent(ν::PushforwardMeasure) = ν.origin

function transport_def(ν::PushforwardMeasure{FF,IF,M}, μ::M, x) where {FF,IF,M}
if μ == parent(ν)
return ν.f(x)
else
invoke(transport_def, Tuple{Any, PushforwardMeasure, Any}, ν, μ, x)
end
end

function transport_def(μ::M, ν::PushforwardMeasure{FF,IF,M}, y) where {FF,IF,M}
if μ == parent(ν)
return ν.inv_f(y)
else
invoke(transport_def, Tuple{Any, PushforwardMeasure, Any}, μ, ν, y)
end
end

function Pretty.tile(ν::PushforwardMeasure)
Pretty.list_layout(Pretty.tile.([ν.f, ν.inv_f, ν.origin]); prefix = :PushforwardMeasure)
end
Expand Down Expand Up @@ -67,7 +84,7 @@ end

insupport(ν::PushforwardMeasure, y) = insupport(transport_origin(ν), to_origin(ν, y))

testvalue(ν::PushforwardMeasure) = from_origin(ν, testvalue(transport_origin(ν)))
testvalue(::Type{T}, ν::PushforwardMeasure) where {T} = from_origin(ν, testvalue(T, transport_origin(ν)))

@inline function basemeasure(ν::PushforwardMeasure)
PushforwardMeasure(ν.f, ν.inv_f, basemeasure(transport_origin(ν)), NoVolCorr())
Expand All @@ -91,15 +108,38 @@ end
@inline to_origin(ν::PushforwardMeasure, y) = ν.inv_f(y)

function Base.rand(rng::AbstractRNG, ::Type{T}, ν::PushforwardMeasure) where {T}
return from_origin(ν, rand(rng, T, transport_origin(ν)))
return ν.f(rand(rng, T, parent(ν)))
end

export pushfwd

"""
pushfwd(f, μ, volcorr = WithVolCorr())
pushfwd(f, [f_inverse,] μ, volcorr = WithVolCorr())

Return the [pushforward measure](https://en.wikipedia.org/wiki/Pushforward_measure)
from `μ` the [measurable function](https://en.wikipedia.org/wiki/Measurable_function) `f`.
Return the [pushforward
measure](https://en.wikipedia.org/wiki/Pushforward_measure) from `μ` the
[measurable function](https://en.wikipedia.org/wiki/Measurable_function) `f`.

If `f_inverse` is specified, it must be a valid inverse of the function given by
restricting `f` to the support of `μ`.
"""
pushfwd(f, μ, volcorr = WithVolCorr()) = PushforwardMeasure(f, inverse(f), μ, volcorr)
pushfwd(f, μ, volcorr::TransformVolCorr = WithVolCorr()) = PushforwardMeasure(f, inverse(f), μ, volcorr)

pushfwd(f, finv, μ, volcorr::TransformVolCorr = WithVolCorr()) = PushforwardMeasure(f, finv, μ, volcorr)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not working yet:

julia> μ = Lebesgue() ^ 2
Lebesgue(ℝ) ^ 2

julia> f(x) = [2x[1], x[2]]
f (generic function with 1 method)

julia> finv(y) = [y[1]/2, y[2]]
finv (generic function with 1 method)

julia> logdensityof(pushfwd(f, finv, μ), zeros(2))
ERROR: MethodError: no method matching iterate(::ChangesOfVariables.NoLogAbsDetJacobian{typeof(finv), Vector{Float64}})
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:872
  iterate(::Union{LinRange, StepRangeLen}, ::Integer) at range.jl:872
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}} at dict.jl:712
  ...
Stacktrace:
 [1] indexed_iterate(I::ChangesOfVariables.NoLogAbsDetJacobian{typeof(finv), Vector{Float64}}, i::Int64)
   @ Base ./tuple.jl:91
 [2] logdensity_def
   @ ~/git/MeasureBase.jl/src/combinators/transformedmeasure.jl:62 [inlined]
 [3] unsafe_logdensityof
   @ ~/git/MeasureBase.jl/src/density.jl:139 [inlined]
 [4] logdensityof::PushforwardMeasure{typeof(f), typeof(finv), PowerMeasure{Lebesgue{MeasureBase.RealNumbers}, Tuple{Base.OneTo{Int64}}}, MeasureBase.WithVolCorr}, x::Vector{Float64})
   @ MeasureBase ~/git/MeasureBase.jl/src/density.jl:123
 [5] top-level scope
   @ REPL[46]:1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works!!

julia> using MeasureBase, ForwardDiff, ChangesOfVariables, IrrationalConstants

julia> μ = Lebesgue(MeasureBase.BoundedReals(-halfπ, halfπ))
Lebesgue(MeasureBase.BoundedReals{Float64, Irrational{:halfπ}}(-1.5707963267948966, halfπ))

julia> function f(x)
           asin(x)
       end
f (generic function with 1 method)

julia> function finv(y)
           if -halfπ  y  halfπ
               return sin(y)
           else
               @error "finv is only defined on [-π/2, π/2]"
           end
       end
finv (generic function with 1 method)

julia> function withlogjac(f, x)
           dx = ForwardDiff.Dual{ForwardDiff.Tag{typeof(f)}}(x, 1.0)
           dy = f(dx)
           value = dy.value
           deriv = first(dy.partials)
           (value, log(abs(deriv)))   
       end
withlogjac (generic function with 1 method)

julia> function ChangesOfVariables.with_logabsdet_jacobian(::typeof(f), x)
           withlogjac(f, x)
       end

julia> function ChangesOfVariables.with_logabsdet_jacobian(::typeof(finv), x)
           withlogjac(finv, x)
       end

julia> # Both of these pass
       ChangesOfVariables.test_with_logabsdet_jacobian(f, 2 * rand() - 1, ForwardDiff.derivative)
Test Summary:                                                 | Pass  Total  Time
test_with_logabsdet_jacobian: f with input 0.9708087843236501 |    2      2  0.0s

julia> ChangesOfVariables.test_with_logabsdet_jacobian(finv, π * rand() - halfπ, ForwardDiff.derivative)
Test Summary:                                                     | Pass  Total  Time
test_with_logabsdet_jacobian: finv with input 0.43781065021456067 |    2      2  0.0s

julia> ν = pushfwd(f, finv, μ)
PushforwardMeasure(
    f,
    finv,
    Lebesgue(MeasureBase.BoundedReals{Float64, Irrational{:halfπ}}(-1.5707963267948966, halfπ)))

julia> logdensityof(ν, π/4)
-0.3465735902799726

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 juhu!


@inline function pushfwd(f, μ::PushforwardMeasure, volcorr::TransformVolCorr = WithVolCorr())
_pushfwd(f, inverse(f), μ, volcorr, μ.volcorr)
end


@inline function pushfwd(f, finv, μ::PushforwardMeasure, volcorr::TransformVolCorr = WithVolCorr())
_pushfwd(f, finv, μ, volcorr, μ.volcorr)
end

@inline function _pushfwd(f, finv, μ::PushforwardMeasure, vf::V, vμ::V) where {V<:TransformVolCorr}
pushfwd(f ∘ μ.f, μ.inv_f ∘ finv, v)
end

@inline function _pushfwd(f, finv, μ::PushforwardMeasure, vf, vμ)
PushforwardMeasure(f, finv, μ, vf)
end
2 changes: 2 additions & 0 deletions src/combinators/weighted.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function Base.rand(rng::AbstractRNG, ::Type{T}, μ::AbstractWeightedMeasure) whe
rand(rng, T, basemeasure(μ))
end

testvalue(::Type{T}, μ::AbstractWeightedMeasure) where {T} = testvalue(T, basemeasure(μ))

###############################################################################

struct WeightedMeasure{R,M} <: AbstractWeightedMeasure
Expand Down
6 changes: 3 additions & 3 deletions src/domains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Base.maximum(b::BoundedReals) = b.upper
Base.show(io::IO, ::typeof(ℝ₊)) = print(io, "ℝ₊")
Base.show(io::IO, ::typeof(𝕀)) = print(io, "𝕀")

testvalue(::typeof(ℝ)) = 0.0
testvalue(::typeof(ℝ₊)) = 1.0
testvalue(::typeof(𝕀)) = 0.5
testvalue(::Type{T}, ::typeof(ℝ)) where {T} = zero(T)
testvalue(::Type{T}, ::typeof(ℝ₊)) where {T} = one(T)
testvalue(::Type{T}, ::typeof(𝕀)) where {T} = one(T) / 2

abstract type IntegerDomain <: AbstractDomain end

Expand Down
19 changes: 19 additions & 0 deletions src/fixedrng.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export FixedRNG
struct FixedRNG <: AbstractRNG end

Base.rand(::FixedRNG) = one(Float64) / 2
Random.randn(::FixedRNG) = zero(Float64)
Random.randexp(::FixedRNG) = one(Float64)

Base.rand(::FixedRNG, ::Type{T}) where {T<:Real} = one(T) / 2
Random.randn(::FixedRNG, ::Type{T}) where {T<:Real} = zero(T)
Random.randexp(::FixedRNG, ::Type{T}) where {T<:Real} = one(T)

# We need concrete type parameters to avoid amiguity for these cases
for T in [Float16, Float32, Float64]
@eval begin
Base.rand(::FixedRNG, ::Type{$T}) = one($T) / 2
Random.randn(::FixedRNG, ::Type{$T}) = zero($T)
Random.randexp(::FixedRNG, ::Type{$T}) = one($T)
end
end
4 changes: 2 additions & 2 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ function test_interface(μ::M) where {M}
###########################################################################
# testvalue, logdensityof

x = @inferred testvalue(μ)
x = @inferred testvalue(Float64, μ)
β = @inferred basemeasure(μ, x)

ℓμ = @inferred logdensityof(μ, x)
ℓβ = @inferred logdensityof(β, x)

@test ℓμ ≈ logdensity_def(μ, x) + ℓβ

@test logdensity_def(μ, testvalue(μ)) isa Real
@test logdensity_def(μ, testvalue(Float64, μ)) isa Real
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/primitives/counting.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Counting measure

export Counting, CountingMeasure
export Counting, CountingBase

struct CountingMeasure <: PrimitiveMeasure end
struct CountingBase <: PrimitiveMeasure end

insupport(::CountingMeasure, x) = true
insupport(::CountingBase, x) = true

struct Counting{T} <: AbstractMeasure
support::T
Expand All @@ -16,15 +16,15 @@ function logdensity_def(μ::Counting, x)
insupport(μ, x) ? 0.0 : -Inf
end

basemeasure(::Counting) = CountingMeasure()
basemeasure(::Counting) = CountingBase()

Counting() = Counting(ℤ)

testvalue(d::Counting) = testvalue(d.support)
testvalue(::Type{T}, d::Counting) where {T} = testvalue(T, d.support)

proxy(d::Counting) = restrict(in(d.support), CountingMeasure())
proxy(d::Counting) = restrict(in(d.support), CountingBase())

Base.:∘(::typeof(basemeasure), ::Type{Counting}) = CountingMeasure()
Base.:∘(::typeof(basemeasure), ::Type{Counting}) = CountingBase()

Base.show(io::IO, d::Counting) = print(io, "Counting(", d.support, ")")

Expand Down
4 changes: 1 addition & 3 deletions src/primitives/dirac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function (μ::Dirac{X})(s) where {X}
return 0
end

basemeasure(d::Dirac) = CountingMeasure()
basemeasure(d::Dirac) = CountingBase()

logdensity_def(μ::Dirac, x) = 0.0

Expand All @@ -26,8 +26,6 @@ export dirac

dirac(d::AbstractMeasure) = Dirac(rand(d))

testvalue(d::Dirac) = d.x

insupport(d::Dirac, x) = x == d.x

@inline getdof(::Dirac) = static(0)
Expand Down
26 changes: 13 additions & 13 deletions src/primitives/lebesgue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

export Lebesgue

struct LebesgueMeasure <: PrimitiveMeasure end
struct LebesgueBase <: PrimitiveMeasure end

testvalue(::LebesgueMeasure) = 0.0
testvalue(::LebesgueBase) = 0.0

insupport(::LebesgueMeasure, x) = true
insupport(::LebesgueBase, x) = true

insupport(::LebesgueMeasure) = Returns(true)
insupport(::LebesgueBase) = Returns(true)

struct Lebesgue{T} <: AbstractMeasure
support::T
Expand All @@ -22,29 +22,29 @@ gentype(::Lebesgue) = Float64

Lebesgue() = Lebesgue(ℝ)

# basemeasure(::Lebesgue) = LebesgueMeasure()
# basemeasure(::Lebesgue) = LebesgueBase()

testvalue(d::Lebesgue) = testvalue(d.support)
testvalue(::Type{T}, d::Lebesgue) where {T} = testvalue(T, d.support)::T

proxy(d::Lebesgue) = restrict(in(d.support), LebesgueMeasure())
proxy(d::Lebesgue) = restrict(in(d.support), LebesgueBase())
@useproxy Lebesgue

Base.:∘(::typeof(basemeasure), ::Type{Lebesgue}) = LebesgueMeasure()
Base.:∘(::typeof(basemeasure), ::Type{Lebesgue}) = LebesgueBase()

Base.show(io::IO, d::Lebesgue) = print(io, "Lebesgue(", d.support, ")")

insupport(μ::Lebesgue, x) = x ∈ μ.support

insupport(::Lebesgue{RealNumbers}, ::Real) = true

logdensity_def(::LebesgueMeasure, ::CountingMeasure, x) = -Inf
logdensity_def(::LebesgueBase, ::CountingBase, x) = -Inf

logdensity_def(::CountingMeasure, ::LebesgueMeasure, x) = Inf
logdensity_def(::CountingBase, ::LebesgueBase, x) = Inf

@inline getdof(::Lebesgue) = static(1)
@inline getdof(::LebesgueBase) = static(1)

@inline checked_arg(::Lebesgue, x::Real) = x
@inline checked_arg(::LebesgueBase, x::Real) = x

@propagate_inbounds function checked_arg(::Lebesgue, x::Any)
@propagate_inbounds function checked_arg(::LebesgueBase, x::Any)
@boundscheck throw(ArgumentError("Invalid variate type for measure"))
end
2 changes: 1 addition & 1 deletion src/standard/stdexponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export StdExponential
insupport(d::StdExponential, x) = x ≥ zero(x)

@inline logdensity_def(::StdExponential, x) = -x
@inline basemeasure(::StdExponential) = Lebesgue()
@inline basemeasure(::StdExponential) = LebesgueBase()

@inline transport_def(::StdUniform, μ::StdExponential, x) = -expm1(-x)
@inline transport_def(::StdExponential, μ::StdUniform, x) = -log1p(-x)
Expand Down
2 changes: 1 addition & 1 deletion src/standard/stdlogistic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export StdLogistic
@inline insupport(d::StdLogistic, x) = true

@inline logdensity_def(::StdLogistic, x) = (u = -abs(x); u - 2 * log1pexp(u))
@inline basemeasure(::StdLogistic) = Lebesgue()
@inline basemeasure(::StdLogistic) = LebesgueBase()

@inline transport_def(::StdUniform, μ::StdLogistic, x) = logistic(x)
@inline transport_def(::StdLogistic, μ::StdUniform, x) = logit(x)
Expand Down
1 change: 1 addition & 0 deletions src/standard/stdmeasure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ abstract type StdMeasure <: AbstractMeasure end

StdMeasure(::typeof(rand)) = StdUniform()
StdMeasure(::typeof(randexp)) = StdExponential()
StdMeasure(::typeof(randn)) = StdNormal()

@inline check_dof(::StdMeasure, ::StdMeasure) = nothing

Expand Down
20 changes: 20 additions & 0 deletions src/standard/stdnormal.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using SpecialFunctions: erfc, erfcinv
using IrrationalConstants: invsqrt2

struct StdNormal <: StdMeasure end

export StdNormal

@inline insupport(d::StdNormal, x) = true

@inline logdensity_def(::StdNormal, x) = -x^2 / 2
@inline basemeasure(::StdNormal) = WeightedMeasure(static(-0.5 * log2π), LebesgueBase())

@inline getdof(::StdNormal) = static(1)

@inline transport_def(::StdUniform, μ::StdNormal, x) = erfc(-x * invsqrt2) / 2
@inline transport_def(::StdNormal, μ::StdUniform, p) = -erfcinv(2*p) * sqrt2

@inline Base.rand(rng::Random.AbstractRNG, ::Type{T}, ::StdNormal) where {T} = randn(rng, T)


2 changes: 1 addition & 1 deletion src/standard/stduniform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export StdUniform
insupport(d::StdUniform, x) = zero(x) ≤ x ≤ one(x)

@inline logdensity_def(::StdUniform, x) = zero(x)
@inline basemeasure(::StdUniform) = Lebesgue()
@inline basemeasure(::StdUniform) = LebesgueBase()

Base.rand(rng::Random.AbstractRNG, ::Type{T}, ::StdUniform) where {T} = rand(rng, T)
Loading