Commit 02d294c3 authored by Antonino D'Anna's avatar Antonino D'Anna

pvalue now has a keyword option C::AbstractMatrix{Float64} defaulted to ADerrors.cov(data,wmp)

parent 0a7d3d60
...@@ -1235,7 +1235,7 @@ function get_chi(model::Function,x::Vector,par,data,W::Vector{Float64}) ...@@ -1235,7 +1235,7 @@ function get_chi(model::Function,x::Vector,par,data,W::Vector{Float64})
end end
@doc raw""" @doc raw"""
fit_routine(model::Function, xdata::Array{<:Real}, ydata::Array{uwreal}, npar::Int64; wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}}=Dict{Int64,Vector{Float64}}(),corr::Bool = true, W::VecOrMat{Float64}=Vector{Float64}(),guess::Vector{Float64} = fill(0.5,npar)); fit_routine(model::Function, xdata::Array{<:Real}, ydata::Array{uwreal}, npar::Int64; wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}}=Dict{Int64,Vector{Float64}}(),corr::Bool = true, W::VecOrMat{Float64}=Vector{Float64}(),guess::Vector{Float64} = fill(0.5,npar),logfile);
It executes a fit of the `ydata` with the fit function `model`. It executes a fit of the `ydata` with the fit function `model`.
...@@ -1246,11 +1246,13 @@ It executes a fit of the `ydata` with the fit function `model`. ...@@ -1246,11 +1246,13 @@ It executes a fit of the `ydata` with the fit function `model`.
- `W::VecOrMat{Float64}`: Weight matrix/vector. When given, the flag `corr` is overridden. If `W` is a `Vector` the fit is perfomed uncorrelated,otherwise is correlated. - `W::VecOrMat{Float64}`: Weight matrix/vector. When given, the flag `corr` is overridden. If `W` is a `Vector` the fit is perfomed uncorrelated,otherwise is correlated.
- `guess::Vector{Float64}`: Initial guess used in the fit routine. - `guess::Vector{Float64}`: Initial guess used in the fit routine.
- `logfile`: handle to a logfile. If `nothing`, no log will be written. It can be `IO` file or a custom log structure that has an overloading for `print()` and `println()`. - `logfile`: handle to a logfile. If `nothing`, no log will be written. It can be `IO` file or a custom log structure that has an overloading for `print()` and `println()`.
# Returns # Returns
It returns a NamedTuple with names: It returns a NamedTuple with names:
- `:par`: fit parameter as `Vector{uwreal}` - `:par`: fit parameter as `Vector{uwreal}`
- `:chi2`: chisquare, - `:chi2`: chisquare,
- `:chiexp`: chisquare expected - `:chiexp`: chisquare expected
- `:pval`: pvalue - `:pval`: pvalue
""" """
function fit_routine(model::Function, function fit_routine(model::Function,
...@@ -1261,7 +1263,7 @@ function fit_routine(model::Function, ...@@ -1261,7 +1263,7 @@ function fit_routine(model::Function,
corr::Bool = true, corr::Bool = true,
W::VecOrMat{Float64}=Vector{Float64}(), W::VecOrMat{Float64}=Vector{Float64}(),
guess::Vector{Float64} = fill(0.5,npar), guess::Vector{Float64} = fill(0.5,npar),
logfile = nothing); logfile = nothing,);
nobs = length(ydata) nobs = length(ydata)
if length(xdata)!=nobs if length(xdata)!=nobs
...@@ -1271,7 +1273,7 @@ function fit_routine(model::Function, ...@@ -1271,7 +1273,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
W = ADerrors.cov(ydata,wpm) C = ADerrors.cov(ydata,wpm)
W = LinearAlgebra.pinv(W); W = LinearAlgebra.pinv(W);
W = (W'+W)*0.5 W = (W'+W)*0.5
else else
...@@ -1283,21 +1285,26 @@ function fit_routine(model::Function, ...@@ -1283,21 +1285,26 @@ function fit_routine(model::Function,
chisq(p,d) = get_chi(model,xdata,p,d,W) chisq(p,d) = get_chi(model,xdata,p,d,W)
chi2 = sum(fit.resid.^2) chi2 = sum(fit.resid.^2)
par,chiexp = fit_error(chisq,LsqFit.coef(fit),ydata,wpm,W) par = fit_error(chisq,LsqFit.coef(fit),ydata,wpm,W,false)
chiexp = ADerrors.chiexp(chisq,LsqFit.coef(fit),ydata,wpm,W=W)
pval = juobs.pvalue(chisq,chi2,LsqFit.coef(fit),ydata,W) pval = juobs.pvalue(chisq,chi2,LsqFit.coef(fit),ydata,W)
if !isnothing(logfile) if !isnothing(logfile)
println(logfile, "Fit routine in [$(xdata[1]),...,$(xdata[2])]")
println(logifle, "parameters :", par...) println(logfile, "juobs.fit_routine output:")
println(logfile, "chi2: ", chi2) println(logfile, "\t\tFit routine in [$(xdata[1]),...,$(xdata[2])]")
println(logfile, "chiexp: ", chiexp) println(logfile, "\t\tparameters :", par...)
println(logfile, "pvalue: ", pval) println(logfile, "\t\tchi2: ", chi2)
println(logfile, "\t\tchiexp: ", chiexp)
println(logfile, "\t\tpvalue: ", pval)
end end
return (par=par,chi2=chi2,chiexp=chiexp,pval=pval) return (par=par,chi2=chi2,chiexp=chiexp,pval=pval)
end end
function fit_routine(model::Vector{Function}, xdata::Vector{Array{Float64, N}} where N, ydata::Vector{Array{uwreal, N}} where N, param::Int64; wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing}=nothing, #= function fit_routine(model::Vector{Function}, xdata::Vector{Array{Float64, N}} where N, ydata::Vector{Array{uwreal, N}} where N, param::Int64; wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing}=nothing,
correlated_fit::Bool=false) correlated_fit::Bool=false)
if !(length(model) == length(xdata) == length(ydata)) if !(length(model) == length(xdata) == length(ydata))
...@@ -1800,7 +1807,7 @@ function inv_covar_multi_id(obs::Vector{uwreal}; wpm::Union{Dict{Int64,Vector{Fl ...@@ -1800,7 +1807,7 @@ function inv_covar_multi_id(obs::Vector{uwreal}; wpm::Union{Dict{Int64,Vector{Fl
cov = get_covar_multi_id(obs, wpm=wpm) cov = get_covar_multi_id(obs, wpm=wpm)
cov_inv = inv(cov) cov_inv = inv(cov)
return (cov_inv' + cov_inv) / 2. return (cov_inv' + cov_inv) / 2.
end end =#
@doc raw""" @doc raw"""
pvalue(chisq::Function, pvalue(chisq::Function,
...@@ -1840,7 +1847,8 @@ function pvalue(chisq::Function, ...@@ -1840,7 +1847,8 @@ function pvalue(chisq::Function,
data::Vector{uwreal}, data::Vector{uwreal},
W::Vector{Float64}=Vector{Float64}(); W::Vector{Float64}=Vector{Float64}();
wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing} = Dict{Int64,Vector{Float64}}(), wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing} = Dict{Int64,Vector{Float64}}(),
nmc::Int64 = 5000) nmc::Int64 = 5000,
C::AbstractMatrix{Float64} = ADerrors.cov(data,wpm))
n = length(xp) # Number of fit parameters n = length(xp) # Number of fit parameters
m = length(data) # Number of data m = length(data) # Number of data
...@@ -1892,8 +1900,6 @@ function pvalue(chisq::Function, ...@@ -1892,8 +1900,6 @@ function pvalue(chisq::Function,
Px[i,i] = W[i] + Px[i,i] Px[i,i] = W[i] + Px[i,i]
end end
C = cov(data)
nu = sqrt(C) * Px * sqrt(C) nu = sqrt(C) * Px * sqrt(C)
N = length(nu[1,:]) N = length(nu[1,:])
...@@ -1919,7 +1925,8 @@ function pvalue(chisq::Function, ...@@ -1919,7 +1925,8 @@ function pvalue(chisq::Function,
data::Vector{uwreal}, data::Vector{uwreal},
W::Array{Float64,2}; W::Array{Float64,2};
wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing} = Dict{Int64,Vector{Float64}}(), wpm::Union{Dict{Int64,Vector{Float64}},Dict{String,Vector{Float64}}, Nothing} = Dict{Int64,Vector{Float64}}(),
nmc::Int64 = 5000) nmc::Int64 = 5000,
C::AbstractMatrix{Float64} = ADerrors.cov(data,wpm))
n = length(xp) # Number of fit parameters n = length(xp) # Number of fit parameters
m = length(data) # Number of data m = length(data) # Number of data
...@@ -1955,7 +1962,7 @@ function pvalue(chisq::Function, ...@@ -1955,7 +1962,7 @@ function pvalue(chisq::Function,
hi = LinearAlgebra.pinv(maux) hi = LinearAlgebra.pinv(maux)
Px = W - hm' * hi * hm Px = W - hm' * hi * hm
C = cov(data)
nu = sqrt(C) * Px * sqrt(C) nu = sqrt(C) * Px * sqrt(C)
......
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