# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import openturns as ot
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from depimpact import ConservativeEstimate

# %matplotlib inline
# %load_ext autoreload
# %autoreload 2

random_state = 123
np.random.seed(random_state)
# -

from depimpact.tests import func_sum
from dependence import quantile_func

dim = 3
beta = np.asarray([0.1, 1, 5])
test_func = lambda x: func_sum(x, a=beta)
margins = [ot.Uniform()]*dim
families = np.zeros((dim, dim), dtype=int)
for i in range(1, dim):
    for j in range(i):
        families[i, j] = 1

# +
K = 200
n = 20000
vine_structure_1 = [[1, 0, 0],
                    [2, 2, 0],
                    [3, 3, 3]]

vine_structure_2 = [[2, 0, 0],
                    [3, 3, 0],
                    [1, 1, 1]]

vine_structure_1 = np.asarray(vine_structure_1)
vine_structure_2 = np.asarray(vine_structure_2)
# -

ot.RandomGenerator.SetSeed(0)
np.random.seed(0)
q_estimate = ConservativeEstimate(test_func, margins, families, vine_structure=vine_structure_1)
results_1 = q_estimate.gridsearch(K, n, save_grid=True)

ot.RandomGenerator.SetSeed(0)
np.random.seed(0)
q_estimate = ConservativeEstimate(test_func, margins, families, vine_structure=vine_structure_2)
results_2 = q_estimate.gridsearch(K, n, use_grid=0)

alpha = 0.90
q_func = quantile_func(alpha)
results_1.q_func = q_func
results_2.q_func = q_func
results_1.min_quantity, results_2.min_quantity

# +
fig, axes = plt.subplots(dim, dim, figsize=(15, 15), sharex=True, sharey=True)

k = 0
for i in range(1, dim):
    for j in range(i):
        ax = axes[i, j]
        for results in [results_1, results_2]:
            params_k = results.dep_params[:, k]
            quantities_k = results.quantities
            ax.plot(params_k, quantities_k, '.')
        k += 1
        
fig.tight_layout()
# -



from depimpact.dependence import VineCopula

params = np.zeros((dim, dim))
for i in range(1, dim):
    for j in range(i):
        params[i, j] = 0.5
vine = VineCopula(vine_structure_1, families, params)
vine.get_sample(n)



StarcoderdataJupyterScriptsDedupFiltered