Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
- 'min'
- '1' # will automatically expand to the latest stable 1.x release of Julia.
# - 'nightly' # NOTE: nightly disabled as it currently fails
os:
- ubuntu-latest
Expand All @@ -30,7 +30,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
Expand All @@ -43,7 +43,9 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
- uses: codecov/codecov-action@v5
with:
file: lcov.info
# NOTE: you need to add the token to Github secrets, see
# https://docs.codecov.com/docs/adding-the-codecov-token
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false # or true if you want CI to fail when Codecov fails
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[compat]
ArgCheck = "1, 2"
DocStringExtensions = "0.8, 0.9"
julia = "1.6"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff"
TransformedLogDensities = "f9bc47f6-f3f8-4f3b-ab21-f8bc73906f26"
Expand Down
24 changes: 14 additions & 10 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ which is added to the log likelihood to obtain the log posterior.

It is useful to define a *callable* that implements this, taking some vector `x` as an input and calculating the summary statistics, then, when called with a `NamedTuple` containing the parameters, evaluating to the log posterior.

```@example 1
```@setup A
using LogDensityProblems
```

```@example A
using Random; Random.seed!(1) # hide
using Statistics, SimpleUnPack # imported for our implementation
using Statistics

struct NormalPosterior{T} # contains the summary statistics
N::Int
Expand All @@ -63,8 +67,8 @@ end

# define a callable that unpacks parameters, and evaluates the log likelihood
function (problem::NormalPosterior)(θ)
@unpack μ, σ = θ
@unpack N, x̄, S = problem
(; μ, σ) = θ
(; N, x̄, S) = problem
loglikelihood = -N * (log(σ) + (S + abs2(μ - x̄)) / (2 * abs2(σ)))
logprior = - abs2(σ)/8 - abs2(μ)/50
loglikelihood + logprior
Expand All @@ -76,7 +80,7 @@ nothing # hide

Let's try out the posterior calculation:

```@repl 1
```@repl A
problem((μ = 0.0, σ = 1.0))
```

Expand All @@ -90,13 +94,13 @@ In our example, we require ``\sigma > 0``, otherwise the problem is meaningless.
!!! note
Since version 1.0, TransformedLogDensity has been moved to the package TransformedLogDensities.

```@repl 1
```@repl A
using LogDensityProblems, TransformVariables, TransformedLogDensities
ℓ = TransformedLogDensity(as((μ = asℝ, σ = asℝ₊)), problem)
```

Then we can query the dimension of this problem, and evaluate the log density:
```@repl 1
```@repl A
LogDensityProblems.dimension(ℓ)
LogDensityProblems.logdensity(ℓ, zeros(2))
```
Expand All @@ -108,7 +112,7 @@ LogDensityProblems.logdensity(ℓ, zeros(2))

If you prefer to implement the transformation yourself, you just have to define the following three methods for your problem: declare that it can evaluate log densities (but not their gradient, hence the `0` order), allow the dimension of the problem to be queried, and then finally code the density calculation with the transformation. (Note that using `TransformedLogDensities.TransformedLogDensity` takes care of all of these for you, as shown above).

```@example 1
```@example A
function LogDensityProblems.capabilities(::Type{<:NormalPosterior})
LogDensityProblems.LogDensityOrder{0}()
end
Expand All @@ -123,7 +127,7 @@ end
nothing # hide
```

```@repl 1
```@repl A
LogDensityProblems.logdensity(problem, zeros(2))
```

Expand All @@ -134,7 +138,7 @@ Here we use the exponential function to transform from ``\mathbb{R}`` to the pos
Using either definition, you can transform to another object which is capable of evaluating the *gradient*, using automatic differentiation. For this, you need the [LogDensityProblemsAD.jl](https://github.com/tpapp/LogDensityProblemsAD.jl) package.

Now observe that we can obtain gradients, too:
```@repl 1
```@repl A
import ForwardDiff
using LogDensityProblemsAD
∇ℓ = ADgradient(:ForwardDiff, ℓ)
Expand Down
Loading