sdv-dev / Copulas

A library to model multivariate data using copulas.
https://sdv.dev/Copulas/
Other
532 stars 104 forks source link

Clayton and Gumbel Copula Error #324

Open dorcasgicuku opened 2 years ago

dorcasgicuku commented 2 years ago

Working with Gumbel and Clayton Copula with weather dataset produces the following error for some set of variables.

Clayton Error ValueError Traceback (most recent call last)

in 14 clayton_parameters = clayton_copula_model.to_dict() 15 print("The theta value of", pairedData, "is",clayton_parameters['theta']) ---> 16 clayton_copula_function() in clayton_copula_function() 11 clayton_data = clayton_df.to_numpy() 12 #Fit the model to paired data obtained from permutations the dataset ---> 13 clayton_copula_model.fit(clayton_data) 14 clayton_parameters = clayton_copula_model.to_dict() 15 print("The theta value of", pairedData, "is",clayton_parameters['theta']) ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in fit(self, X) 182 raise ValueError("Constant column.") 183 raise ValueError("Unable to compute tau.") --> 184 self._compute_theta() 185 186 def to_dict(self): ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in _compute_theta(self) 163 """Compute theta, validate it and assign it to self.""" 164 self.theta = self.compute_theta() --> 165 self.check_theta() 166 167 def fit(self, X): ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in check_theta(self) 126 if (not lower <= self.theta <= upper) or (self.theta in self.invalid_thetas): 127 message = 'The computed theta value {} is out of limits for the given {} copula.' --> 128 raise ValueError(message.format(self.theta, self.copula_type.name)) 129 130 def check_fit(self): ValueError: The computed theta value -0.6795841166551324 is out of limits for the given CLAYTON copula Gumbel Copula ValueError Traceback (most recent call last) in 14 gumbel_parameters = gumbel_copula_model.to_dict() 15 print(gumbel_parameters['theta']) ---> 16 gumbel_copula_function() 17 # gumbel_copula_model.fit(arch_data) 18 # gumbel_parameters = gumbel_copula_model.to_dict() in gumbel_copula_function() 11 gumbel_data = gumbel_df.to_numpy() 12 #Fit the model to paired data obtained from permutations the dataset ---> 13 gumbel_copula_model.fit(gumbel_data) 14 gumbel_parameters = gumbel_copula_model.to_dict() 15 print(gumbel_parameters['theta']) ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in fit(self, X) 182 raise ValueError("Constant column.") 183 raise ValueError("Unable to compute tau.") --> 184 self._compute_theta() 185 186 def to_dict(self): ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in _compute_theta(self) 163 """Compute theta, validate it and assign it to self.""" 164 self.theta = self.compute_theta() --> 165 self.check_theta() 166 167 def fit(self, X): ~\Anaconda3\envs\copulas_env\lib\site-packages\copulas\bivariate\base.py in check_theta(self) 126 if (not lower <= self.theta <= upper) or (self.theta in self.invalid_thetas): 127 message = 'The computed theta value {} is out of limits for the given {} copula.' --> 128 raise ValueError(message.format(self.theta, self.copula_type.name)) 129 130 def check_fit(self): ValueError: The computed theta value 0.6602079416724338 is out of limits for the given GUMBEL copula.
arfogg commented 1 month ago

Hello, I am having a similar error to this - @dorcasgicuku did you come to a solution? Details of my problem are below:

Environment Details

Copulas version: 0.9.0 Python version: 3.8.8 (iPython environment in Spyder) Operating System: Windows

Error Description

Fitting Bivariate Gumbel copula returns ValueError

Steps to reproduce

from copulas.bivariate import Bivariate

# Format data
copula_arr=np.array([x,y]).T

# Initialise the copula
copula=Bivariate(copula_type='gumbel')

# Fit the copula to the extremes
copula.fit(copula_arr)

Please note that x and y are on uniform margins as required. Please see histograms of x and y below. image

Error

Traceback (most recent call last):

  File "C:\Users\admin\AppData\Local\Temp\ipykernel_15604\3537192975.py", line 1, in <module>
    run_solar_earth_bi_eva.phi_d_vs_ae()

  File "C:\Users\admin\Documents\wind_waves_akr_code\run_solar_earth_bi_eva\run_solar_earth_bi_eva.py", line 680, in phi_d_vs_ae
    bs_copula_arr.append(fit_copula_to_extremes.fit_copula_bivariate(bs_phid_um, bs_ae_um, 'phi_d', 'AE'))

  File "C:\Users\admin\Documents\wind_waves_akr_code\bi_multi_variate_eva\fit_copula_to_extremes.py", line 61, in fit_copula_bivariate
    copula.fit(copula_arr)

  File "C:\Users\admin\anaconda3\lib\site-packages\copulas\bivariate\base.py", line 186, in fit
    self._compute_theta()

  File "C:\Users\admin\anaconda3\lib\site-packages\copulas\bivariate\base.py", line 167, in _compute_theta
    self.check_theta()

  File "C:\Users\admin\anaconda3\lib\site-packages\copulas\bivariate\base.py", line 130, in check_theta
    raise ValueError(message.format(self.theta, self.copula_type.name))

ValueError: The computed theta value 0.9601028214324197 is out of limits for the given GUMBEL copula.

Experimenting, the Gumbel copula has a theta range of 1-inf:

from copulas.bivariate import Bivariate
copula=Bivariate(copula_type='gumbel')
print(copula.theta_interval)
out: [1, inf]

Given that I'm inputing data on uniform margins, why is the copula fitting the data in a way that's out of bounds? Please note that I have also used this same copula from this package without errors.

Thanks, Alexandra

arfogg commented 1 week ago

Just to note I came to a solution on this. Essentially the data I was putting in to copula.fit was not related as it should be. Essentially x and y were almost independent, and hence the fitting was not a success.

Hence I'm not sure if this is a bug. If that's the case, it could be helpful to make the error message more verbose if possible! Thanks, Alexandra