---
title: "ADerrors.jl — Basic Example"
author: "Plaquette Archive"
format:
  html:
    theme: cosmo
    include-in-header: "../quarto-header.html"
    include-before-body: "../quarto-before-body.html"
    self-contained: true
engine: julia
execute:
  eval: false
---

# Error analysis with ADerrors.jl

This notebook demonstrates how to perform error analysis of Monte Carlo
data using [ADerrors.jl](https://igit.ific.uv.es/alramos/aderrors.jl),
the Julia implementation of the $\Gamma$-method with automatic
differentiation [@Ramos:2018vgu].

## Creating an observable

Load the package and create a mock Monte Carlo observable:

```{julia}
using ADerrors

# Create a uwreal with 1000 measurements
data = randn(1000)
obs = uwreal(data, "Mock MC run")

# Basic statistics
println("Mean:  ", value(obs))
println("Error: ", err(obs))
```

## Propagating errors

With automatic differentiation, error propagation through arbitrary
functions is exact:

```{julia}
# Define a function of the observable
f(x) = sin(x)^2 + log(1 + x^2)

result = f(obs)
uwerr(result)

println("f(obs) = ", value(result), " ± ", err(result))
```

## Combining independent runs

ADerrors can also combine measurements that from independent
runs. Just label the Monte Carlo IDs:

```{julia}
data1 = randn(500)
data2 = randn(500)

obs1 = uwreal(data1, "Run A")
obs2 = uwreal(data2, "Run B")

combined = obs1 + obs2
uwerr(combined)

println("Combined: ", value(combined), " ± ", err(combined))
```

## References

See [@Wolff:2003sm] for the original $\Gamma$-method and
[@Ramos:2018vgu] for the automatic differentiation extension.
