# Compiler doesn't know the type of real.(vec) in compile time

**URL:** <https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336>\
**Category:** Performance\
**Tags:** question\
**Created:** [January 27, 2024, 11:02am UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336 "2024-01-27T11:02:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 27, 2024, 11:02am UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/1 "2024-01-27T11:02:57Z")

</div>

How to make the `cc` function type stable?

```julia
function cc(
  model::Type{<:CepstralCoeffModel},
  tseries::AbstractVector,
  p::Integer,
  n::Integer;
  normalize::Bool=false
)
  series = tseries
  normalize && begin
    series = copy(tseries)
    normalizer!(series)
  end
  α = fit_arima(series, p)
  coefs = cepscoef(model, α, p, n)
  return real.(coefs)
end

function cepscoef(::Type{RealCepstral}, series::AbstractVector, p::Integer, n::Int)
  p≥n || ArgumentError("`p` must be equal to or greater than `n` when using `RealCepstral`. \
  Passed $p and $n.") |> throw
  res = series |> fft .|> abs .|> log |> ifft
  return res[1:n]
end

julia> using YFinance
julia> series2019 = get_prices("INTC", startdt="2019-10-29", enddt="2020-01-28")["adjclose"];

julia> @code_warntype cc(RealCepstral, series2019, 5, 5)
MethodInstance for Main.CepstralClustering.cc(::Type{RealCepstral}, ::Vector{Float64}, ::Int64, ::Int64)
  from cc(model::Type{<:Main.CepstralClustering.CepstralCoeffModel}, tseries::AbstractVector, p::Integer, n::Integer; normalize) @ Main.CepstralClustering e:\Julia Forks\TimeSeries-Cepstral-Clustering\src\CepstralClustering.jl:53
Arguments
  #self#::Core.Const(Main.CepstralClustering.cc)
  model::Core.Const(RealCepstral)
  tseries::Vector{Float64}       
  p::Int64
  n::Int64
Body::Any
1 ─ %1 = Main.CepstralClustering.:(var"#cc#4")(false, #self#, model, tseries, p, n)::Any
└── return %1

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [January 27, 2024, 11:10am UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/2 "2024-01-27T11:10:23Z")

</div>

[GitHub - JuliaDebug/Cthulhu.jl: The slow descent into madness](https://github.com/JuliaDebug/Cthulhu.jl) holds the answer

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 27, 2024, 12:06pm UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/3 "2024-01-27T12:06:55Z")

</div>

You didn’t provide a reproducer, but maybe try making `model` a type parameter of the method.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 27, 2024, 12:46pm UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/4 "2024-01-27T12:46:22Z")

</div>

Full code:

```julia
using FFTW, ARCHModels
abstract type CepstralCoeffModel end
struct RealCepstral <: CepstralCoeffModel end

function cc(
  model::Type{<:CepstralCoeffModel},
  tseries::AbstractVector,
  p::Integer,
  n::Integer
)
  series = tseries
  normalize && begin
    series = copy(tseries)
    normalizer!(series)
  end
  α = fit_arima(series, p)
  coefs = cepscoef(model, α, p, n)
  return real.(coefs)
end

normalizer!(series::AbstractVector) = series .= (series .- mean(series)) ./ std(series)

function fit_arima(series::AbstractVector, p::Integer)
  model = fit(ARMA{p, 0}, series)
  return model.meanspec.coefs[2:end]
end

function cepscoef(::Type{RealCepstral}, series::AbstractVector, p::Integer, n::Int)
  p≥n || ArgumentError("`p` must be equal to or greater than `n` when using `RealCepstral`. \
  Passed $p and $n.") |> throw
  res = series |> fft .|> abs .|> log |> ifft
  return res[1:n]
end

```

Sorry for any inconvenience, @nsajko, @cjdoris.  
The complete script is provided above.

> [@cjdoris](#):
>
> [GitHub - JuliaDebug/Cthulhu.jl: The slow descent into madness](https://github.com/JuliaDebug/Cthulhu.jl) holds the answer

Thank you for the great advice. I found out there is an internal problem with ARCHModels.jl.

The root of the problem derives from `model = fit(ARMA{p, 0}, series)` in my code. The `Cthulhu` shows:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/3/e38b8b7a36048f4289d1bf821574f83196473d9a.png)

Which is related to this line of code:

> <https://github.com/s-broda/ARCHModels.jl/blob/05209135786e94b3602e3b38174434689bf2d696/src/univariatearchmodel.jl#L438>

According to Cthulhu, another problem arises from here:

> <https://github.com/s-broda/ARCHModels.jl/blob/05209135786e94b3602e3b38174434689bf2d696/src/TGARCH.jl#L120>

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6ae67d4a29e0aff89448572e51e7c0a536f232f1.png)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 27, 2024, 1:20pm UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/5 "2024-01-27T13:20:22Z")

</div>

> [@Shayan](#):
>
> ```julia
> normalize && begin
> series = copy(tseries)
> normalizer!(series)
> end
> 
> ```

Sorry for going off topic, but for cases like the above, there’s the `if`-construct, which is more idiomatic, and also briefer:

```julia
if normalize
    series = copy(tseries)
    normalizer!(series)
end

```

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 27, 2024, 1:28pm UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/6 "2024-01-27T13:28:35Z")

</div>

> [@DNF](#):
>
> Sorry for going off topic, but for cases like the above

Oh, come on! Any time!! Thank you so much!!

> [@DNF](#):
>
> there’s the `if`-construct, which is more idiomatic

Yes, you’re right! I barely use `begin` in my deployed codes. Thank you for your reminder. 🌹

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [January 27, 2024, 2:03pm UTC](https://discourse.julialang.org/t/compiler-doesnt-know-the-type-of-real-vec-in-compile-time/109336/7 "2024-01-27T14:03:04Z")

</div>

> [@Shayan](#):
>
> `@code_warntype cc(RealCepstral, series2019, 5, 5)`

The problem that I mentioned in the original post:

> [@Shayan](#):
>
> ```julia
> julia> @code_warntype cc(RealCepstral, series2019, 5, 5)
> MethodInstance for Main.CepstralClustering.cc(::Type{RealCepstral}, ::Vector{Float64}, ::Int64, ::Int64)
> from cc(model::Type{<:Main.CepstralClustering.CepstralCoeffModel}, tseries::AbstractVector, p::Integer, n::Integer; normalize) @ Main.CepstralClustering e:\Julia Forks\TimeSeries-Cepstral-Clustering\src\CepstralClustering.jl:53
> Arguments
> #self#::Core.Const(Main.CepstralClustering.cc)
> model::Core.Const(RealCepstral)
> tseries::Vector{Float64}       
> p::Int64
> n::Int64
> Body::Any
> 1 ─ %1 = Main.CepstralClustering.:(var"#cc#4")(false, #self#, model, tseries, p, n)::Any
> └── return %1
> 
> ```

Handled by using type assertions in the definition of the `fit_arima` function. The former version of the function:

> [@Shayan](#):
>
> ```julia
> function fit_arima(series::AbstractVector, p::Integer)
> model = fit(ARMA{p, 0}, series)
> return model.meanspec.coefs[2:end]
> end
> 
> ```

The new version:

```julia
function fit_arima(series::AbstractVector{T}, p::Integer) where T
  model = fit(ARMA{p, 0}, series)
  return model.meanspec.coefs[2:end]::Vector{T}
end

```

And the problem gets solved. Credits belong to my friend, @eliascarv. Thank you so much 🌹

P.S.: The problem with the type instability of the ARCHModels.jl still exists. There are several technical issues with the source code that I will hopefully manage to fix through some PRs.
