@@ -43,113 +43,6 @@ Concrete type for maximum a posteriori estimation. Only used for the Optim.jl in
43
43
"""
44
44
struct MAP <: ModeEstimator end
45
45
46
- # Most of these functions for LogPriorWithoutJacobianAccumulator are copied from
47
- # LogPriorAccumulator. The only one that is different is the accumulate_assume!! one.
48
- """
49
- LogPriorWithoutJacobianAccumulator{T} <: DynamicPPL.AbstractAccumulator
50
-
51
- Exactly like DynamicPPL.LogPriorAccumulator, but does not include the log determinant of the
52
- Jacobian of any variable transformations.
53
-
54
- Used for MAP optimisation.
55
- """
56
- struct LogPriorWithoutJacobianAccumulator{T} <: DynamicPPL.AbstractAccumulator
57
- logp:: T
58
- end
59
-
60
- """
61
- LogPriorWithoutJacobianAccumulator{T}()
62
-
63
- Create a new `LogPriorWithoutJacobianAccumulator` accumulator with the log prior initialized to zero.
64
- """
65
- LogPriorWithoutJacobianAccumulator {T} () where {T<: Real } =
66
- LogPriorWithoutJacobianAccumulator (zero (T))
67
- function LogPriorWithoutJacobianAccumulator ()
68
- return LogPriorWithoutJacobianAccumulator {DynamicPPL.LogProbType} ()
69
- end
70
-
71
- function Base. show (io:: IO , acc:: LogPriorWithoutJacobianAccumulator )
72
- return print (io, " LogPriorWithoutJacobianAccumulator($(repr (acc. logp)) )" )
73
- end
74
-
75
- function DynamicPPL. accumulator_name (:: Type{<:LogPriorWithoutJacobianAccumulator} )
76
- return :LogPriorWithoutJacobian
77
- end
78
-
79
- Base. copy (acc:: LogPriorWithoutJacobianAccumulator ) = acc
80
-
81
- function DynamicPPL. split (:: LogPriorWithoutJacobianAccumulator{T} ) where {T}
82
- return LogPriorWithoutJacobianAccumulator (zero (T))
83
- end
84
-
85
- function DynamicPPL. combine (
86
- acc:: LogPriorWithoutJacobianAccumulator , acc2:: LogPriorWithoutJacobianAccumulator
87
- )
88
- return LogPriorWithoutJacobianAccumulator (acc. logp + acc2. logp)
89
- end
90
-
91
- function Base.:+ (
92
- acc1:: LogPriorWithoutJacobianAccumulator , acc2:: LogPriorWithoutJacobianAccumulator
93
- )
94
- return LogPriorWithoutJacobianAccumulator (acc1. logp + acc2. logp)
95
- end
96
-
97
- function Base. zero (acc:: LogPriorWithoutJacobianAccumulator )
98
- return LogPriorWithoutJacobianAccumulator (zero (acc. logp))
99
- end
100
-
101
- function DynamicPPL. accumulate_assume!! (
102
- acc:: LogPriorWithoutJacobianAccumulator , val, logjac, vn, right
103
- )
104
- return acc + LogPriorWithoutJacobianAccumulator (Distributions. logpdf (right, val))
105
- end
106
- function DynamicPPL. accumulate_observe!! (
107
- acc:: LogPriorWithoutJacobianAccumulator , right, left, vn
108
- )
109
- return acc
110
- end
111
-
112
- function Base. convert (
113
- :: Type{LogPriorWithoutJacobianAccumulator{T}} , acc:: LogPriorWithoutJacobianAccumulator
114
- ) where {T}
115
- return LogPriorWithoutJacobianAccumulator (convert (T, acc. logp))
116
- end
117
-
118
- function DynamicPPL. convert_eltype (
119
- :: Type{T} , acc:: LogPriorWithoutJacobianAccumulator
120
- ) where {T}
121
- return LogPriorWithoutJacobianAccumulator (convert (T, acc. logp))
122
- end
123
-
124
- function getlogprior_without_jacobian (vi:: DynamicPPL.AbstractVarInfo )
125
- acc = DynamicPPL. getacc (vi, Val (:LogPriorWithoutJacobian ))
126
- return acc. logp
127
- end
128
-
129
- function getlogjoint_without_jacobian (vi:: DynamicPPL.AbstractVarInfo )
130
- return getlogprior_without_jacobian (vi) + DynamicPPL. getloglikelihood (vi)
131
- end
132
-
133
- # This is called when constructing a LogDensityFunction, and ensures the VarInfo has the
134
- # right accumulators.
135
- function DynamicPPL. ldf_default_varinfo (
136
- model:: DynamicPPL.Model , :: typeof (getlogprior_without_jacobian)
137
- )
138
- vi = DynamicPPL. VarInfo (model)
139
- vi = DynamicPPL. setaccs!! (vi, (LogPriorWithoutJacobianAccumulator (),))
140
- return vi
141
- end
142
-
143
- function DynamicPPL. ldf_default_varinfo (
144
- model:: DynamicPPL.Model , :: typeof (getlogjoint_without_jacobian)
145
- )
146
- vi = DynamicPPL. VarInfo (model)
147
- vi = DynamicPPL. setaccs!! (
148
- vi, (LogPriorWithoutJacobianAccumulator (), DynamicPPL. LogLikelihoodAccumulator ())
149
- )
150
- return vi
151
- end
152
-
153
46
"""
154
47
OptimLogDensity{
155
48
M<:DynamicPPL.Model,
@@ -628,8 +521,10 @@ function estimate_mode(
628
521
629
522
# Create an OptimLogDensity object that can be used to evaluate the objective function,
630
523
# i.e. the negative log density.
631
- getlogdensity =
632
- estimator isa MAP ? getlogjoint_without_jacobian : DynamicPPL. getloglikelihood
524
+ # Note that we use `getlogjoint` rather than `getlogjoint_internal`: this
525
+ # is intentional, because even though the VarInfo may be linked, the
526
+ # optimisation target should not take the Jacobian term into account.
527
+ getlogdensity = estimator isa MAP ? DynamicPPL. getlogjoint : DynamicPPL. getloglikelihood
633
528
634
529
# Set its VarInfo to the initial parameters.
635
530
# TODO (penelopeysm): Unclear if this is really needed? Any time that logp is calculated
0 commit comments