-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathestimation_mixture.py
More file actions
57 lines (43 loc) · 2.17 KB
/
estimation_mixture.py
File metadata and controls
57 lines (43 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas as pd
import numpy as np
from scipy.optimize import minimize
from numpy.linalg import LinAlgError
from pycop import estimation, mixture, archimedean
def estim_mixture(data, opti_method, options={}):
"""
Estimate the best-fitting three-component copula model for the given data.
This function iterates over various combinations of copula families for a
three-component copula model and estimates the best-fitting model based on
the minimum IAD distance.
Args:
data (array-like): The input data for which to estimate the copula model.
Returns:
tuple: A tuple containing the following elements:
- ctype (str): The best-fitting copula type as a string in the format "fam1-fam2-fam3".
list of errors: When `allow_singular is False`, the input matrix must be symmetric positive definite
"""
results = []
for fam1 in ["clayton", "rgumbel", "rjoe", "rgalambos"]:
for fam2 in ["gaussian", "frank", "fgm", "plackett"]: #
for fam3 in ["gumbel", "rclayton", "joe", "galambos" ]:
coptype = f"{fam1}-{fam2}-{fam3}"
cop = mixture([fam1, fam2, fam3])
try:
param, cmle = estimation.fit_cmle_mixt(cop, data, opti_method, options=options)
iad_dist = estimation.IAD_dist(cop, data, param)
line = {"coptype": coptype, "param": param, "iad_dist": iad_dist}
LTC_cop = archimedean(family= coptype.split("-")[0]).LTDC(param[3])
LTDC = LTC_cop*param[0]
#print("cop:", coptype.split("-")[0], ", param:", param[3], ", LTDC:", LTDC)
results.append(line)
except (LinAlgError, TypeError) as e:
#print(e)
continue
# Find the best result based on the minimum IAD distance
# Convert the list to a DataFrame
df = pd.DataFrame(results)
# Find the row with the minimum 'iad_dist'
min_row = df.loc[df['iad_dist'].idxmin()]
# Extract 'coptype' and 'param' values
ctype, param = min_row['coptype'], min_row['param']
return ctype, param