Skip to content

Commit

Permalink
add diagnostic.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Nov 19, 2023
1 parent 0528a85 commit fbf3d60
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/DataAssim.jl
Expand Up @@ -18,6 +18,7 @@ include("ensemble.jl")
include("fourDVar.jl")
include("KalmanFilter.jl")
include("TwinExperiment.jl")
include("diagnostic.jl")

export FreeRun, fourDVar, TwinExperiment, LinShallowWater1DModel, KalmanFilter
export AbstractModel, ModelFun
Expand Down
47 changes: 47 additions & 0 deletions src/diagnostic.jl
@@ -0,0 +1,47 @@


function _talagrand_diagram!(nbins,buffer,x,y)
@assert size(x,1) == size(y,1)
@assert size(x,2) == size(buffer,1)
@assert size(nbins,1) == size(buffer,1) + 1

@inbounds for i = 1:size(x,1)
buffer .= @view x[i,:]
sort!(buffer)
ibin = count(<(y[i]), buffer) + 1
nbins[ibin] += 1
end

return nbins
end

_talagrand_diagram(x,y) = _talagrand_diagram!(zeros(Int,size(x,2)+1),x[1,:],x,y)

"""
freq = talagrand_diagram(x,y)
Compute the frequency of each bins for a Talagrand Diagram where
`x` is the ensemble (array of the size sample × ensemble size) and `y` are
the obervations (sample).
## Example
```julia
using PyPlot
Nens = 100
Nsample = 10000
# Ensemble
x = randn(Nsample,Nens)
# Observation
y = randn(Nsample)
freq = talagrand_diagram(x,y)
plot(freq)
plot(ones(size(freq)) / size(freq,1))
ylim(0,ylim()[2])
xlabel("bins")
ylabel("frequency")
```
"""
talagrand_diagram(x,y) = _talagrand_diagram(x,y) / size(x,1)
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -5,3 +5,4 @@ using Statistics

include("test_ensemble.jl")
include("test_4dvar_assim.jl")
include("test_diagnostic.jl")
16 changes: 16 additions & 0 deletions test/test_diagnostic.jl
@@ -0,0 +1,16 @@
using Random
using Test
using DataAssim: talagrand_diagram

# Example from
# ABC of ensemble forecasting
# by Peter Houtekamer, ARMA
# https://collaboration.cmc.ec.gc.ca/cmc/cmoi/product_guide/docs/lib/ens_en.pdf
# https://web.archive.org/web/20231118202650/https://collaboration.cmc.ec.gc.ca/cmc/cmoi/product_guide/docs/lib/ens_en.pdf

x = [1.5;; 2.3;; 0.8;; 4.1;; 0.3]
y = [2.95]


freq = talagrand_diagram(x,y)
@test nbins == [0,0,0,0,1,0]

0 comments on commit fbf3d60

Please sign in to comment.