# Path: esg/generator_correlated_variables.py
# def generator_correlated_variables(corr_matrix, time_horizon, fixed_seed, method = 'cholesky'):
#     np.random.seed(fixed_seed)
#     ran = np.random.standard_normal((len(corr_matrix), time_horizon))
#         
#     if method == 'cholesky':
#         c = cholesky(corr_matrix, lower = True)
#     else:
#         evals, evecs = eigh(corr_matrix)
#         c = np.dot(evecs, np.diag(np.sqrt(evals)))
#     dw = np.dot(c, ran)
#     return dw


from ..generator_correlated_variables import generator_correlated_variables
from ...core_math.function_optim import function_optim
from ...core_math.functions_credit import generator_matrix, exp_matrix
from abc import ABCMeta, abstractmethod
from scipy.linalg import inv, norm
from numpy import linalg as la
from scipy.optimize import minimize
import numpy as np
        if self.pi_0 is None:
            self.pi_0 = market.JLT_pi
        if self.recovery_rate is None:
            self.recovery_rate = market.recovery_rate

        spread_list, col_index, row_index = self.market_spread
        
        def f(pi_0):
            return function_optim(pi_0, self.alpha, self.mu, self.sigma,
                                  self.recovery_rate, self.eigenvect_hist_gen, self.eigenval_hist_gen,
                                  row_index, col_index, spread_list)     
    
        res = minimize(f,x0=2)
        self.pi_0 = res.x[0]
        return self.pi_0
        

      
    def generate_spreads_and_matrix(self):
        """
            Method : generate_spreads_and_matrix
            
            Function : generate the spreads and risk-neutral transition matrix with parameters in the model
            
            Parameter : None
                    
        """        
        self.spreads=[]
        self.RN_migration_matrix=[]
        

# Path: esg/generator_correlated_variables.py
# def generator_correlated_variables(corr_matrix, time_horizon, fixed_seed, method = 'cholesky'):
#     np.random.seed(fixed_seed)
#     ran = np.random.standard_normal((len(corr_matrix), time_horizon))
#         
#     if method == 'cholesky':
#         c = cholesky(corr_matrix, lower = True)
#     else:
#         evals, evecs = eigh(corr_matrix)
#         c = np.dot(evecs, np.diag(np.sqrt(evals)))
#     dw = np.dot(c, ran)
#     return dw


# Path: esg/credit_risk/credit_model_classes.py
from ..generator_correlated_variables import generator_correlated_variables
from ...core_math.function_optim import function_optim
from ...core_math.functions_credit import generator_matrix, exp_matrix
from abc import ABCMeta, abstractmethod
from scipy.linalg import inv, norm
from numpy import linalg as la
from scipy.optimize import minimize
import numpy as np

        if self.pi_0 is None:
            self.pi_0 = market.JLT_pi
        if self.recovery_rate is None:
            self.recovery_rate = market.recovery_rate

        spread_list, col_index, row_index = self.market_spread
        
        def f(pi_0):
            return function_optim(pi_0, self.alpha, self.mu, self.sigma,
                                  self.recovery_rate, self.eigenvect_hist_gen, self.eigenval_hist_gen,
                                  row_index, col_index, spread_list)     
    
        res = minimize(f,x0=2)
        self.pi_0 = res.x[0]
        return self.pi_0
        

      
    def generate_spreads_and_matrix(self):
        """
            Method : generate_spreads_and_matrix
            
            Function : generate the spreads and risk-neutral transition matrix with parameters in the model
            
            Parameter : None
                    
        """        
        self.spreads=[]
        self.RN_migration_matrix=[]
        

        dw = generator_correlated_variables(corr_matrix = self.corr_matrix, time_horizon = self.time_horizon,  fixed_seed = self.fixed_seed)