On Fri, May 27, 2016 at 7:53 PM, Boylan, Ross <[email protected]> wrote:
> A type is defined as
> type Coefficients{T}
> raw::Vector{T}
> σ_0::T
> σ_T::T
> ρ::T
> ν::T
> Σ::Matrix{T}
> # because of this c'tor the type must be mutable
> function Coefficients(vals::Vector{T})
This defines a method for `Coefficients{T}` and needs to be called
with `Coefficients{T}(...)` and not `Coefficients(...)`
Defining a inner constructor turns off the automatically defined
construction (method for `Coefficients`) that automatically deduce the
type parameter so you need to manually define that constructor with
`Coefficients{T}(vals::Vector{T}) = Coefficients{T}(vals)` (note that
the first `{T}` is a parameter for this method of `Coefficients`, the
last `{T}` uses this variable to instantiate `Coefficients` and then
call the `Coefficients{T}` object instead.)
> if length(vals) ≠ 10
> error("Must have exactly 10 parameters")
> end
> self = new(vals)
> self.σ_0 = exp(lnσ_0(self))
> self.σ_T = exp(lnσ_T(self))
> self.ρ = tanh(atanhρ(self))
> self.ν = exp(lnν(self))
> # chol doesn't work on SymTridiagonal
> self.Σ = Matrix(SymTridiagonal([ self.σ_0, self.σ_T].^2, [self.ρ *
> self.σ_0 * self.σ_T]))
> self
> end
> end
>
> But
> julia> @time g = Calculus.gradient(RB.mylike, RB.troubleParamsInit.raw)
> ERROR: MethodError: `convert` has no method matching
> convert(::Type{RB.Coefficients{T}}, ::Array{Float64,1})
> This may have arisen from a call to the constructor RB.Coefficients{T}(...),
> since type constructors fall back to convert methods.
> Closest candidates are:
> call{T}(::Type{T}, ::Any)
> convert{T}(::Type{T}, !Matched::T)
> in logitlite at /home/ross/PCORI/trouble.jl:316
> in mylike at /home/ross/PCORI/trouble.jl:345
> in finite_difference! at
> /home/ross/.julia/v0.4/Calculus/src/finite_difference.jl:126
> in gradient at /home/ross/.julia/v0.4/Calculus/src/derivative.jl:14
>
> The triggering line was
> c = Coefficients(params)
>
> Since Array{Float64,1} = Vector{Float64} I am puzzled that the inner
> constructor isn't called.
> I also tried with the inner constructor defined as
> function Coefficients{T}(vals::Vector{T})
>
> When I call this from my own code things work OK.
>
> Can anyone help me identify the problem, or even the solution :)
>
> Thanks.
> Ross Boylan