Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

## Documentation

* [Statistics and parameter inference (ABC shadow)](docs/statistics.md)

## Questions

For questions and support please use the official [slack](https://geode-solutions.com/#slack) and go to the channel #module_template. The issue list of this repo is exclusively for bug reports and feature requests.
Expand All @@ -48,6 +50,7 @@ https://gitlab.univ-lorraine.fr/labos/iecl/drlib
* Illian J, Penttinen A, Stoyan H, Stoyan D (2008) Statistical analysis and modelling of spatial point patterns.Wiley, Chichester.https://doi.org/10.1002/9780470725160
* Mardia KV, Jupp PE (2000) Directional statistics. Wiley, Chichester. https://doi.org/10.1002/9780470316979
* Mauldon M (1998) Estimating mean fracture trace length and density from observations in convex windows. Rock Mechanics and Rock Engineering 31(4):201–216. https://doi.org/10.1007/s006030050021
* Stoica RS, Philippe A, Gregori P, Mateu J (2017) ABC Shadow algorithm: a tool for statistical analysis of spatial patterns. Statistics and Computing 27:1225–1238. https://doi.org/10.1007/s11222-016-9682-x
* Stoica RS (2025) Random patterns and structures in spatial data. Chapman & Hall, CRC Press, Boca Raton.https://doi.org/10.1201/9781003380382
* Van Lieshout MNM (2000) Markov point processes and their applications. Imperial College Press, London.https://doi.org/10.1142/p060

Expand Down
170 changes: 170 additions & 0 deletions docs/statistics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Statistics and parameter inference

The models of OpenGeode-Stochastic are Gibbs point processes in the exponential
family: the energy of a configuration x is

U(x) = - sum_i theta_i s_i(x), theta_i = log(gamma_i)

where gamma_i is the parameter given to the builders (a density, an
intensity, an interaction parameter) and s_i the statistic of term i. A
hard-core term (gamma = 0) is a constraint, not a parameter.

Each term has two statistics:

- the **energy statistic**, computed on the extended domain (the domain plus
its buffer) and multiplied by theta in the energy;
- the **observed statistic**, computed inside the domain D only:
`Model::observed_statistics`. This is the value compared with the data.

The inference compares observed statistics on both sides: t_obs measured on
the data in D, and t(y) measured in D on patterns y simulated on the extended
domain. This keeps the edge correction of the buffer and was validated on a
Strauss process (see below).

## The statistics

| Term (builder name) | Parameter | Observed statistic in D | Rule at the boundary of D |
|---|---|---|---|
| Point density `<set>_density` (Poisson, Strauss) | lambda | number of points in D | a point on the boundary counts |
| Fracture density `<set>_p20` | p20 (an activity, not the observed P20 once there are interactions) | number of fracture endpoints strictly inside D, divided by 2 | a fracture cut by the window counts 1/2 per endpoint inside; a crossing fracture counts 0 (Mauldon 1998); unbiased: its mean is lambda.\|D\| for a stationary Poisson process |
| Fracture intensity `<set>_p21` | p21 (an activity) | total length of the fractures inside D (divided by a characteristic length of 1) | only the part inside D counts |
| Point interaction `pwint_<sets>` (Strauss) | gamma in [0, 1] (above 1 only on sets with a hard-core) | number of pairs closer than the distance whose middle is in D | location of a pair: the middle of the two points |
| Fracture spacing `<set>_spacing` | hard-core | (constraint, not inferred) | |
| X-nodes `x_node_<sets>` | beta in [0, 1] (above 1 only on sets with a hard-core) | number of crossings between fractures of different sets, in D | location: the intersection point |
| Observation `<set>_observation` | 1 - observed ratio | fixed objects only (see `IntensityScope`) | |

The parameters are activities, not observed densities: with interactions,
the observed statistic of a density term differs from lambda.|D|. Only the
inference links the parameters to the observed statistics.

### Measuring t_obs on an outcrop

t(y) is always computed on simulated patterns with a buffer. Measuring t_obs
on the data needs the same rules, which is not always possible on an outcrop:

- point and fracture counts, lengths and X-nodes only need the objects seen in
D (the intersection point of an X-node in D is seen with both traces);
- a distance interaction (Strauss pairs, spacing) counts a pair whose middle is
in D even if one of its objects lies outside D, within the interaction
distance: such a partner is not seen on the outcrop. Counting only the
visible pairs underestimates t_obs. Measurement tools for outcrops are
planned.

## Inference: ABC shadow

`run_abc_shadow` (`inference/abc_shadow.hpp`) samples the posterior of the
parameters of the named terms, given their observed statistics, with the ABC
shadow algorithm (Stoica et al., 2017). It works on a `SimulationContext`
built by a builder:

```cpp
#include <geode/stochastic/applications/strauss_process.hpp>
#include <geode/stochastic/inference/abc_shadow.hpp>

auto context = strauss.build_simulation_context(); // starting parameters
geode::AbcShadowConfig config;
config.parameters = {
{ "A_density", 99. }, // term name and t_obs; box and step by default
{ "pwint_A", 19. }
};
config.outer_iterations = 2000;
config.shadow_steps = 100;
config.mh_steps = 3000;
config.burn_in_steps = 200000;
geode::RandomEngine engine;
engine.set_seed( 42 );
const auto trace = geode::run_abc_shadow( context, config, engine );
// trace.thetas: one line of theta = log(gamma) per outer iteration
```

Each outer iteration simulates mh_steps Metropolis-Hastings steps at the
current theta, measures t_y in D, then runs a shadow chain of shadow_steps
proposals on theta with the acceptance ratio exp(<theta' - theta,
t_obs - t_y>), computed in log. The terms not named keep their parameters.

### Defaults

- **Prior box:** uniform. theta_max defaults to the integrability bound of
the term (`Model::theta_bounds`): 0 for an interaction whose attraction is
not integrable, no bound otherwise; a larger theta_max is refused.
theta_min defaults to -infinity (a flat prior on theta, log-uniform on
gamma).
- **Nothing observed:** t_obs = 0 pushes theta to -infinity (the data suggest
a hard-core): give a finite theta_min, which then regularizes; it is
refused otherwise.
- **Steps:** calibrated, unless all of them are given. A calibration that
fails (a statistic that does not vary) has already simulated: the context
state has moved, the model parameters have not.
- **Fixed objects:** refused for now (conditioning needs a constant
observation rate).

### Tuning the inference

The shadow chain uses t_y frozen during its shadow_steps = m proposals. Over
these proposals theta drifts by (h/2)(t_obs - t_y), with h = m.delta^2/12, and
the outer chain behaves like an unadjusted Langevin chain whose stability is
set by h.P, P ~ Cov(t) being the Fisher information:

- h.P < 4: stable; h.P ~ 0.1 to 0.2: close to the posterior (the variance is
inflated by about (1 + hP/4)/(1 - hP/4), to first order);
- the shadow chain then accepts almost every proposal (~97 % in the tests): **do not tune
delta for a Metropolis acceptance rate such as 0.3**, which leads to the
unstable regime;
- a smaller delta is always stable but slower: the outer autocorrelation is
1 - hP/2, so a delta ten times smaller costs a hundred times more
simulations.

The **calibration** chooses delta for you: `calibration.iterations` (100 by
default, at least 30) simulations at the starting theta estimate Cov(t), then
delta_i = sqrt(12 s_eff / (m_i Cov_ii)) with s = `calibration.step_scale`
(0.2), m_i = m/K in `one_coordinate` mode (m in `all_coordinates`), and
s_eff = s divided by the largest row sum of |correlations|, so that
correlated statistics stay stable. The trace gives Cov(t), s_eff and the
deltas: reuse them in another run by giving the deltas.

**Checking delta after a run:** `trace.estimated_step_scales` gives h.P per
parameter, with P estimated by Var(t_y)/2 over the trace (t_y varies with the
simulation noise and with theta). About 0.2 is right. Above 1, delta is too
large and the posterior is inflated; below 0.02, delta is too small and the
chain mixes slowly; `run_abc_shadow` logs a warning in both cases. Remedy:
rerun (the steps are recalibrated at the last theta, near the posterior), or
give the deltas, scaled by sqrt(0.2 / estimated h.P).

Advice:

- **mh_steps** must decorrelate successive t_y: the calibration warns when
their lag 1 correlation exceeds 0.5 (`trace.statistic_lag_correlations`).
- **Starting far from the posterior:** Cov(t) is estimated at the starting
theta, so delta can be off. Run a first call, discard its trace, then a
second call: it continues the chain from the last theta and recalibrates
near the posterior.
- **Correlated statistics** (density and pairs of a Strauss process) mix
slowly along their least informative direction: run more iterations and
check the effective sample size of each theta.
- **Continuing a chain:** the model keeps the last theta and the context its
last state; call `run_abc_shadow` again.

### Validation

- shadow chain alone: its stationary law is the truncated exponential
exp(theta (t_obs - t_y)) on the box;
- synthetic Gaussian family: the calibrated delta recovers the posterior, a
delta giving h.P = 2 inflates it as predicted, a much larger one fails;
- Poisson process: posterior of log(lambda) recovered (tolerances 0.25
posterior standard deviation on the mean and 20 % on the standard
deviation around digamma(n) - log|D| and sqrt(trigamma(n)); measured: 0.5 %
and 0.7 %);
- Strauss process (simulated on the extended domain, statistics compared in
D on both sides): parameters recovered within the posterior
spread, spread consistent with the Fisher information, and at the posterior
mean the model gives the observed statistics on average
(`tests/stochastic/inference/test-abc-shadow-strauss.cpp`).

## References

- Mauldon M (1998) Estimating mean fracture trace length and density from
observations in convex windows. Rock Mechanics and Rock Engineering
31(4):201–216. https://doi.org/10.1007/s006030050021
- Stoica RS, Philippe A, Gregori P, Mateu J (2017) ABC Shadow algorithm: a tool
for statistical analysis of spatial patterns. Statistics and Computing
27:1225–1238. https://doi.org/10.1007/s11222-016-9682-x
Loading
Loading