<commit_before>﻿"""Conversion of copula.

This class makes the conversion between copula parameters and dependence measures
"""
import numpy as np
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri


VINECOPULA = importr('VineCopula')

def get_param1_interval(copula):
    """
    """
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str")

    if copula in [1, 'Gaussian', 2, 't']:
        return -1., 1.
    elif copula in [3, 'Clayton']:
        return 0., np.inf
    elif copula in [4, 'Gumbel']:
        return 1., np.inf
    elif copula in [5, 'Frank']:
        return -np.inf, np.inf
    else:
        raise NotImplementedError("Not implemented yet.")

def get_param2_interval(copula):
    """
    """
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str")

    if copula in [2, 't']:
        return 1.e-6, np.inf
    else:
        raise NotImplementedError("Not implemented yet.")

def get_tau_interval(copula):
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str. Not: ", type(copula))

    if copula in [1, 'Gaussian', 2, 't']:
        return -1., 1.
    elif copula in [3, 'Clayton']:
        return 0., 1.
    elif copula in [4, 'Gumbel']:
        return 0., 1.
    elif copula in [5, 'Frank']:
        return 0., 1.
    else:
        raise NotImplementedError("Not implemented yet.")

class Conversion(object):
    """
    Static class to convert dependence parameters.
    """    
    def __init__(self, family):
        self.family = family

    def to_copula_parameter(self, measure_param, dep_measure):
        """Convert the dependence_measure to the copula parameter.
        """
        if isinstance(measure_param, np.ndarray):
            n_sample = measure_param.shape[0]
            assert n_sample == measure_param.size, \
                AttributeError("It must be a vector")
        elif isinstance(measure_param, float):
            n_sample = 1
        else:
            raise TypeError("Wrong type for measure_param")
        
        if dep_measure == "KendallTau":
            r_params = numpy2ri(measure_param)
            copula_param = np.asarray(VINECOPULA.BiCopTau2Par(self._family, r_params))
        elif dep_measure == "PearsonRho":
            copula_param = self._copula.fromPearsonToParam(measure_param)
        else:
            raise ValueError("Unknow Dependence Measure")

        return copula_param
    
    def to_Kendall(self, params):
        """Convert the dependence_measure to the copula parameter.
        """
        r_params = numpy2ri(params)
        copula_param = np.asarray(VINECOPULA.BiCopPar2Tau(self._family, r_params))
        return copula_param

    def to_Pearson(self, measure_param):
        """Convert the dependence_measure to the copula parameter.
        """
        return self._copula.fromParamToPearson(measure_param)
        
    @property
    def family(self):
        return self._family
        
    @family.setter
    def family(self, value):
        """
        """<commit_msg>Corrected a bug in to_Kendall and changed the intervals in the limits.<commit_after>﻿"""Conversion of copula.

This class makes the conversion between copula parameters and dependence measures
"""
import numpy as np
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri


VINECOPULA = importr('VineCopula')

def get_param1_interval(copula):
    """
    """
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str")

    if copula in [1, 'Gaussian', 2, 't']:
        return -1., 1.
    elif copula in [3, 'Clayton']:
        return 0., np.inf
    elif copula in [4, 'Gumbel']:
        return 1., np.inf
    elif copula in [5, 'Frank']:
        return -np.inf, np.inf
    else:
        raise NotImplementedError("Not implemented yet.")

def get_param2_interval(copula):
    """
    """
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str")

    if copula in [2, 't']:
        return 1.e-6, np.inf
    else:
        raise NotImplementedError("Not implemented yet.")

def get_tau_interval(copula):
    assert isinstance(copula, (int, str)), \
        TypeError("Input must be int or str. Not: ", type(copula))

    if copula in [1, 'Gaussian', 2, 't']:
        return -0.99, 0.99
    elif copula in [3, 'Clayton']:
        return 0., 0.99
    elif copula in [4, 'Gumbel']:
        return 0., 0.99
    elif copula in [5, 'Frank']:
        return 0., 0.99
    else:
        raise NotImplementedError("Not implemented yet.")

class Conversion(object):
    """
    Static class to convert dependence parameters.
    """    
    def __init__(self, family):
        self.family = family

    def to_copula_parameter(self, measure_param, dep_measure):
        """Convert the dependence_measure to the copula parameter.
        """
        if isinstance(measure_param, np.ndarray):
            n_sample = measure_param.shape[0]
            assert n_sample == measure_param.size, \
                AttributeError("It must be a vector")
        elif isinstance(measure_param, float):
            n_sample = 1
        else:
            raise TypeError("Wrong type for measure_param")
        
        if dep_measure == "KendallTau":
            r_params = numpy2ri(measure_param)
            copula_param = np.asarray(VINECOPULA.BiCopTau2Par(self._family, r_params))
        elif dep_measure == "PearsonRho":
            copula_param = self._copula.fromPearsonToParam(measure_param)
        else:
            raise ValueError("Unknow Dependence Measure")

        return copula_param
    
    def to_Kendall(self, params):
        """Convert the dependence_measure to the copula parameter.
        """
        if isinstance(params, np.ndarray):
            r_params = numpy2ri(params)
        elif isinstance(params, float):
            r_params = params
        else:
            raise TypeError("Wrong type for params. Got: ", type(params))
        copula_param = np.asarray(VINECOPULA.BiCopPar2Tau(self._family, r_params))
        return copula_param

    def to_Pearson(self, measure_param):
        """Convert the dependence_measure to the copula parameter.
        """
        return self._copula.fromParamToPearson(measure_param)
        
    @property
    def family(self):
        return self._family
        
    @family.setter
    def family(self, value):
        """
        """
{'fraction_non_alphanumeric': 0.10687847008019741, 'fraction_numerical': 0.012954966070326958, 'mean_word_length': 2.3864229765013056, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2098119', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1967, 'n_words': 553}