Commit ff40c6fb authored by Alberto Ramos's avatar Alberto Ramos

Documentation working

parent eb38e343
......@@ -19,7 +19,7 @@ version = "2.0.0"
[[BDIO]]
deps = ["Documenter", "Nettle"]
git-tree-sha1 = "60af425a7baf34c51f96dc5169f6fbf66169a58a"
git-tree-sha1 = "6097f62fdbf858aa97d928005f2bc511e06018bc"
repo-rev = "master"
repo-url = "https://gitlab.ift.uam-csic.es/alberto/bdio.jl.git"
uuid = "375f315e-f2c4-11e9-2ef9-134f02f79e27"
......@@ -54,21 +54,21 @@ version = "3.9.0"
[[ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "27eb374570946a02aa184ef5b403dabaa7380693"
git-tree-sha1 = "6e7aa35d0294f647bb9c985ccc34d4f5d371a533"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.10.4"
version = "0.10.6"
[[Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "InteractiveUtils", "Reexport"]
git-tree-sha1 = "1e9bba7984e78aa8cdeea7f9f7cc984ad4e4b1c7"
git-tree-sha1 = "5639e44833cfcf78c6a73fbceb4da75611d312cd"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.12.2"
version = "0.12.3"
[[CommonSubexpressions]]
deps = ["Test"]
git-tree-sha1 = "efdaf19ab11c7889334ca247ff4c9f7c322817b0"
git-tree-sha1 = "34aa50efad19a788db0cb2eb44d149942f64816a"
uuid = "bbf7d656-a473-5ed7-a52c-81e309532950"
version = "0.2.0"
version = "0.2.1"
[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
......@@ -142,9 +142,9 @@ version = "0.8.2"
[[Documenter]]
deps = ["Base64", "Dates", "DocStringExtensions", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "395fa1554c69735802bba37d9e7d9586fd44326c"
git-tree-sha1 = "f3464968c65fc78846dad1c038c474a2c39bbb23"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.24.11"
version = "0.25.0"
[[FFMPEG]]
deps = ["FFMPEG_jll"]
......@@ -199,12 +199,6 @@ git-tree-sha1 = "2f56bee16bd0151de7b6a1eeea2ced190a2ad8d4"
uuid = "559328eb-81f9-559d-9380-de523a88c83c"
version = "1.0.5+3"
[[GMP_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "4dd9301d3a027c05ec403e756ee7a60e3c367e5d"
uuid = "781609d7-10c4-51f6-84f2-b8444358ff6d"
version = "6.1.2+5"
[[GR]]
deps = ["Base64", "DelimitedFiles", "HTTP", "JSON", "LinearAlgebra", "Printf", "Random", "Serialization", "Sockets", "Test", "UUIDs"]
git-tree-sha1 = "247adbd2b33c0c4b42efa20d1e807acf6312145f"
......@@ -213,9 +207,9 @@ version = "0.50.1"
[[GeometryBasics]]
deps = ["IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "c1a5a41f28a301d1bf7c393671accee56b02d207"
git-tree-sha1 = "119f32f9c2b497b49cd3f7f513b358b82660294c"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.2.13"
version = "0.2.15"
[[GeometryTypes]]
deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "StaticArrays"]
......@@ -225,9 +219,9 @@ version = "0.8.3"
[[HTTP]]
deps = ["Base64", "Dates", "IniFile", "MbedTLS", "Sockets"]
git-tree-sha1 = "ec87d5e2acbe1693789efbbe14f5ea7525758f71"
git-tree-sha1 = "eca61b35cdd8cd2fcc5eec1eda766424a995b02f"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
version = "0.8.15"
version = "0.8.16"
[[IniFile]]
deps = ["Test"]
......@@ -316,9 +310,9 @@ version = "1.0.2"
[[MbedTLS_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "f85473aeb7a2561a5c58c06c4868971ebe2bcbff"
git-tree-sha1 = "a0cb0d489819fa7ea5f9fa84c7e7eba19d8073af"
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.16.6+0"
version = "2.16.6+1"
[[Measures]]
git-tree-sha1 = "e498ddeee6f9fdb4551ce855a46f54dbd900245f"
......@@ -345,12 +339,6 @@ git-tree-sha1 = "f57e8e907faab4f55f9f164313a633509ac83e2c"
uuid = "49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9"
version = "0.4.0"
[[Nettle_jll]]
deps = ["GMP_jll", "Libdl", "Pkg"]
git-tree-sha1 = "d69f99a48b9f5722bff9f0fa031f1c916b657017"
uuid = "4c82536e-c426-54e4-b420-14f461c4ed8b"
version = "3.4.1+1"
[[Ogg_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "59cf7a95bf5ac39feac80b796e0f39f9d69dc887"
......@@ -376,9 +364,9 @@ uuid = "91d4177d-7536-5919-b921-800302f37372"
version = "1.3.1+1"
[[OrderedCollections]]
git-tree-sha1 = "12ce190210d278e12644bcadf5b21cbdcf225cd3"
git-tree-sha1 = "293b70ac1780f9584c89268a6e2a560d938a7065"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.2.0"
version = "1.3.0"
[[PGFPlotsX]]
deps = ["ArgCheck", "DataStructures", "Dates", "DefaultApplication", "DocStringExtensions", "MacroTools", "Parameters", "Requires", "StatsBase"]
......
......@@ -11,7 +11,7 @@
module ADerrors
import ForwardDiff, Statistics, FFTW, LinearAlgebra, QuadGK, BDIO, Printf
import ForwardDiff, Statistics, FFTW, LinearAlgebra, QuadGK, BDIO, Printf, Roots
import ForwardDiff: HessianConfig, GradientConfig, Chunk, hessian!
# Include data types
......
......@@ -74,7 +74,14 @@ function add_DB(delta::Vector{Float64}, id::Int64, iv::Vector{Int64}, ws::wspace
end
ws.nob += 1
push!(ws.map_nob, id)
if (!haskey(ws.map_ids, id))
if (haskey(ws.map_ids, id))
if (length(delta) != ws.fluc[ws.map_ids[id]].nd)
error("Mistmatch in data length for the same ensemble ID")
end
if (iv != ws.fluc[ws.map_ids[id]].ivrep)
error("Mistmatch in replica vector for the same ensemble ID")
end
else
ws.map_ids[id] = ws.nob
end
......@@ -313,12 +320,14 @@ function uwerror(a::uwreal, ws::wspace, wpm::Dict{Int64,Vector{Float64}})
end
if (wp[3] > 0.0)
iw = 1
for k in 2:nt
if (a.cfd[j].drho[k]*wp[3] > a.cfd[j].gamm[k]/a.cfd[j].gamm[1])
continue
iw = k-1
break
end
end
a.cfd[j].iw = k-1
a.cfd[j].iw = iw
end
if (wp[4] > 0.0)
......@@ -612,9 +621,9 @@ empt = Dict{Int64,Vector{Float64}}()
uwreal(data::Vector{Float64}, mcid::Int64[, replica::Vector{Int64}], idm::Vector{Int64}, nms::Int64)
Returns an `uwreal` data type. Depending on the first argument, the `uwreal` stores the following information:
- A single `Float64`. In this case the variable acts in exactly the same way as a real number. This is understood as a quantity with zero error.
- A 2 element Vector of `Float64` `[value, error]`. In this case the data is understood as `value +/- error`.
- A Vector of `Float64` of length larger than 2. In this case the data is understood as consecutive measurements of an observable in a Monte Carlo (MC) simulation.
- Input is a single `Float64`. In this case the variable acts in exactly the same way as a real number. This is understood as a quantity with zero error.
- Input is a 2 element Vector of `Float64` `[value, error]`. In this case the data is understood as `value +/- error`.
- Input is a Vector of `Float64` of length larger than 4. In this case the data is understood as consecutive measurements of an observable in a Monte Carlo (MC) simulation.
In the last two cases, an ensemble `ID` is required as input. Data with the same `ID` are considered as correlated (i.e. fully correlated for the case of a `value +/- error` observables measured on the same sample for the case of data from a MC simulation). For example:
......@@ -644,8 +653,8 @@ a = uwreal(rand(1000), 12, [500, 100, 400])
```
### Gaps in the measurements
In some situations an observable is not measured in every configuration. In this case two additional arguments aree needed to define the observable
- `idm`. Type `Vector{Int64}`. `idm[n]` labels the configuration where data[n] is measured.
In some situations an observable is not measured in every configuration. In this case two additional arguments are needed to define the observable
- `idm`. Type `Vector{Int64}`. `idm[n]` labels the configuration where `data[n]`is measured.
- `nms`. Type `Int64`. The total number of measurements in the ensemble
```@example
using ADerrors # hide
......@@ -694,17 +703,17 @@ b = sin(2.0*a)
uwerr(b)
println("Look how I propagate errors: ", b)
c = 1.0 + b - 2.0*sin(a)*cos(a)
uwerr(b)
uwerr(c)
println("Look how good I am at this (zero error!): ", c)
```
### Optimal window
Error in data coming from a Monte Carlo ensemble is performed by summing the autocorrelation function ``\Gamma_{\rm ID}(t)`` of the data for each ensemble ID. In practice this sum is truncated up to a window ``W_{\rm ID}``.
Error in data coming from a Monte Carlo ensemble is determined by summing the autocorrelation function ``\Gamma_{\rm ID}(t)`` of the data for each ensemble ID. In practice this sum is truncated up to a window ``W_{\rm ID}``.
By default, the summation window is determined as [proposed by U. Wolff](https://inspirehep.net/literature/621085) with a parameter ``S_{\tau} = 4``, but other methods are avilable via the optional argument `wpm`.
For each ensemble `ID` one has to pass a vector of `Float64` of length 4. The first three components of the vector specify the criteria to determine the summation window:
For each ensemble `ID` one can pass a vector of `Float64` of length 4. The first three components of the vector specify the criteria to determine the summation window:
- `vp[1]`: The autocorrelation function is summed up to `t = round(vp[1])`.
- `vp[2]`: The sumation window is determined [using U. Wolff poposal](https://inspirehep.net/literature/621085) with ``S_{\tau} = {\rm vp[2]}``.
- `vp[3]`: The autocorrelation function ``\Gamma(t)`` is summed up a point where its error ``\delta\Gamma(t)`` is a factor `vp[3]` times larger than the signal.
......@@ -716,44 +725,80 @@ Note that:
- One, and only one, of the components `vp[1:3]` has to be positive. This chooses your criteria to determine the summation window.
```@example
using ADerrors # hide
a = uwreal(rand(2000), 1233)
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
# Load the data in a uwreal
a = uwreal(x.^2, 1233)
wpm = Dict{Int64,Vector{Float64}}()
# Use default analysis (stau = 4.0)
uwerr(a)
println(a)
println("default: ", a, " (tauint = ", taui(a, 1233), ")")
# This will still do default analysis because
# a does not depend on emsemble 300
wpm[300] = [-1.0, 8.0, -1.0, 145.0]
uwerr(a, wpm)
println(a)
println("default: ", a, " (tauint = ", taui(a, 1233), ")")
# Fix the summation window to 1 (i.e. uncorrelated data)
wpm[1233] = [1.0, -1.0, -1.0, -1.0]
uwerr(a, wpm)
println(a)
println("uncorrelated: ", a, " (tauint = ", taui(a, 1233), ")")
# Use stau = 1.5
wpm[1233] = [-1.0, 1.5, -1.0, -1.0]
uwerr(a, wpm)
println(a)
println("stau = 1.5: ", a, " (tauint = ", taui(a, 1233), ")")
# Use fixed window 15 and add tail with texp = 100.0
wpm[1233] = [15.0, -1.0, -1.0, 100.0]
uwerr(a, wpm)
println(a)
println("Fixed window 15, texp=100: ", a, " (tauint = ", taui(a, 1233), ")")
# Sum up to the point that the signal in Gamma is
# 1.5 times the error and add a tail with texp = 30.0
wpm[1233] = [-1.0, -1.0, 1.5, 30.0]
uwerr(a, wpm)
println(a)
println("signal/noise=1.5, texp=30: ", a, " (tauint = ", taui(a, 1233), ")")
```
"""
uwerr(a::uwreal, wpm::Dict{Int64,Vector{Float64}}) = ADerrors.uwerror(a::uwreal, wsg, wpm)
uwerr(a::uwreal) = ADerrors.uwerror(a::uwreal, wsg, empt)
"""
cov(a::Vector{uwreal}[, wpm::Dict{Int64,Vector{Float64}}])
Determine the covariance matrix between the vector of observables `a[:]`.
```@example
using ADerrors, LinearAlgebra # hide
a = uwreal([1.3, 0.01], 1) # 1.3 +/- 0.01
b = uwreal([5.3, 0.23], 2) # 5.3 +/- 0.23
uwerr(a)
uwerr(b)
x = [a+b, a-b]
mat = cov(x)
println("Covariance: ", mat[1,1], " ", mat[1,2])
println(" ", mat[2,1], " ", mat[2,2])
println("Check (should be zero): ", mat[1,1] - mat[2,2])
println("Check (should be zero): ", mat[1,2] - (err(a)^2-err(b)^2))
```
# Case of Monte Carlo data
An optional parameter `wpm` can be used to choose the summation window for the relevant autocorrelation functions. The situation is completely analogous to the case of error analysis of single variables.
"""
cov(a::Vector{uwreal}) = cov(a::Vector{uwreal}, wsg, empt)
cov(a::Vector{uwreal}, wpm::Dict{Int64,Vector{Float64}}) = cov(a::Vector{uwreal}, wsg, wpm::Dict{Int64,Vector{Float64}})
......
......@@ -2,7 +2,7 @@
function find_mcid(a::uwreal, mcid::Int64)
if (length(a.cfd) == 0)
return 0
return nothing
else
for i in 1:length(a.ids)
if (a.ids[i] == mcid)
......@@ -11,53 +11,214 @@ function find_mcid(a::uwreal, mcid::Int64)
end
end
return 0
return nothing
end
err(a::uwreal) = a.err
"""
err(a::uwreal)
Returns the error of the `uwreal` variable `a`. It is assumed that `uwerr` has been run on the variable so that an error is available. Otherwise an error message is printed.
```@example
using ADerrors # hide
a = uwreal([1.2, 0.2], 12) # a = 1.2 +/- 0.2
uwerr(a)
println("a has error: ", err(a))
```
"""
function err(a::uwreal)
if (length(a.cfd) == 0)
error("No error available... maybe run uwerr")
end
return a.err
end
"""
value(a::uwreal)
Returns the (mean) value of the `uwreal` variable `a`
```@example
using ADerrors # hide
a = uwreal([1.2, 0.2], 12) # a = 1.2 +/- 0.2
uwerr(a)
println("a has central value: ", value(a))
```
"""
value(a::uwreal) = a.mean
derror(a::uwreal) = a.derr
"""
derror(a::uwreal)
Returns an estimate of teh error of the error of the `uwreal` variable `a`. It is assumed that `uwerr` has been run on the variable so that an error is available. Otherwise an error message is printed.
```@example
using ADerrors # hide
a = uwreal([1.2, 0.2], 12) # a = 1.2 +/- 0.2
uwerr(a)
println("a has error of the error: ", derror(a))
```
"""
function derror(a::uwreal)
if (length(a.cfd) == 0)
error("No error available... maybe run uwerr")
end
return a.derr
end
"""
taui(a::uwreal, id::Int64)
Returns the value of tauint for the ensemble `id`. It is assumed that `uwerr` has been run on the variable and that `id` contributes to the observable `a`. Otherwise an error message is printed.
```@example
using ADerrors # hide
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
a = uwreal(x.^2, 666)
uwerr(a)
println("Error analysis result: ", a, " (tauint = ", taui(a, 666), ")")
```
"""
function taui(a::uwreal, mcid::Int64)
idx = find_mcid(a, mcid)
if (idx == 0)
return 0.5
if (idx == nothing)
error("No error available... maybe run uwerr")
else
return a.cfd[idx].taui
end
end
"""
dtaui(a::uwreal, id::Int64)
Returns an estimate on the error of tauint for the ensemble `id`. It is assumed that `uwerr` has been run on the variable and that `id` contributes to the observable `a`. Otherwise an error message is printed.
```@example
using ADerrors # hide
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
a = uwreal(x.^2, 666)
uwerr(a)
println("Error analysis result: ", a,
" (tauint = ", taui(a, 666), " +/- ", dtaui(a, 666), ")")
```
"""
function dtaui(a::uwreal, mcid::Int64)
idx = find_mcid(a, mcid)
if (idx == 0)
return 0.0
if (idx == nothing)
error("No error available... maybe run uwerr")
else
return a.cfd[idx].dtaui
end
end
"""
window(a::uwreal, id::Int64)
Returns the summation window for the ensemble `id`. It is assumed that `uwerr` has been run on the variable and that `id` contributes to the observable `a`. Otherwise an error message is printed.
```@example
using ADerrors # hide
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
a = uwreal(x.^2, 666)
uwerr(a)
println("Error analysis result: ", a,
" (window = ", window(a, 666), ")")
```
"""
function window(a::uwreal, mcid::Int64)
idx = find_mcid(a, mcid)
if (idx == 0)
return 0
if (idx == nothing)
error("No error available... maybe run uwerr")
else
return a.cfd[idx].iw
end
end
"""
rho(a::uwreal, id::Int64)
Returns the normalized autocorrelation function of `a` for the ensemble `id`. It is assumed that `uwerr` has been run on the variable and that `id` contributes to the observable `a`. Otherwise an error message is printed.
```@example
using ADerrors # hide
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
a = uwreal(x.^2, 666)
uwerr(a)
v = rho(a, 666)
for i in 1:length(v)
println(i, " ", v[i])
end
```
"""
function rho(a::uwreal, mcid::Int64)
idx = find_mcid(a, mcid)
if (idx == 0)
return [1.0]
if (idx == nothing)
error("No error available... maybe run uwerr")
else
return a.cfd[idx].gamm ./ a.cfd[idx].gamm[1]
end
end
"""
rho(a::uwreal, id::Int64)
Returns an estimate of the error on the normalized autocorrelation function of `a` for the ensemble `id`. It is assumed that `uwerr` has been run on the variable and that `id` contributes to the observable `a`. Otherwise an error message is printed.
```@example
using ADerrors # hide
# Generate some correlated data
eta = randn(1000)
x = Vector{Float64}(undef, 1000)
x[1] = 0.0
for i in 2:1000
x[i] = x[i-1] + eta[i]
if abs(x[i]) > 1.0
x[i] = x[i-1]
end
end
a = uwreal(x.^2, 666)
uwerr(a)
v = rho(a, 666)
dv = drho(a, 666)
for i in 1:length(v)
println(i, " ", v[i], " +/- ", dv[i])
end
```
"""
function drho(a::uwreal, mcid::Int64)
idx = find_mcid(a, mcid)
if (idx == 0)
return [0.0]
if (idx == nothing)
error("No error available... maybe run uwerr")
else
return a.cfd[idx].drho
end
......@@ -219,11 +380,11 @@ function details(a::uwreal, ws::wspace, io::IO=stdout, names::Dict{Int64, String
ip = sortperm(v, rev=true)
for i in 1:length(a.cfd)
idx = ws.map_ids[a.ids[ip[i]]]
nd = ws.fluc[idx].nd
sndt = join(ws.fluc[idx].ivrep, ",")
sid = truncate_ascii(get(names, a.ids[ip[i]], string(a.ids[ip[i]])), ntrunc)
if (nd > 1)
Printf.@printf(" # %45s %6.2f %10d\n",
sid, 100.0 .* a.cfd[ip[i]].var ./ a.err^2, nd)
if (ws.fluc[idx].nd > 1)
Printf.@printf(" # %45s %6.2f %s\n",
sid, 100.0 .* a.cfd[ip[i]].var ./ a.err^2, sndt)
else
Printf.@printf(" # %45s %6.2f -\n",
sid, 100.0 .* a.cfd[ip[i]].var ./ a.err^2)
......@@ -236,6 +397,60 @@ end
details(a::uwreal; io::IO=stdout, names::Dict{Int64, String} = Dict{Int64, String}()) = details(a, wsg, io, names)
"""
read_uwreal(fb)
Given a `BDIO` file handler `fb`, this routine returns the observable stored in the current record.
```@example
using ADerrors # hide
using BDIO
a = uwreal(rand(2000), 12)
fb = BDIO_open("/tmp/foo.bdio", "w", "Test file")
write(a, fb, 8)
BDIO_close(fb)
# Open the file and move to first record
fb = BDIO_open("/tmp/foo.bdio", "r")
BDIO_seek!(fb)
# Read observable
b = read_uwreal(fb)
# Check
c = a - b
uwerr(c)
println("Better be zero: ", c)
```
"""
read_uwreal(fb) = read_bdio(fb, ADerrors.wsg)
"""
write_uwreal(p::uwreal, fb, iu::Int)
Given a `BDIO` file handler `fb`, this writes the observable `p` in a BDIO resord with user info `iu`.
```@example
using ADerrors # hide
using BDIO
a = uwreal(rand(2000), 12)
# Create a BDIO file and write a with user info 8.
fb = BDIO_open("/tmp/foo.bdio", "w", "Test file")
write(a, fb, 8)
BDIO_close(fb)
# Open the file and move to first record
fb = BDIO_open("/tmp/foo.bdio", "r")
BDIO_seek!(fb)
# Read observable
b = read_uwreal(fb)
# Check
c = a - b
uwerr(c)
println("Better be zero: ", c)
```
"""
write_uwreal(p::uwreal, fb, iu::Int) = write_bdio(p::uwreal, fb, iu::Int, ADerrors.wsg)
......@@ -9,6 +9,32 @@
### created: Fri Jun 26 21:48:35 2020
###
"""
cobs(avgs::Vector{Float64}, Mcov::Array{Float64, 2}, ids::Vector{Int64})
Returns a vector of `uwreal` such that their mean values are `avgs[:]` and their covariance is `Mcov[:,:]`. In order to construct these observables `n=length(avgs)` ensemble ID are used. These have to be specified in the vector `ids[:]`.
```@example
using ADerrors # hide
# Put some average values and covariance
avg = [16.26, 0.12, -0.0038]
Mcov = [0.478071 -0.176116 0.0135305
-0.176116 0.0696489 -0.00554431
0.0135305 -0.00554431 0.000454180]
# Produce observables with ensemble ID
# [1, 2001, 32]. Do error analysis
p = cobs(avg, Mcov, [1, 2001, 32])
uwerr.(p)
# Check central values are ok
avg2 = value.(p)
println("Better be zero: ", sum((avg.-avg2).^2))
# Check that the covariance is ok
Mcov2 = cov(p)
println("Better be zero: ", sum((Mcov.-Mcov2).^2))
```
"""
function cobs(avgs::Vector{Float64}, cov::Array{Float64, 2}, ids::Vector{Int64})
ch = LinearAlgebra.cholesky(cov)
......
......@@ -9,6 +9,33 @@
### created: Fri Jun 26 19:30:27 2020
###
"""
root_error(fnew::Function, x0::Float64, data::Vector{uwreal})
Returns `x` such that `fnew(x, data) = 0` (i.e. a root of the function). The error in `data` is propagated to the root position `x`.
```@example
using ADerrors # hide
# First define some arbitrary data
data = Vector{uwreal}(undef, 3)
data[1] = uwreal([1.0, 0.2], 120)
data[2] = uwreal([1.2, 0.023], 121)
data[3] = uwreal(rand(1000), 122)
# Now define a function
f(x, p) = x + p[1]*x + cos(p[2]*x+p[3])
# Find its root using x0=1.0 as initial
# guess of the position of the root
x = root_error(f, 1.0, data)
uwerr(x)
println("Root: ", x)
# Check
z = f(x, data)
uwerr(z)
println("Better be zero (with zero error): ", z)
```
"""
function root_error(fnew::Function, x::Float64,
data::Vector{uwreal})
......@@ -16,13 +43,20 @@ function root_error(fnew::Function, x::Float64,
return fnew(x[1], x[2:end])
end
xv = zeros(Float64, length(data)+1)
xv[1] = x
for i in 1:length(data)
xv[i+1] = data[i].mean
end
grad = ForwardDiff.gradient(fvec, xv)
return addobs(data, -grad[2:end]./grad[1], x)
xdt = @view xv[2:end]
f = x -> fnew(x, xdt)
D(f) = x -> ForwardDiff.derivative(f,float(x))
xv[1] = Roots.find_zero((f,D(f)), x, Roots.Newton())
cfg = GradientConfig(fvec, xv, Chunk{2}());
grad = ForwardDiff.gradient(fvec, xv, cfg)
gw = @view grad[2:end]
return addobs(data, -gw ./ grad[1], xv[1])
end
function chiexp(hess::Array{Float64, 2}, data::Vector{uwreal}, W::Vector{Float64})
......@@ -64,6 +98,32 @@ function chiexp(hess::Array{Float64, 2}, data::Vector{uwreal}, W::Array{Float64,
return trcov(Px, data)
end
@doc raw"""
chiexp(chisq::Function,
xp::Vector{Float64},
data::Vector{uwreal};
W = Vector{Float64}())
Given a ``\chi^2(p, d)``, function of the fit parameters `p[:]` and the data `d[:]`, compute the expected value of the ``\chi^2(p, d)``.
### Arguments
- `chisq`: Must be a function of two vectors (i.e. `chisq(p::Vector, d::Vector)`). The function is assumed to have the form
``\chi^2(p, d) = \sum_i [d_i - f_i(p)]W_{ij}[d_j - f_j(p)]``
where the function ``f_i(p)`` is an arbitrary function of the fit parameters. In simple words, the function is assumed to be quadratic in the data.
- `xp`: A vector of `Float64`. The value of the fit parameters at the minima.
- `data`: A vector of `uwreal`. The data whose fluctuations enter in the evaluation of the `chisq`.
- `W`: A matrix. The weights that enter in the evaluation of the `chisq` function. If a vector is passed, the matrix is assumed to be diagonal (i.e. **uncorrelated** fit). If no weights are passed, the routines assumes that `W` is diagonal with entries given by the inverse errors squared of the data (i.w. the `chisq` is weighted with the errors of the data).
### Example
```@example
using ADerrors # hide
```
"""
function chiexp(chisq::Function,
xp::Vector{Float64},
data::Vector{uwreal};
......
using ADerrors
a = uwreal([1.0, 0.1], 1)
b = uwreal([0.5, 0.023], 23)
b = uwreal(rand(10000), 23)
c = 1.0 + sin(a+b)
d = sin(a)*cos(b) + cos(a)*sin(b) - 3.0
......
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