Commit 5f3ab2cc authored by Antonino's avatar Antonino

Reverted Gamma method to accept only Vector

parent 30cb3eb7
...@@ -14,9 +14,9 @@ by an newer version of ADerrors should be deprecrated. ...@@ -14,9 +14,9 @@ by an newer version of ADerrors should be deprecrated.
- chiexp(chisq::Function,par,d,C,W) - chiexp(chisq::Function,par,d,C,W)
""" """
module GammaMethod module GammaMethod
import ADerrors, ForwardDiff, LinearAlgebra import ADerrors, ForwardDiff, LinearAlgebra
""" """
calc_gamma(a1::ADerrors.uwreal [,a2::Aderrors.uwreal] ,id::Union{Int64,String}[, ws::ADerrors.wspace]) calc_gamma(a1::ADerrors.uwreal [,a2::Aderrors.uwreal] ,id::Union{Int64,String}[, ws::ADerrors.wspace])
Computes the Gamma function associated to the ensemble `id` without the bias included by ADerrors for t>=0 Computes the Gamma function associated to the ensemble `id` without the bias included by ADerrors for t>=0
...@@ -90,16 +90,16 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -90,16 +90,16 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
gamma[ig] = gamma[ig] / (nd_eff - nrcnt*(ig-1)) gamma[ig] = gamma[ig] / (nd_eff - nrcnt*(ig-1))
end end
return gamma return gamma
end end
calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String,ws::ADerrors.wspace) = calc_gamma(a1,a2,ws.str2id[id],ws) calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String,ws::ADerrors.wspace) = calc_gamma(a1,a2,ws.str2id[id],ws)
calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String) = calc_gamma(a1,a2,ADerrors.wsg.str2id[id],ADerrors.wsg) calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String) = calc_gamma(a1,a2,ADerrors.wsg.str2id[id],ADerrors.wsg)
calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64) = calc_gamma(a1,a2,id,ADerrors.wsg) calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64) = calc_gamma(a1,a2,id,ADerrors.wsg)
calc_gamma(a::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace) = calc_gamma(a,a,id,ws) calc_gamma(a::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace) = calc_gamma(a,a,id,ws)
calc_gamma(a::ADerrors.uwreal,id::String,ws::ADerrors.wspace) = calc_gamma(a,a,ws.str2id[id],ws) calc_gamma(a::ADerrors.uwreal,id::String,ws::ADerrors.wspace) = calc_gamma(a,a,ws.str2id[id],ws)
calc_gamma(a::ADerrors.uwreal,id::String) = calc_gamma(a,a,ADerrors.wsg.str2id[id],ADerrors.wsg) calc_gamma(a::ADerrors.uwreal,id::String) = calc_gamma(a,a,ADerrors.wsg.str2id[id],ADerrors.wsg)
calc_gamma(a::ADerrors.uwreal,id::Int64) = calc_gamma(a,a,id,ADerrors.wsg) calc_gamma(a::ADerrors.uwreal,id::Int64) = calc_gamma(a,a,id,ADerrors.wsg)
function drho(rho::Vector{Float64},nd::Int64,iw::Int64=0) function drho(rho::Vector{Float64},nd::Int64,iw::Int64=0)
iw = iw ==0 ? ADerrors.wopt_ulli(nd,ADerrors.DEFAULT_STAU,rho) : iw iw = iw ==0 ? ADerrors.wopt_ulli(nd,ADerrors.DEFAULT_STAU,rho) : iw
nt = length(rho); nt = length(rho);
dr = zeros(nt); dr = zeros(nt);
...@@ -122,9 +122,9 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -122,9 +122,9 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
dr[t] = sqrt(dr[t]/nd) dr[t] = sqrt(dr[t]/nd)
end end
return dr return dr
end end
""" """
bias(a1::ADerrors.uwreal [,a2::ADerrors.uwreal], id::Union{Int64,String} [, ws::Aderrors.wspace]; iw::Int64=0) bias(a1::ADerrors.uwreal [,a2::ADerrors.uwreal], id::Union{Int64,String} [, ws::Aderrors.wspace]; iw::Int64=0)
it return the bias associated to `id` that ADErrors includes in the estimate of the autocorrelation function it return the bias associated to `id` that ADErrors includes in the estimate of the autocorrelation function
...@@ -138,7 +138,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -138,7 +138,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
if `iw = 0` the integration window is determined by the automatic windowing procedure with S_τ = 4.0 if `iw = 0` the integration window is determined by the automatic windowing procedure with S_τ = 4.0
""" """
function bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace;iw::Int64=0) function bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace;iw::Int64=0)
idx = ws.map_ids[id] idx = ws.map_ids[id]
nd = ws.fluc[idx].nd nd = ws.fluc[idx].nd
if nd ==1 if nd ==1
...@@ -146,13 +146,13 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -146,13 +146,13 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
end end
gamma = calc_gamma(a1,a2,id,ws) gamma = calc_gamma(a1,a2,id,ws)
return gamma[1] + 2.0*sum(gamma[2:iw]) return gamma[1] + 2.0*sum(gamma[2:iw])
end end
bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String,ws::ADerrors.wspace; iw::Int64=0)= bias(a1,a2,ws.str2id[id],ws,iw=iw) bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String,ws::ADerrors.wspace; iw::Int64=0)= bias(a1,a2,ws.str2id[id],ws,iw=iw)
bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64; iw::Int64=0) = bias(a1,a2,id,wsg,iw=iw) bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64; iw::Int64=0) = bias(a1,a2,id,wsg,iw=iw)
bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String; iw::Int64=0)= bias(a1,a2,ADerrors.wsg.str2id[id],wsg,iw=iw) bias(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::String; iw::Int64=0)= bias(a1,a2,ADerrors.wsg.str2id[id],wsg,iw=iw)
function bias(a::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace;iw::Int64=0) function bias(a::ADerrors.uwreal,id::Int64,ws::ADerrors.wspace;iw::Int64=0)
idx = ws.map_ids[id] idx = ws.map_ids[id]
nd = ws.fluc[idx].nd nd = ws.fluc[idx].nd
if nd ==1 if nd ==1
...@@ -161,13 +161,13 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -161,13 +161,13 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
gamma = calc_gamma(a,id,ws) gamma = calc_gamma(a,id,ws)
iw = iw ==0 ? ADerrors.wopt_ulli(nd,ADerrors.DEFAULT_STAU,gamma) : iw iw = iw ==0 ? ADerrors.wopt_ulli(nd,ADerrors.DEFAULT_STAU,gamma) : iw
return gamma[1] + 2.0*sum(gamma[2:iw]) return gamma[1] + 2.0*sum(gamma[2:iw])
end end
bias(a::ADerrors.uwreal,id::String,ws::ADerrors.wspace; iw::Int64=0) = bias(a,ws.str2id[id],ws,iw=iw) bias(a::ADerrors.uwreal,id::String,ws::ADerrors.wspace; iw::Int64=0) = bias(a,ws.str2id[id],ws,iw=iw)
bias(a::ADerrors.uwreal,id::Int64; iw::Int64=0) = bias(a,id,ADerrors.wsg,iw=iw) bias(a::ADerrors.uwreal,id::Int64; iw::Int64=0) = bias(a,id,ADerrors.wsg,iw=iw)
bias(a::ADerrors.uwreal,id::String; iw::Int64=0) = bias(a,ADerrors.wsg.str2id[id],ADerrors.wsg,iw=iw) bias(a::ADerrors.uwreal,id::String; iw::Int64=0) = bias(a,ADerrors.wsg.str2id[id],ADerrors.wsg,iw=iw)
function bias(vobs::Vector{ADerrors.uwreal}, id::Int64, ws::ADerrors.wspace;) function bias(vobs::Vector{ADerrors.uwreal}, id::Int64, ws::ADerrors.wspace;)
b = zeros(length(vobs),length(vobs)) b = zeros(length(vobs),length(vobs))
iw = maximum([ADerrors.window(obs,id) for obs in vobs]) iw = maximum([ADerrors.window(obs,id) for obs in vobs])
for i in 1:length(vobs)-1, j in i+1:length(vobs) for i in 1:length(vobs)-1, j in i+1:length(vobs)
...@@ -178,14 +178,14 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -178,14 +178,14 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
b[i,i] = bias(vobs[i],id,ws,iw = 0) b[i,i] = bias(vobs[i],id,ws,iw = 0)
end end
return b return b
end end
bias(vobs::Vector{ADerrors.uwreal}, id::String, ws::ADerrors.wspace) = bias(vobs,ws.str2id[id],ws) bias(vobs::Vector{ADerrors.uwreal}, id::String, ws::ADerrors.wspace) = bias(vobs,ws.str2id[id],ws)
bias(vobs::Vector{ADerrors.uwreal}, id::Int64) = bias(vobs,id,ADerrors.wsg) bias(vobs::Vector{ADerrors.uwreal}, id::Int64) = bias(vobs,id,ADerrors.wsg)
bias(vobs::Vector{ADerrors.uwreal}, id::String) = bias(vobs,ADerrors.wsg.str2id[id],ADerrors.wsg) bias(vobs::Vector{ADerrors.uwreal}, id::String) = bias(vobs,ADerrors.wsg.str2id[id],ADerrors.wsg)
@doc raw""" @doc raw"""
cov_pe(vobs::Vectir{ADerrors.uwreal}[,ws:ADerrors.wspace]) cov_pe(vobs::Vectir{ADerrors.uwreal}[,ws:ADerrors.wspace])
Computes the unbiased covariance matrix of `vobs` à la `pyerrors`. Computes the unbiased covariance matrix of `vobs` à la `pyerrors`.
...@@ -201,7 +201,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -201,7 +201,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
See also [`cov_min`](@ref) [`cov_ad`](@ref) See also [`cov_min`](@ref) [`cov_ad`](@ref)
""" """
function cov_pe(vobs::AbstractVector{ADerrors.uwreal}, ws::ADerrors.wspace) function cov_pe(vobs::Vector{ADerrors.uwreal}, ws::ADerrors.wspace)
ids = ADerrors.unique_ids_multi(vobs, ws) ids = ADerrors.unique_ids_multi(vobs, ws)
nid = length(ids) nid = length(ids)
nobs = length(vobs) nobs = length(vobs)
...@@ -222,12 +222,12 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -222,12 +222,12 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
corr = [cov[i,j]/sqrt(cov[i,i]*cov[j,j]) for i in 1:nobs, j in 1:nobs] corr = [cov[i,j]/sqrt(cov[i,i]*cov[j,j]) for i in 1:nobs, j in 1:nobs]
cov = [corr[i,j]*vobs[i].err*vobs[j].err for i in 1:nobs, j in 1:nobs] cov = [corr[i,j]*vobs[i].err*vobs[j].err for i in 1:nobs, j in 1:nobs]
return cov return cov
end end
cov_pe(vobs::AbstractVector{ADerrors.uwreal}) = cov_pe(vobs,ADerrors.wsg) cov_pe(vobs::Vector{ADerrors.uwreal}) = cov_pe(vobs,ADerrors.wsg)
@doc raw""" @doc raw"""
cov_min(vobs::Vector{ADerrors.uwreal} [ws::ADerrors.wspace]) cov_min(vobs::Vector{ADerrors.uwreal} [ws::ADerrors.wspace])
It computes the unbiased covariance matrix of `vobs` using a mixed method. It computes the unbiased covariance matrix of `vobs` using a mixed method.
...@@ -242,7 +242,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -242,7 +242,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
See also [`cov_pe`](@ref) [`cov_ad`](@ref) See also [`cov_pe`](@ref) [`cov_ad`](@ref)
""" """
function cov_min(vobs::AbstractVector{ADerrors.uwreal},ws::ADerrors.wspace) function cov_min(vobs::Vector{ADerrors.uwreal},ws::ADerrors.wspace)
ids = ADerrors.unique_ids_multi(vobs, ws) ids = ADerrors.unique_ids_multi(vobs, ws)
nid = length(ids) nid = length(ids)
iw = fill(typemax(Int64),nid) iw = fill(typemax(Int64),nid)
...@@ -281,11 +281,11 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -281,11 +281,11 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
corr = [cov[i,j]/sqrt(cov[i,i]*cov[j,j]) for i in 1:nobs, j in 1:nobs] corr = [cov[i,j]/sqrt(cov[i,i]*cov[j,j]) for i in 1:nobs, j in 1:nobs]
cov = [corr[i,j]*vobs[i].err*vobs[j].err for i in 1:nobs, j in 1:nobs] cov = [corr[i,j]*vobs[i].err*vobs[j].err for i in 1:nobs, j in 1:nobs]
return cov return cov
end end
cov_min(vobs::AbstractVector{ADerrors.uwreal}) = cov_min(vobs,ADerrors.wsg) cov_min(vobs::Vector{ADerrors.uwreal}) = cov_min(vobs,ADerrors.wsg)
@doc raw""" @doc raw"""
cov_AD(vobs::Vector{ADerrors.uwrela} [ws::ADerrors.wspace]; biased::Bool=true, info::Bool=false) cov_AD(vobs::Vector{ADerrors.uwrela} [ws::ADerrors.wspace]; biased::Bool=true, info::Bool=false)
It computes the unbiased covariance matrix of `vobs` "à la ADerrors". It computes the unbiased covariance matrix of `vobs` "à la ADerrors".
...@@ -302,7 +302,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -302,7 +302,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
- `info::Bool`: if `true` return the integration window used to compute the covariance matrix as a Dict{Int64,Int64}(id=> iw) - `info::Bool`: if `true` return the integration window used to compute the covariance matrix as a Dict{Int64,Int64}(id=> iw)
See also [`cov_min`](@ref) [`cov_pe`](@ref) See also [`cov_min`](@ref) [`cov_pe`](@ref)
""" """
function cov_AD(vobs::AbstractVector{ADerrors.uwreal},ws::ADerrors.wspace; biased::Bool=true, info::Bool=false) function cov_AD(vobs::Vector{ADerrors.uwreal},ws::ADerrors.wspace; biased::Bool=true, info::Bool=false)
ids = ADerrors.unique_ids_multi(vobs, ws) ids = ADerrors.unique_ids_multi(vobs, ws)
nid = length(ids) nid = length(ids)
iw = zeros(Int64,nid) iw = zeros(Int64,nid)
...@@ -346,17 +346,17 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -346,17 +346,17 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
cov[n2,n1] = cov[n1,n2] cov[n2,n1] = cov[n1,n2]
end end
return info ? (cov, Dict(ids.=>iw)) : cov return info ? (cov, Dict(ids.=>iw)) : cov
end end
cov_AD(vobs::AbstractVector{ADerrors.uwreal};biased::Bool=false) = cov_AD(vobs,ADerrors.wsg,biased=biased) cov_AD(vobs::Vector{ADerrors.uwreal};biased::Bool=false) = cov_AD(vobs,ADerrors.wsg,biased=biased)
""" """
chiexp(hess, C, W) chiexp(hess, C, W)
It computes the expected chisquare given the hessian of the chisquare function computed at its minimum, It computes the expected chisquare given the hessian of the chisquare function computed at its minimum,
the covariance matrix of data and the weigth function. the covariance matrix of data and the weigth function.
""" """
function chiexp(hess,C,W) function chiexp(hess,C,W)
N, = size(hess) N, = size(hess)
ndata, = size(C) ndata, = size(C)
npar = N-ndata; npar = N-ndata;
...@@ -373,15 +373,15 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -373,15 +373,15 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
sum(aux[alpha,alpha] for alpha in 1:ndata ) sum(aux[alpha,alpha] for alpha in 1:ndata )
end end
return chiexp return chiexp
end end
""" """
chiexp(chisq::Function,par,d,C,W) chiexp(chisq::Function,par,d,C,W)
It computes the expected chisquare given the chisquare function, the fit parameters and the data. It computes the expected chisquare given the chisquare function, the fit parameters and the data.
This is preferred to ADerrors.chiexp for correlated fits since it accepts the covariance matrix as an input, This is preferred to ADerrors.chiexp for correlated fits since it accepts the covariance matrix as an input,
that usually has been already computed before hand. that usually has been already computed before hand.
""" """
function chiexp(chisq::Function,par::AbstractVector{Float64},d::AbstractVector{Float64},C::AbstractMatrix{Float64},W::AbstractMatrix{Float64}) function chiexp(chisq::Function,par::Vector{Float64},d::Vector{Float64},C::AbstractMatrix{Float64},W::AbstractMatrix{Float64})
ndata = length(d); ndata = length(d);
if size(W) != (ndata,ndata) if size(W) != (ndata,ndata)
throw(DimensionMismatch("[chiexp] d and W must have same dimension")) throw(DimensionMismatch("[chiexp] d and W must have same dimension"))
...@@ -391,9 +391,9 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -391,9 +391,9 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
ccsq(x) = chisq(view(x,1:npar),view(x,npar+1:npar+ndata)) ccsq(x) = chisq(view(x,1:npar),view(x,npar+1:npar+ndata))
ForwardDiff.hessian!(hess,ccsq,[par; d]) ForwardDiff.hessian!(hess,ccsq,[par; d])
return chiexp(hess,C,W) return chiexp(hess,C,W)
end end
function chiexp(chisq::Function,par::AbstractVector,d::AbstractVector{Float64},C::AbstractMatrix{Float64},W::AbstractVector{Float64}) function chiexp(chisq::Function,par::Vector,d::Vector{Float64},C::AbstractMatrix{Float64},W::Vector{Float64})
ndata = length(d); ndata = length(d);
if length(W) != ndata if length(W) != ndata
throw(DimensionMismatch("[chiexp] d and W must have same dimension")) throw(DimensionMismatch("[chiexp] d and W must have same dimension"))
...@@ -403,7 +403,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro ...@@ -403,7 +403,7 @@ function calc_gamma(a1::ADerrors.uwreal,a2::ADerrors.uwreal,id::Int64,ws::ADerro
ccsq(x) = chisq(view(x,1:npar),view(x,npar+1:npar+ndata)) ccsq(x) = chisq(view(x,1:npar),view(x,npar+1:npar+ndata))
ForwardDiff.hessian!(hess,ccsq,[par; d]) ForwardDiff.hessian!(hess,ccsq,[par; d])
return chiexp(hess,C,LinearAlgebra.Diagonal(W)) return chiexp(hess,C,LinearAlgebra.Diagonal(W))
end end
chiexp(chisq::Function,par::AbstractVector{ADerrors.uwreal},d::AbstractVector{ADerrors.uwreal},C,W) = chiexp(chisq,ADerrors.value.(par),ADerrors.value.(d),C,W) chiexp(chisq::Function,par::Vector{ADerrors.uwreal},d::Vector{ADerrors.uwreal},C,W) = chiexp(chisq,ADerrors.value.(par),ADerrors.value.(d),C,W)
end end
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