Commit a2f4bcfc authored by Antonino's avatar Antonino

bug fixes in fit routine

parent 5f3ab2cc
""" """
plat_av(obs::Vector{uwreal}, plat::Vector{Int64}, [W::VecOrMat{Float64},] wpm::Union{Dict{Int64},Vector{Float{64}},Dict{String,Vector{Float{64}},Nothing}=nothing} ) plat_av(obs::Vector{uwreal}, plat::Vector{Int64}, [W::VecOrMat{Float64},] wpm::Union{Dict{Int64},Vector{Float{64}},Dict{String,Vector{Float{64}},Nothing}=nothing} )
...@@ -159,7 +158,7 @@ function fit_routine(model::Function, ...@@ -159,7 +158,7 @@ function fit_routine(model::Function,
[uwerr(y, wpm) for y in ydata] [uwerr(y, wpm) for y in ydata]
if length(W) ==0 if length(W) ==0
if corr if corr
C = length(C) ==0 ? GammaMethod.cov_AD(ydata,wpm) : C C = length(C) ==0 ? GammaMethod.cov_AD(ydata) : C
W = LinearAlgebra.pinv(C); W = LinearAlgebra.pinv(C);
W = (W'+W)*0.5 W = (W'+W)*0.5
else else
...@@ -282,36 +281,36 @@ It returns a `NamedTuple` with names: ...@@ -282,36 +281,36 @@ It returns a `NamedTuple` with names:
- `:chiexp`: chisquare expected - `:chiexp`: chisquare expected
- `:pval`: pvalue - `:pval`: pvalue
""" """
function fit_routines(models::AbstractArray{Function}, function fit_routine(models::AbstractArray{Function},
xdata::AbstractArray{<:AbstractArray}, xdata::AbstractArray{<:AbstractArray},
ydata::AbstractArray{<:AbstractArray{ADerrors.uwreal}}, ydata::AbstractArray{<:AbstractArray{ADerrors.uwreal}},
npar::Int64; npar::Int64;
W::AbstractArray = Float64[], W = Float64[],
C::AbstractArray = Float64[], C = Float64[],
wpm::AbstractDict = Dict{Int64,Vector{Float64}}(), wpm::AbstractDict = Dict{Int64,Vector{Float64}}(),
corr::Bool = true, corr::Bool = true,
guess::AbstractArray = fill(0.5,npar), guess::AbstractArray = fill(0.5,npar),
logfile = nothing, logfile = nothing,
) )
Nmodel = length(models) Nmodel = length(models)
ndata = length.(xdata)
Ntotal = sum(ndata);
if Nmodel != length(xdata) != length(ydata) if Nmodel != length(xdata) != length(ydata)
error("[juobs] Error: you need the same number of model, xdata and ydata") error("[juobs] Error: you need the same number of model, xdata and ydata")
end end
if !(length(W) == 0 || length(W)==Nmodel) if !(length(W) == 0 || length(W)==Nmodel || length(W) == Ntotal^2)
error("[juobs] Error: You need to pass a W matrix for model or none") error("[juobs] Error: You need to pass a W matrix for model, for all the data or none")
end end
if !(length(C) == 0 || length(C)==Nmodel) if !(length(C) == 0 || length(C)==Nmodel || length(C) == Ntotal^2)
error("[juobs] Error: You need to pass a C matrix for model or none") error("[juobs] Error: You need to pass a C matrix for model, for all the data or none")
end end
ndata = length.(xdata)
if !all(length.(ydata).== ndata) if !all(length.(ydata).== ndata)
error("[juobs] Error: Mismatch in xdata and ydata. Make sure that the data corresponds") error("[juobs] Error: Mismatch in xdata and ydata. Make sure that the data corresponds")
end end
Ntotal = sum(ndata);
X = vcat(xdata...) X = vcat(xdata...)
Y = vcat(ydata...) Y = vcat(ydata...)
function make_matrix(M) function make_matrix(M::Vector{<:AbstractMatrix})
aux = zeros(Ntotal,Ntotal) aux = zeros(Ntotal,Ntotal)
ie = 0 ie = 0
for m in 1:Nmodel for m in 1:Nmodel
...@@ -321,6 +320,7 @@ function fit_routines(models::AbstractArray{Function}, ...@@ -321,6 +320,7 @@ function fit_routines(models::AbstractArray{Function},
end end
return aux return aux
end end
make_matrix(M::T where {T<:AbstractMatrix}) = M
WW = length(W)==0 ? W : make_matrix(W); WW = length(W)==0 ? W : make_matrix(W);
CC = length(C)==0 ? C : make_matrix(C); CC = length(C)==0 ? C : make_matrix(C);
......
#= #=
using ForwardDiff, LinearAlgebra using ForwardDiff, LinearAlgebra
for op in (:eigvals, :eigvecs) for op in (:eigvals, :eigvecs)
@eval function LinearAlgebra.$op(a::Matrix{uwreal}) @eval function LinearAlgebra.$op(a::Matrix{uwreal})
function fvec(x::Matrix) function fvec(x::Matrix)
return LinearAlgebra.$op(x) return LinearAlgebra.$op(x)
end end
return fvec(value.(a)) return fvec(value.(a))
#return uwreal(LinearAlgebra.$op(a.mean), a.prop, ForwardDiff.derivative($op, a.mean)*a.der) #return uwreal(LinearAlgebra.$op(a.mean), a.prop, ForwardDiff.derivative($op, a.mean)*a.der)
end end
end end
=# =#
...@@ -893,14 +893,9 @@ Return the square absolute value. ...@@ -893,14 +893,9 @@ Return the square absolute value.
Previously this function returned the absolute value of `uw`, not the square absolute value. Previously this function returned the absolute value of `uw`, not the square absolute value.
The function was changed to make it consistent with `Base.abs2()` in Julia. The function was changed to make it consistent with `Base.abs2()` in Julia.
When used, a warning is printed to informe of this change future users, feel free to remove the warning
from your branch!
""" """
function Base.abs2(uw::uwreal) Base.abs2(uw::uwreal) = uw^2
@warn "This function was updated by Antonino to make it consisted with the Julia version of it.
Check the documentation for info and then feel free to comment out this warning in your branch"
return uw^2
end
""" """
Base.abs(uw::uwreal) Base.abs(uw::uwreal)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment