Skip to content

Commit f9cd7f9

Browse files
author
Alexey Stukalov
committed
simulation: update rand()
1 parent ef589d4 commit f9cd7f9

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

src/additional_functions/simulation.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
# simulate data
33
############################################################################################
44
"""
5-
rand(sem::Union{Sem, SemLoss, SemImplied}, params, n)
6-
rand(sem::Union{Sem, SemLoss, SemImplied}, n)
5+
rand(sem::Union{Sem, SemLoss, SemImplied}, [params], n)
76
87
Sample from the multivariate normal distribution implied by the SEM model.
98
109
# Arguments
1110
- `sem`: SEM model to use. Ensemble models with multiple SEM terms are not supported.
12-
- `params`: SEM model parameters to simulate from.
11+
- `params`: optional SEM model parameters to simulate from, otherwise uses the
12+
current state of implied covariances and means.
1313
- `n::Integer`: Number of samples to draw.
1414
1515
# Examples
@@ -18,14 +18,10 @@ rand(model, start_simple(model), 100)
1818
```
1919
"""
2020
function Distributions.rand(implied::SemImplied, params, n::Integer)
21-
update!(EvaluationTargets{true, false, false}(), implied, params)
22-
return rand(implied, n)
23-
end
24-
25-
Distributions.rand(implied::SemImplied, n::Integer) =
26-
error("rand($(typeof(implied)), n) is not implemented")
27-
28-
function Distributions.rand(implied::Union{RAM, RAMSymbolic}, n::Integer)
21+
if !isnothing(params)
22+
# update the implied covariances with the new model params
23+
update!(EvaluationTargets{true, false, false}(), implied, params)
24+
end
2925
Σ = Symmetric(implied.Σ)
3026
if MeanStruct(implied) === NoMeanStruct
3127
return permutedims(rand(MvNormal(Σ), n))
@@ -36,8 +32,8 @@ end
3632

3733
Distributions.rand(loss::SemLoss, params, n::Integer) = rand(SEM.implied(loss), params, n)
3834

39-
Distributions.rand(loss::SemLoss, n::Integer) = rand(SEM.implied(loss), n)
40-
4135
Distributions.rand(model::Sem, params, n::Integer) = rand(sem_term(model), params, n)
4236

43-
Distributions.rand(model::Sem, n::Integer) = rand(sem_term(model), n)
37+
# rand() overloads without SEM params
38+
Distributions.rand(implied::Union{SemImplied, SemLoss, Sem}, n::Integer) =
39+
Distributions.rand(implied, nothing, n)

0 commit comments

Comments
 (0)