@@ -212,13 +212,25 @@ usually it is sufficient to implement `logpdf`.
212
212
See also: [`logpdf`](@ref).
213
213
"""
214
214
@inline function pdf (
215
- d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,N}
216
- ) where {N}
217
- @boundscheck begin
218
- size (x) == size (d) ||
219
- throw (DimensionMismatch (" inconsistent array dimensions" ))
215
+ d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,M}
216
+ ) where {N,M}
217
+ if M == N
218
+ @boundscheck begin
219
+ size (x) == size (d) ||
220
+ throw (DimensionMismatch (" inconsistent array dimensions" ))
221
+ end
222
+ return _pdf (d, x)
223
+ else
224
+ @boundscheck begin
225
+ M > N ||
226
+ throw (DimensionMismatch (
227
+ " number of dimensions of the variates ($M ) must be greater than or equal to the dimension of the distribution ($N )"
228
+ ))
229
+ ntuple (i -> size (x, i), Val (N)) == size (d) ||
230
+ throw (DimensionMismatch (" inconsistent array dimensions" ))
231
+ end
232
+ return @inbounds map (Base. Fix1 (pdf, d), eachvariate (x, variate_form (typeof (d))))
220
233
end
221
- return _pdf (d, x)
222
234
end
223
235
224
236
function _pdf (d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,N} ) where {N}
@@ -241,13 +253,25 @@ size of `x`.
241
253
See also: [`pdf`](@ref).
242
254
"""
243
255
@inline function logpdf (
244
- d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,N}
245
- ) where {N}
246
- @boundscheck begin
247
- size (x) == size (d) ||
248
- throw (DimensionMismatch (" inconsistent array dimensions" ))
256
+ d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,M}
257
+ ) where {N,M}
258
+ if M == N
259
+ @boundscheck begin
260
+ size (x) == size (d) ||
261
+ throw (DimensionMismatch (" inconsistent array dimensions" ))
262
+ end
263
+ return _logpdf (d, x)
264
+ else
265
+ @boundscheck begin
266
+ M > N ||
267
+ throw (DimensionMismatch (
268
+ " number of dimensions of the variates ($M ) must be greater than or equal to the dimension of the distribution ($N )"
269
+ ))
270
+ ntuple (i -> size (x, i), Val (N)) == size (d) ||
271
+ throw (DimensionMismatch (" inconsistent array dimensions" ))
272
+ end
273
+ return @inbounds map (Base. Fix1 (logpdf, d), eachvariate (x, variate_form (typeof (d))))
249
274
end
250
- return _logpdf (d, x)
251
275
end
252
276
253
277
# `_logpdf` should be implemented and has no default definition
@@ -272,20 +296,6 @@ Base.@propagate_inbounds function pdf(
272
296
return map (Base. Fix1 (pdf, d), x)
273
297
end
274
298
275
- @inline function pdf (
276
- d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,M} ,
277
- ) where {N,M}
278
- @boundscheck begin
279
- M > N ||
280
- throw (DimensionMismatch (
281
- " number of dimensions of `x` ($M ) must be greater than number of dimensions of `d` ($N )"
282
- ))
283
- ntuple (i -> size (x, i), Val (N)) == size (d) ||
284
- throw (DimensionMismatch (" inconsistent array dimensions" ))
285
- end
286
- return @inbounds map (Base. Fix1 (pdf, d), eachvariate (x, variate_form (typeof (d))))
287
- end
288
-
289
299
"""
290
300
logpdf(d::Distribution{ArrayLikeVariate{N}}, x) where {N}
291
301
@@ -305,20 +315,6 @@ Base.@propagate_inbounds function logpdf(
305
315
return map (Base. Fix1 (logpdf, d), x)
306
316
end
307
317
308
- @inline function logpdf (
309
- d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,M} ,
310
- ) where {N,M}
311
- @boundscheck begin
312
- M > N ||
313
- throw (DimensionMismatch (
314
- " number of dimensions of `x` ($M ) must be greater than number of dimensions of `d` ($N )"
315
- ))
316
- ntuple (i -> size (x, i), Val (N)) == size (d) ||
317
- throw (DimensionMismatch (" inconsistent array dimensions" ))
318
- end
319
- return @inbounds map (Base. Fix1 (logpdf, d), eachvariate (x, variate_form (typeof (d))))
320
- end
321
-
322
318
"""
323
319
pdf!(out, d::Distribution{ArrayLikeVariate{N}}, x) where {N}
324
320
365
361
@boundscheck begin
366
362
M > N ||
367
363
throw (DimensionMismatch (
368
- " number of dimensions of `x` ($M ) must be greater than number of dimensions of `d` ($N )"
364
+ " number of dimensions of the variates ($M ) must be greater than the dimension of the distribution ($N )"
369
365
))
370
366
ntuple (i -> size (x, i), Val (N)) == size (d) ||
371
367
throw (DimensionMismatch (" inconsistent array dimensions" ))
@@ -414,7 +410,7 @@ See also: [`pdf!`](@ref).
414
410
@boundscheck begin
415
411
M > N ||
416
412
throw (DimensionMismatch (
417
- " number of dimensions of `x` ($M ) must be greater than number of dimensions of `d` ($N )"
413
+ " number of dimensions of the variates ($M ) must be greater than the dimension of the distribution ($N )"
418
414
))
419
415
ntuple (i -> size (x, i), Val (N)) == size (d) ||
420
416
throw (DimensionMismatch (" inconsistent array dimensions" ))
445
441
- an array of dimension `N + 1` with `size(x)[1:N] == size(d)`, or
446
442
- an array of arrays `xi` of dimension `N` with `size(xi) == size(d)`.
447
443
"""
448
- Base. @propagate_inbounds function loglikelihood (
449
- d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,N} ,
450
- ) where {N}
451
- return logpdf (d, x)
452
- end
453
- @inline function loglikelihood (
444
+ Base. @propagate_inbounds @inline function loglikelihood (
454
445
d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:Real,M} ,
455
446
) where {N,M}
456
- @boundscheck begin
457
- M > N ||
458
- throw (DimensionMismatch (
459
- " number of dimensions of `x` ($M ) must be greater than number of dimensions of `d` ($N )"
460
- ))
461
- ntuple (i -> size (x, i), Val (N)) == size (d) ||
462
- throw (DimensionMismatch (" inconsistent array dimensions" ))
447
+ if M == N
448
+ return logpdf (d, x)
449
+ else
450
+ @boundscheck begin
451
+ M > N ||
452
+ throw (DimensionMismatch (
453
+ " number of dimensions of the variates ($M ) must be greater than or equal to the dimension of the distribution ($N )"
454
+ ))
455
+ ntuple (i -> size (x, i), Val (N)) == size (d) ||
456
+ throw (DimensionMismatch (" inconsistent array dimensions" ))
457
+ end
458
+ return @inbounds sum (Base. Fix1 (logpdf, d), eachvariate (x, ArrayLikeVariate{N}))
463
459
end
464
- return @inbounds sum (Base. Fix1 (logpdf, d), eachvariate (x, ArrayLikeVariate{N}))
465
460
end
466
461
Base. @propagate_inbounds function loglikelihood (
467
462
d:: Distribution{ArrayLikeVariate{N}} , x:: AbstractArray{<:AbstractArray{<:Real,N}} ,
0 commit comments