Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Subtype of hmm.B and automatic type for outputs #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/hmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ hmm = HMM([0.9 0.1; 0.1 0.9], [0. 0.5 0.5; 0.25 0.25 0.5])
struct HMM{F,T} <: AbstractHMM{F}
a::Vector{T}
A::Matrix{T}
B::Vector{Distribution{F}}
B::Vector{<:Distribution{F}}
HMM{F,T}(a, A, B) where {F,T} = assert_hmm(a, A, B) && new(a, A, B)
end

Expand Down Expand Up @@ -106,8 +106,8 @@ Sample a trajectory of `T` timesteps from `hmm`.

**Output**
- `Vector{Int}` (if `seq == true`): hidden state sequence.
- `Vector{Float64}` (for `Univariate` HMMs): observations (`T`).
- `Matrix{Float64}` (for `Multivariate` HMMs): observations (`T x dim(obs)`).
- `Vector` (for `Univariate` HMMs): observations (`T`).
- `Matrix` (for `Multivariate` HMMs): observations (`T x dim(obs)`).

**Examples**
```julia
Expand All @@ -130,8 +130,8 @@ function rand(
rng::AbstractRNG,
hmm::AbstractHMM,
T::Integer;
init = rand(rng, Categorical(hmm.a)),
seq = false,
init=rand(rng, Categorical(hmm.a)),
seq=false
)
z = Vector{Int}(undef, T)
(T >= 1) && (z[1] = init)
Expand All @@ -148,8 +148,8 @@ end
Sample observations from `hmm` according to trajectory `z`.

**Output**
- `Vector{Float64}` (for `Univariate` HMMs): observations (`T`).
- `Matrix{Float64}` (for `Multivariate` HMMs): observations (`T x dim(obs)`).
- `Vector` (for `Univariate` HMMs): observations (`T`).
- `Matrix` (for `Multivariate` HMMs): observations (`T x dim(obs)`).

**Example**
```julia
Expand All @@ -159,7 +159,7 @@ y = rand(hmm, [1, 1, 2, 2, 1])
```
"""
function rand(rng::AbstractRNG, hmm::AbstractHMM{Univariate}, z::AbstractVector{<:Integer})
y = Vector{Float64}(undef, length(z))
y = Vector{eltype(eltype(hmm.B))}(undef, length(z))
for t in eachindex(z)
y[t] = rand(rng, hmm.B[z[t]])
end
Expand All @@ -171,7 +171,7 @@ function rand(
hmm::AbstractHMM{Multivariate},
z::AbstractVector{<:Integer},
)
y = Matrix{Float64}(undef, length(z), size(hmm, 2))
y = Matrix{eltype(eltype(hmm.B))}(undef, length(z), size(hmm, 2))
for t in eachindex(z)
y[t, :] = rand(rng, hmm.B[z[t]])
end
Expand All @@ -196,7 +196,7 @@ size(hmm)
(2, 1)
```
"""
size(hmm::AbstractHMM, dim = :) = (length(hmm.B), length(hmm.B[1]))[dim]
size(hmm::AbstractHMM, dim=:) = (length(hmm.B), length(hmm.B[1]))[dim]

"""

Expand Down