@@ -24,25 +24,6 @@ a vector of length `dim(d)` or a matrix with `dim(d)` rows.
2424"""
2525rand! (rng:: AbstractRNG , d:: MultivariateDistribution , x:: AbstractArray )
2626
27- # multivariate with pre-allocated array
28- function _rand! (rng:: AbstractRNG , s:: Sampleable{Multivariate} , m:: AbstractMatrix )
29- @boundscheck size (m, 1 ) == length (s) ||
30- throw (DimensionMismatch (" Output size inconsistent with sample length." ))
31- smp = sampler (s)
32- for i in Base. OneTo (size (m,2 ))
33- _rand! (rng, smp, view (m,:,i))
34- end
35- return m
36- end
37-
38- # single multivariate with pre-allocated vector
39- function rand! (rng:: AbstractRNG , s:: Sampleable{Multivariate} ,
40- v:: AbstractVector{<:Real} )
41- @boundscheck length (v) == length (s) ||
42- throw (DimensionMismatch (" Output size inconsistent with sample length." ))
43- _rand! (rng, s, v)
44- end
45-
4627# multiple multivariates with pre-allocated array of maybe pre-allocated vectors
4728rand! (rng:: AbstractRNG , s:: Sampleable{Multivariate} ,
4829 X:: AbstractArray{<:AbstractVector} ) =
@@ -65,22 +46,11 @@ function rand!(rng::AbstractRNG, s::Sampleable{Multivariate},
6546 return X
6647end
6748
68- # multiple multivariate, must allocate matrix or array of vectors
69- rand (s:: Sampleable{Multivariate} , n:: Int ) = rand (GLOBAL_RNG, s, n)
49+ # multiple multivariate, must allocate matrix
7050rand (rng:: AbstractRNG , s:: Sampleable{Multivariate} , n:: Int ) =
71- _rand ! (rng, s , Matrix {eltype(s)} (undef, length (s), n))
51+ @inbounds rand ! (rng, sampler (s) , Matrix {eltype(s)} (undef, length (s), n))
7252rand (rng:: AbstractRNG , s:: Sampleable{Multivariate,Continuous} , n:: Int ) =
73- _rand! (rng, s, Matrix {float(eltype(s))} (undef, length (s), n))
74- rand (rng:: AbstractRNG , s:: Sampleable{Multivariate} , dims:: Dims ) =
75- rand! (rng, s, Array {Vector{eltype(s)}} (undef, dims), true )
76- rand (rng:: AbstractRNG , s:: Sampleable{Multivariate,Continuous} , dims:: Dims ) =
77- rand! (rng, s, Array {Vector{float(eltype(s))}} (undef, dims), true )
78-
79- # single multivariate, must allocate vector
80- rand (rng:: AbstractRNG , s:: Sampleable{Multivariate} ) =
81- _rand! (rng, s, Vector {eltype(s)} (undef, length (s)))
82- rand (rng:: AbstractRNG , s:: Sampleable{Multivariate,Continuous} ) =
83- _rand! (rng, s, Vector {float(eltype(s))} (undef, length (s)))
53+ @inbounds rand! (rng, sampler (s), Matrix {float(eltype(s))} (undef, length (s), n))
8454
8555# # domain
8656
@@ -193,83 +163,6 @@ Return the logarithm of probability density evaluated at `x`.
193163"""
194164logpdf (d:: MultivariateDistribution , x:: AbstractArray )
195165
196- _pdf (d:: MultivariateDistribution , X:: AbstractVector ) = exp (_logpdf (d, X))
197-
198- function logpdf (d:: MultivariateDistribution , X:: AbstractVector )
199- length (X) == length (d) ||
200- throw (DimensionMismatch (" Inconsistent array dimensions." ))
201- _logpdf (d, X)
202- end
203-
204- function pdf (d:: MultivariateDistribution , X:: AbstractVector )
205- length (X) == length (d) ||
206- throw (DimensionMismatch (" Inconsistent array dimensions." ))
207- _pdf (d, X)
208- end
209-
210- function _logpdf! (r:: AbstractArray , d:: MultivariateDistribution , X:: AbstractMatrix )
211- for i in 1 : size (X,2 )
212- @inbounds r[i] = logpdf (d, view (X,:,i))
213- end
214- return r
215- end
216-
217- function _pdf! (r:: AbstractArray , d:: MultivariateDistribution , X:: AbstractMatrix )
218- for i in 1 : size (X,2 )
219- @inbounds r[i] = pdf (d, view (X,:,i))
220- end
221- return r
222- end
223-
224- function logpdf! (r:: AbstractArray , d:: MultivariateDistribution , X:: AbstractMatrix )
225- size (X) == (length (d), length (r)) ||
226- throw (DimensionMismatch (" Inconsistent array dimensions." ))
227- _logpdf! (r, d, X)
228- end
229-
230- function pdf! (r:: AbstractArray , d:: MultivariateDistribution , X:: AbstractMatrix )
231- size (X) == (length (d), length (r)) ||
232- throw (DimensionMismatch (" Inconsistent array dimensions." ))
233- _pdf! (r, d, X)
234- end
235-
236- function logpdf (d:: MultivariateDistribution , X:: AbstractMatrix )
237- size (X, 1 ) == length (d) ||
238- throw (DimensionMismatch (" Inconsistent array dimensions." ))
239- map (i -> _logpdf (d, view (X, :, i)), axes (X, 2 ))
240- end
241-
242- function pdf (d:: MultivariateDistribution , X:: AbstractMatrix )
243- size (X, 1 ) == length (d) ||
244- throw (DimensionMismatch (" Inconsistent array dimensions." ))
245- map (i -> _pdf (d, view (X, :, i)), axes (X, 2 ))
246- end
247-
248- """
249- _logpdf{T<:Real}(d::MultivariateDistribution, x::AbstractArray)
250-
251- Evaluate logarithm of pdf value for a given vector `x`. This function need not perform dimension checking.
252- Generally, one does not need to implement `pdf` (or `_pdf`) as fallback methods are provided in `src/multivariates.jl`.
253- """
254- _logpdf (d:: MultivariateDistribution , x:: AbstractArray )
255-
256- """
257- loglikelihood(d::MultivariateDistribution, x::AbstractArray)
258-
259- The log-likelihood of distribution `d` with respect to all samples contained in array `x`.
260-
261- Here, `x` can be a vector of length `dim(d)`, a matrix with `dim(d)` rows, or an array of
262- vectors of length `dim(d)`.
263- """
264- loglikelihood (d:: MultivariateDistribution , X:: AbstractVector{<:Real} ) = logpdf (d, X)
265- function loglikelihood (d:: MultivariateDistribution , X:: AbstractMatrix{<:Real} )
266- size (X, 1 ) == length (d) || throw (DimensionMismatch (" Inconsistent array dimensions." ))
267- return sum (i -> _logpdf (d, view (X, :, i)), 1 : size (X, 2 ))
268- end
269- function loglikelihood (d:: MultivariateDistribution , X:: AbstractArray{<:AbstractVector} )
270- return sum (x -> logpdf (d, x), X)
271- end
272-
273166# #### Specific distributions #####
274167
275168for fname in [" dirichlet.jl" ,
0 commit comments