import numpy as np

from ergodic import EdgeProbabilities, tabular
from ergodic.discovery import bootstrap

# 1. Scores a model returned for four variables: rows the source, columns the target.
names = ("rain", "sprinkler", "wet", "slippery")
values = np.array(
    [
        [0.00, 0.45, 0.91, 0.06],
        [0.40, 0.00, 0.83, 0.05],
        [0.04, 0.07, 0.00, 0.88],
        [0.58, 0.02, 0.10, 0.00],
    ]
)
probs = EdgeProbabilities(names, values, kind="independent", provenance={"model": "example"})
print(probs)

# 2. Every edge above 0.5, as a diagnostic graph.
print(probs.threshold(0.5))

# 3. Three exclusive states per pair: no edge, one way, the other way.
pair = probs.to_pair_states()
print(pair.to_frame().round(3))

# 4. The chance each pair is adjacent, either way.
print(probs.existence().round(3))

# 5. Bootstrap frequencies of a discovery method, as edge probabilities.
rng = np.random.default_rng(0)
x = rng.normal(size=500)
y = 0.8 * x + rng.normal(size=500)
z = 0.8 * y + rng.normal(size=500)
data = tabular({"x": x, "y": y, "z": z})
freq = EdgeProbabilities.from_bootstrap(bootstrap(data, method="ges", n_bootstrap=20, seed=0))
print(freq)
print(freq.to_frame())
