44Abstract type for a perturbation.
55It's a function that takes a parameter `θ` and returns a perturbed parameter by a distribution `perturbation_dist`.
66
7- All subtypes should have a `perturbation_dist`
7+ !!! warning
8+ All subtypes should implement a `perturbation_dist` field, which is a `ContinuousUnivariateDistribution`.
89
910# Existing implementations
1011- [`AdditivePerturbation`](@ref)
@@ -44,19 +45,37 @@ function (pdc::AdditivePerturbation)(θ::AbstractArray)
4445 return product_distribution (θ .+ ε * perturbation_dist)
4546end
4647
48+ """
49+ $TYPEDEF
50+
51+ Method with parameters to compute the gradient of the logdensity of η = θ + εZ w.r.t. θ., with Z ∼ N(0, 1).
52+
53+ # Fields
54+ $TYPEDFIELDS
55+ """
56+ struct NormalAdditiveGradLogdensity
57+ " perturbation size"
58+ ε:: Float64
59+ end
60+
61+ function NormalAdditiveGradLogdensity (pdc:: AdditivePerturbation )
62+ return NormalAdditiveGradLogdensity (pdc. ε)
63+ end
64+
4765"""
4866$TYPEDSIGNATURES
4967
5068Compute the gradient of the logdensity of η = θ + εZ w.r.t. θ., with Z ∼ N(0, 1).
5169"""
52- function normal_additive_grad_logdensity (ε, η, θ)
70+ function (f:: NormalAdditiveGradLogdensity )(η:: AbstractArray , θ:: AbstractArray )
71+ (; ε) = f
5372 return ((η .- θ) ./ ε^ 2 ,)
5473end
5574
5675"""
5776$TYPEDEF
5877
59- Multiplicative perturbation: θ ↦ θ ⊙ exp(εZ - ε²/2 )
78+ Multiplicative perturbation: θ ↦ θ ⊙ exp(εZ - shift )
6079
6180# Fields
6281$TYPEDFIELDS
@@ -66,6 +85,17 @@ struct MultiplicativePerturbation{F}
6685 perturbation_dist:: F
6786 " perturbation size"
6887 ε:: Float64
88+ " optional shift to have 0 mean, default value is ε²/2"
89+ shift:: Float64
90+ end
91+
92+ """
93+ $TYPEDSIGNATURES
94+
95+ Constructor for [`MultiplicativePerturbation`](@ref).
96+ """
97+ function MultiplicativePerturbation (perturbation_dist, ε, shift= ε^ 2 / 2 )
98+ return MultiplicativePerturbation (perturbation_dist, ε, shift)
6999end
70100
71101"""
@@ -74,16 +104,42 @@ $TYPEDSIGNATURES
74104Apply the multiplicative perturbation to the parameter `θ`.
75105"""
76106function (pdc:: MultiplicativePerturbation )(θ:: AbstractArray )
77- (; perturbation_dist, ε) = pdc
78- return product_distribution (θ .* ExponentialOf (ε * perturbation_dist - ε ^ 2 / 2 ))
107+ (; perturbation_dist, ε, shift ) = pdc
108+ return product_distribution (θ .* ExponentialOf (ε * perturbation_dist - shift ))
79109end
110+
111+ """
112+ $TYPEDEF
113+
114+ Method with parameters to compute the gradient of the logdensity of η = θ ⊙ exp(εZ - shift) w.r.t. θ., with Z ∼ N(0, 1).
115+
116+ # Fields
117+ $TYPEDFIELDS
118+ """
119+ struct NormalMultiplicativeGradLogdensity
120+ " perturbation size"
121+ ε:: Float64
122+ " optional shift to have 0 mean"
123+ shift:: Float64
124+ end
125+
126+ function NormalMultiplicativeGradLogdensity (pdc:: MultiplicativePerturbation )
127+ return NormalMultiplicativeGradLogdensity (pdc. ε, pdc. shift)
128+ end
129+
130+ function NormalMultiplicativeGradLogdensity (ε:: Float64 , shift= ε^ 2 / 2 )
131+ return NormalMultiplicativeGradLogdensity (ε, shift)
132+ end
133+
80134"""
81135$TYPEDSIGNATURES
82136
83- Compute the gradient of the logdensity of η = θ ⊙ exp(εZ - ε²/2) w.r.t. θ., with Z ∼ N(0, 1).
137+ Compute the gradient of the logdensity of η = θ ⊙ exp(εZ - shift) w.r.t. θ., with Z ∼ N(0, 1).
138+
84139!!! warning
85140 η should be a realization of θ, i.e. should be of the same sign.
86141"""
87- function normal_multiplicative_grad_logdensity (ε, η, θ)
88- return (inv .(ε^ 2 .* θ) .* (log .(abs .(η)) - log .(abs .(θ)) .+ (ε^ 2 / 2 )),)
142+ function (f:: NormalMultiplicativeGradLogdensity )(η:: AbstractArray , θ:: AbstractArray )
143+ (; ε, shift) = f
144+ return (inv .(ε^ 2 .* θ) .* (log .(abs .(η)) - log .(abs .(θ)) .+ shift),)
89145end
0 commit comments