From 0cbb6817ae3c127343b36125114752436c9d532f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=A9tivier?= <46794064+dmetivie@users.noreply.github.com> Date: Tue, 17 May 2022 12:39:31 +0200 Subject: [PATCH 1/4] subtype and output type --- src/hmm.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/hmm.jl b/src/hmm.jl index 2583b68..ae52672 100644 --- a/src/hmm.jl +++ b/src/hmm.jl @@ -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 @@ -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{F}` (for `Univariate{F}` HMMs): observations (`T`). +- `Matrix{F}` (for `Multivariate{F}` HMMs): observations (`T x dim(obs)`). **Examples** ```julia @@ -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) @@ -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{F}` (for `Univariate{F}` HMMs): observations (`T`). +- `Matrix{F}` (for `Multivariate{F}` HMMs): observations (`T x dim(obs)`). **Example** ```julia @@ -158,8 +158,8 @@ hmm = HMM([0.9 0.1; 0.1 0.9], [Normal(0,1), Normal(10,1)]) y = rand(hmm, [1, 1, 2, 2, 1]) ``` """ -function rand(rng::AbstractRNG, hmm::AbstractHMM{Univariate}, z::AbstractVector{<:Integer}) - y = Vector{Float64}(undef, length(z)) +function rand(rng::AbstractRNG, hmm::AbstractHMM{Univariate,T}, z::AbstractVector{<:Integer}) where {T} + y = Vector{T}(undef, length(z)) for t in eachindex(z) y[t] = rand(rng, hmm.B[z[t]]) end @@ -168,10 +168,10 @@ end function rand( rng::AbstractRNG, - hmm::AbstractHMM{Multivariate}, + hmm::AbstractHMM{Multivariate,T}, z::AbstractVector{<:Integer}, -) - y = Matrix{Float64}(undef, length(z), size(hmm, 2)) +) where {T} + y = Matrix{T}(undef, length(z), size(hmm, 2)) for t in eachindex(z) y[t, :] = rand(rng, hmm.B[z[t]]) end @@ -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] """ From a1a732e5bd4b77bea1bb71d7e4ad32e5b77d4581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=A9tivier?= <46794064+dmetivie@users.noreply.github.com> Date: Tue, 17 May 2022 13:05:24 +0200 Subject: [PATCH 2/4] eltype of hmm.B --- src/hmm.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hmm.jl b/src/hmm.jl index ae52672..ea13cd6 100644 --- a/src/hmm.jl +++ b/src/hmm.jl @@ -158,8 +158,8 @@ hmm = HMM([0.9 0.1; 0.1 0.9], [Normal(0,1), Normal(10,1)]) y = rand(hmm, [1, 1, 2, 2, 1]) ``` """ -function rand(rng::AbstractRNG, hmm::AbstractHMM{Univariate,T}, z::AbstractVector{<:Integer}) where {T} - y = Vector{T}(undef, length(z)) +function rand(rng::AbstractRNG, hmm::AbstractHMM{Univariate}, z::AbstractVector{<:Integer}) + y = Vector{eltype(eltype(hmm.B))}(undef, length(z)) for t in eachindex(z) y[t] = rand(rng, hmm.B[z[t]]) end @@ -168,10 +168,10 @@ end function rand( rng::AbstractRNG, - hmm::AbstractHMM{Multivariate,T}, + hmm::AbstractHMM{Multivariate}, z::AbstractVector{<:Integer}, -) where {T} - y = Matrix{T}(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 From 0bfb355650b5569458df7430cd9791277aa68968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=A9tivier?= <46794064+dmetivie@users.noreply.github.com> Date: Tue, 17 May 2022 13:12:35 +0200 Subject: [PATCH 3/4] format (with vsCode formater) --- src/hmm.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hmm.jl b/src/hmm.jl index ea13cd6..6f6ee89 100644 --- a/src/hmm.jl +++ b/src/hmm.jl @@ -170,7 +170,7 @@ function rand( rng::AbstractRNG, hmm::AbstractHMM{Multivariate}, z::AbstractVector{<:Integer}, -) +) 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]]) From e65510335354be7637a1598a79f02788b718dd1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=A9tivier?= <46794064+dmetivie@users.noreply.github.com> Date: Tue, 17 May 2022 13:27:31 +0200 Subject: [PATCH 4/4] type in comments --- src/hmm.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hmm.jl b/src/hmm.jl index 6f6ee89..24b7f93 100644 --- a/src/hmm.jl +++ b/src/hmm.jl @@ -106,8 +106,8 @@ Sample a trajectory of `T` timesteps from `hmm`. **Output** - `Vector{Int}` (if `seq == true`): hidden state sequence. -- `Vector{F}` (for `Univariate{F}` HMMs): observations (`T`). -- `Matrix{F}` (for `Multivariate{F}` HMMs): observations (`T x dim(obs)`). +- `Vector` (for `Univariate` HMMs): observations (`T`). +- `Matrix` (for `Multivariate` HMMs): observations (`T x dim(obs)`). **Examples** ```julia @@ -148,8 +148,8 @@ end Sample observations from `hmm` according to trajectory `z`. **Output** -- `Vector{F}` (for `Univariate{F}` HMMs): observations (`T`). -- `Matrix{F}` (for `Multivariate{F}` HMMs): observations (`T x dim(obs)`). +- `Vector` (for `Univariate` HMMs): observations (`T`). +- `Matrix` (for `Multivariate` HMMs): observations (`T x dim(obs)`). **Example** ```julia