Skip to content

Commit f4f8385

Browse files
authored
Refactor (#178)
* Dirichlet(k::Integer, α) = Dirichlet(Fill(α, k)) * export TransformVariables as TV * drop redundant import * 0.0 => zero(Float64) * drop outdated Dists.logpdf * update StudentT * drop redundant import * update Uniform * bump MeasureBase version * reworking beta * small update to StudentT * basemeasure for discrete Distributions * using LogExpFunctions => import LogExpFunctions * quoteof(::Chain) * prettyprinting and chain-mucking * Some refactoring for Markov chains * import MeasureBase: ≪ * version bound for PrettyPrinting * copy(rng) might change its type (e.g. GLOBAL_RNG) * tests pass * cleaning up * more cleanup * big update * get tests passing * formatting * oops typo * move affine to MeasureTheory * updating * Val => StaticSymbol * more fixes * fix fix fix * more logdesnity => logdensity_def * more logdesnity fixes * debugging * formatting * bugfixes * working on tests * updates * working on tests * tests passing! * refactor * working on tests * drop static weight for now * fix sampling from ProductMeasure{<:Base.Generator} * tests passing!! * more stuff * constructor => constructorof * constructor =? construtorof * updates * working on tests * fix Dirichlet * update Bernoulli * working on tests * bugfixes for RealizedSamples * tests passing!! * tighten down inference * as(::PowerMeasure) * drop type-level stuff * using InverseFunctions.jl * tests passing * drop old tbasemeasure_type code * update logdensity_def(d::Affine{(:σ,)}, x) * update license * Move `solve` to MeasureTheory * better type stability * `solve!` moved to MeasureTheory * exponential families * work on exponential families * ExpFamMeasure * applying an exponential family * kwstruct for Laplace * update README (license description) * kwstruct for Laplace * kwstruct for Bernoulli * Simplify logdensity_def(::DynamicFor{M}, y) for now * using ConcreteStructs * update dependencies * inner constructors * inline logjac * exponential families and tweedie distributions * `mydot` * Soss tests passing * tests passing * inline * condiional meaures * update affine smart constructor * update for type stability * tests passing! * updates * Make Bernoulli more efficient * license * compat * Run CI on Juila 1.6 * fix typo * bump version
1 parent 7102906 commit f4f8385

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1871
-587
lines changed

.JuliaFormatter.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
indent = 4
2+
margin = 92
3+
always_for_in = true
4+
whitespace_typedefs = false
5+
whitespace_ops_in_indices = false
6+
remove_extra_newlines = true
7+
import_to_using = false
8+
pipe_to_function_call = false
9+
short_to_long_function_def = false
10+
always_use_return = false
11+
whitespace_in_kwargs = true
12+
annotate_untyped_fields_with_any = false
13+
format_docstrings = false
14+
align_struct_field = true
15+
align_conditional = true
16+
align_assignment = true
17+
align_pair_arrow = true
18+
conditional_to_if = true
19+
normalize_line_endings = "unix"
20+
align_matrix = false

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
fail-fast: false
2323
matrix:
2424
version:
25-
- '1.5'
25+
- '1.6'
2626
- '1'
2727
os:
2828
- ubuntu-latest

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

Project.toml

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
name = "MeasureTheory"
22
uuid = "eadaa1a4-d27c-401d-8699-e962e1bbc33b"
33
authors = ["Chad Scherrer <[email protected]> and contributors"]
4-
version = "0.13.2"
4+
version = "0.14.0"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
8+
ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
9+
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
810
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
911
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
12+
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
1013
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1114
DynamicIterators = "6c76993d-992e-5bf1-9e63-34920a5a5a38"
15+
FLoops = "cc61a311-1640-44b5-9fba-1b764f453329"
1216
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
13-
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
17+
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
1418
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
19+
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
1520
KeywordCalls = "4d827475-d3e4-43d6-abe3-9688362ede9f"
21+
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
1622
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1723
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
1824
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
@@ -26,32 +32,42 @@ PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
2632
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2733
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2834
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
35+
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
36+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2937
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
3038
TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff"
3139
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
3240

3341
[compat]
3442
Accessors = "0.1"
43+
ChangesOfVariables = "0.1"
44+
Compat = "3.35"
3545
ConcreteStructs = "0.2"
3646
ConstructionBase = "1.3"
47+
DensityInterface = "0.4"
3748
Distributions = "0.25"
3849
DynamicIterators = "0.4"
39-
FillArrays = "0.12"
40-
InfiniteArrays = "0.11, 0.12"
50+
FLoops = "0.2"
51+
FillArrays = "0.12, 0.13"
52+
Infinities = "0.1"
53+
InverseFunctions = "0.1"
4154
KeywordCalls = "0.2"
55+
LazyArrays = "0.21, 0.22"
4256
LogExpFunctions = "0.3.3"
4357
MLStyle = "0.4"
4458
MacroTools = "0.5"
4559
MappedArrays = "0.4"
46-
MeasureBase = "0.5"
47-
NamedTupleTools = "0.13"
60+
MeasureBase = "0.6"
61+
NamedTupleTools = "0.13, 0.14"
4862
NestedTuples = "0.3"
4963
PositiveFactorizations = "0.2"
50-
PrettyPrinting = "0.3"
64+
PrettyPrinting = "0.3, 0.4"
5165
Reexport = "1"
52-
SpecialFunctions = "1"
66+
SpecialFunctions = "1, 2"
67+
Static = "0.5, 0.6"
68+
StaticArrays = "1.3"
5369
StatsFuns = "0.9"
54-
TransformVariables = "0.4"
70+
TransformVariables = "0.5, 0.6"
5571
Tricks = "0.1"
5672
julia = "1.5"
5773

docs/src/adding.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ The `≪` (typed as `\ll <TAB>`) can be read "is dominated by". This just means
3333

3434
### Defining a Log Density
3535

36-
Most computations involve log-densities rather than densities themselves, so these are our first priority. `density(d,x)` will default to `exp(logdensity(d,x))`, but you can add a separate method if it's more efficient.
36+
Most computations involve log-densities rather than densities themselves, so these are our first priority. `density(d,x)` will default to `exp(logdensity_def(d,x))`, but you can add a separate method if it's more efficient.
3737

3838
The definition is simple:
3939
```julia
40-
logdensity(d::Normal{()} , x) = - x^2 / 2
40+
logdensity_def(d::Normal{()} , x) = - x^2 / 2
4141
```
4242

4343
There are a few things here worth noting.
@@ -53,7 +53,7 @@ Also, there's nothing here about `μ` and `σ`. These _location-scale parameters
5353
Let's look at another example, the Beta distribution. Here the base measure is `Lebesgue(𝕀)` (support is the unit interval). The log-density is
5454

5555
```julia
56-
function logdensity(d::Beta{(:α, :β)}, x)
56+
@inline function logdensity_def(d::Beta{(:α, :β)}, x)
5757
return (d.α - 1) * log(x) + (d.β - 1) * log(1 - x) - logbeta(d.α, d.β)
5858
end
5959
```
@@ -114,15 +114,15 @@ basemeasure(μ::Lebesgue) = μ
114114

115115
isprimitive(::Lebesgue) = true
116116

117-
sampletype(::Lebesgue{ℝ}) = Float64
118-
sampletype(::Lebesgue{ℝ₊}) = Float64
119-
sampletype(::Lebesgue{𝕀}) = Float64
117+
gentype(::Lebesgue{ℝ}) = Float64
118+
gentype(::Lebesgue{ℝ₊}) = Float64
119+
gentype(::Lebesgue{𝕀}) = Float64
120120

121-
logdensity(::Lebesgue, x) = zero(float(x))
121+
logdensity_def(::Lebesgue, x) = zero(float(x))
122122
```
123123

124-
We haven't yet talked about `sampletype`. When you call `rand` without specifying a type, there needs to be a default. That default is the `sampletype`. This only needs to be defined for primitive measures, because others will fall back on
124+
We haven't yet talked about `gentype`. When you call `rand` without specifying a type, there needs to be a default. That default is the `gentype`. This only needs to be defined for primitive measures, because others will fall back on
125125

126126
```julia
127-
sampletype::AbstractMeasure) = sampletype(basemeasure(μ))
127+
gentype::AbstractMeasure) = gentype(basemeasure(μ))
128128
```

docs/src/affine.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ z = σ⁻¹ (x - μ)
6262
so the log-density is
6363

6464
```julia
65-
logdensity(d::Normal{(:μ,:σ)}, x) = logdensity(d.σ \ (x - d.μ)) - logdet(d.σ)
65+
logdensity_def(d::Normal{(:μ,:σ)}, x) = logdensity_def(d.σ \ (x - d.μ)) - logdet(d.σ)
6666
```
6767

6868
Here the `- logdet(σ)` is the "log absolute Jacobian", required to account for the stretching of the space.
@@ -74,7 +74,7 @@ In addition to the covariance ``Σ``, it's also common to parameterize a multiva
7474
This parameterization enables more efficient calculation of the log-density using only multiplication and addition,
7575

7676
```julia
77-
logdensity(d::Normal{(:μ,:ω)}, x) = logdensity(d.ω * (x - d.μ)) + logdet(d.ω)
77+
logdensity_def(d::Normal{(:μ,:ω)}, x) = logdensity_def(d.ω * (x - d.μ)) + logdet(d.ω)
7878
```
7979

8080
## `AffineTransform`
@@ -94,7 +94,7 @@ In the univariate case this is relatively simple to invert. But if `σ` is a mat
9494
With multiple parameterizations of a given family of measures, we can work around these issues. The inverse transform of a ``(μ,σ)`` transform will be in terms of ``(μ,ω)``, and vice-versa. So
9595

9696
```julia
97-
julia> f⁻¹ = inv(f)
97+
julia> f⁻¹ = inverse(f)
9898
AffineTransform{(:μ, :ω), Tuple{Float64, Float64}}((μ = -1.5, ω = 2.0))
9999

100100
julia> f(f⁻¹(4))

docs/src/old_readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,21 @@ A density doesn't exist by itself, but is defined relative to some _base measure
153153
```julia
154154
@implement Density{Normal{X,P},X} where {X, P <: NamedTuple{(:μ, :σ)}} begin
155155
basemeasure(d) = Lebesgue(X)
156-
logdensity(d, x) = - (log(2) + log(π)) / 2 - log(d.par.σ) - (x - d.par.μ)^2 / (2 * d.par.σ^2)
156+
logdensity_def(d, x) = - (log(2) + log(π)) / 2 - log(d.par.σ) - (x - d.par.μ)^2 / (2 * d.par.σ^2)
157157
end
158158
```
159159

160160
Now we can compute the log-density:
161161

162162
```julia
163-
julia> logdensity(Normal(0.0, 0.5), 1.0)
163+
julia> logdensity_def(Normal(0.0, 0.5), 1.0)
164164
-2.2257913526447273
165165
```
166166

167167
And just to check that our default is working,
168168

169169
```julia
170-
julia> logdensity(Normal=0.0, σ=0.5), 1.0)
170+
julia> logdensity_def(Normal=0.0, σ=0.5), 1.0)
171171
-2.2257913526447273
172172
```
173173

@@ -178,14 +178,14 @@ eltype(::Type{Normal}, ::Type{NamedTuple{(:μ, :τ), Tuple{A, B}}}) where {A,B}
178178

179179
@implement Density{Normal{X,P},X} where {X, P <: NamedTuple{(:μ, :τ)}} begin
180180
basemeasure(d) = Lebesgue(X)
181-
logdensity(d, x) = - (log(2) + log(π) - log(d.par.τ) + d.par.τ * (x - d.par.μ)^2) / 2
181+
logdensity_def(d, x) = - (log(2) + log(π) - log(d.par.τ) + d.par.τ * (x - d.par.μ)^2) / 2
182182
end
183183
```
184184

185185
And another check:
186186

187187
```julia
188-
julia> logdensity(Normal=0.0, τ=4.0), 1.0)
188+
julia> logdensity_def(Normal=0.0, τ=4.0), 1.0)
189189
-2.2257913526447273
190190
```
191191

jet-test.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using MeasureTheory
2+
3+
dist = For(3) do j Normal=j) end
4+
5+
b = basemeasure_depth(dist)

scratch/orderstats.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ struct OrderStatistic{n,r,B} <: AbstractMeasure
22
base::B
33
end
44

5-
function basemeasure(d::OrderStatistic{n,r})
5+
@inline function basemeasure(d::OrderStatistic{n,r})
66
WeightedMeasure(logbeta(n+1, r+1), d.base)
77
end
88

9-
function logdensity(d::OrderStatistic{n,r}, x)
9+
@inline function logdensity_def(d::OrderStatistic{n,r}, x)
1010
logF(x) = logcdf(d.base, x)
1111
logFᶜ(x) = logccdf(d.base, x)
1212
return (r - 1) * logF(x) + (n - r) * logFᶜ(x)

0 commit comments

Comments
 (0)