
    hMhV#                     h   d Z ddlZddlZddlmZ ddlmZ dede	de	defd	Z
ded
ej        dej        defdZd Z	 	 	 	 	 ddedededed
ej        dej        dej        fdZ	 	 	 	 	 	 	 	 ddededededed
ej        dej        dedededefdZ	 	 	 ddedededed
ej        dej        dedefdZdS )a  Project: PhiK - correlation analyzer library

Created: 2019/11/23

Description:
    Convert Pearson correlation value into a chi2 value of a contingency test 
    matrix of a bivariate gaussian, and vice-versa.
    Calculation uses scipy's mvn library.

Authors:
    KPMG Advanced Analytics & Big Data team, Amstelveen, The Netherlands

Redistribution and use in source and binary forms, with or without
modification, are permitted according to the terms listed in the file
LICENSE.
    N)optimizemvnunrholowerupperreturnc                     t          j        ddg          }t          j        d| g| dgg          }t          ||||          \  }}|S )a=  Perform integral of bivariate normal gauss with correlation

    Integral is performed using scipy's mvn library.

    :param float rho: tilt parameter
    :param tuple lower: tuple of lower corner of integral area
    :param tuple upper: tuple of upper corner of integral area
    :returns float: integral value
                  ?)nparrayr   )r   r   r   muSpis          N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/phik/bivariate.py_mvn_unr      sO     
3*		B
3*sCj)**Ar1%%DAqH    sxsyc                   	 t          j        ddg          	t          j        d| g| dgg          d}fdt          t                    dz
  dz            D             }t                    dz
  dz  dk    rt                    dz
  dz  |fdt          t                    dz
  dz            D             z  }t                    dz
  dz  dk    rPt                    dz
  dz  }|                             |         gdz            |dz            gf           d}t          j        	fd	|D                       }t          j        ||s|n	|d
d         g          }|S )a5  Array of integrals over bivariate normal gauss with correlation

    Integrals are performed using scipy's mvn library.

    :param float rho: tilt parameter
    :param np.ndarray sx: bin edges array of x-axis
    :param np.ndarray sy: bin edges array of y-axis
    :returns list: list of integral values
    r   r   Fc                     g | ]J}t          t                    d z
            D ](}|         |         g|d z            |d z            gf)KS    )rangelen).0r   jr   r   s      r   
<listcomp>z_mvn_array.<locals>.<listcomp>=   sx       s2ww{##   Q%A"QU)RAY/0   r   r      c                 \    g | ](}         |         gd z            |d z            gf)S r    )r   r   r   r   r   s     r   r    z_mvn_array.<locals>.<listcomp>E   sL     
 
 
9:beRU^bQiAE34
 
 
r   Tc                 :    g | ]\  }}t          ||          S r#   )_calc_mvnun)r   r   r   r   r   s      r   r    z_mvn_array.<locals>.<listcomp>N   s+    QQQ,%[r155QQQr   N)r   r   r   r   appendconcatenate)
r   r   r   odd_oddrangesr   corrr   r   r   s
    ``    @@@r   
_mvn_arrayr,   (   s    
3*		B
3*sCj)**A G    B!)**  F 	B!qAWWq[Q 
 
 
 
 
 
>CSWWq[UVDV>W>W
 
 
 	
 GGaK1!!R1"AMMBqE2a5>Bq1uIr!a%y+ABCCCG8QQQQQ&QQQRRD>4W!C$ss)DEEDKr   c                 2    t          | |||          d         S )Nr   r   )r   r   r   r   s       r   r%   r%   T   s    r1%%a((r   r&   r   nxnync                 l   |dk     rt          d          |t          j        dd|dz             }|t          j        dd|dz             }t          j        ||f          }t	          t          |          dz
            D ]b}t	          t          |          dz
            D ]@}||         ||         f}	||dz            ||dz            f}
t          | |	|
          }||||f<   Ac||z  }t          j        t          j                  j	        ||t          j        t          j                  j	        k     <   |S )a3  Return binned pdf of bivariate normal distribution.

    This function returns a "perfect" binned bivariate normal distribution.

    :param float rho: tilt parameter
    :param int nx: number of uniform bins on x-axis. alternative to sx.
    :param int ny: number of uniform bins on y-axis. alternative to sy.
    :param np.ndarray sx: bin edges array of x-axis. default is None.
    :param np.ndarray sy: bin edges array of y-axis. default is None.
    :param int n: number of entries. default is one.
    :return: np.ndarray of binned bivariate normal pdf
    r   z-Number of entries needs to be one or greater.N   )

ValueErrorr   linspacezerosr   r   r   finfofloateps)r   r.   r/   r0   r   r   bvnr   r   r   r   r   s               r   bivariate_normal_theoryr;   X   s8   * 	1uuHIII	z[QQ''	z[QQ''
(B8

C3r77Q;  s2ww{## 	 	AUBqENEAY1q5	*EUE**AC1II		
 1HC )+(:(:(>Cbhrx  $$%Jr   subtract_from_chi2corr0scalepedestalc
                 l   |t          j        dd|dz             }|t          j        dd|	dz             }|t          d||          }|t          d||          }
|
|z
  dz  }t          j        ||t          j        |          |dk              }|t          j        |          z  }|t          |dz
  |	dz
            z  }||z
  |z  }t          | ||          }||z
  dz  }t          j        ||t          j        |          |dk              }|t          j        |          z  }|||z  z   }||z
  S )au  Calculate chi2-value of bivariate gauss having correlation value rho

    Calculate no-noise chi2 value of bivar gauss with correlation rho,
    with respect to bivariate gauss without any correlation.

    :param float rho: tilt parameter
    :param int n: number of records
    :param float subtract_from_chi2: value subtracted from chi2 calculation. default is 0.
    :param list corr0: mvn_array result for rho=0. Default is None.
    :param float scale: scale is multiplied with the chi2 if set.
    :param np.ndarray sx: bin edges array of x-axis. default is None.
    :param np.ndarray sy: bin edges array of y-axis. default is None.
    :param float pedestal: pedestal is added to the chi2 if set.
    :param int nx: number of uniform bins on x-axis. alternative to sx.
    :param int ny: number of uniform bins on y-axis. alternative to sy.
    :returns float: chi2 value
    Nr2   r3   r   r   r!   outwhere)r   r5   r,   divide
zeros_likesummin)r   r0   r<   r=   r>   r   r   r?   r.   r/   corr1delta_corr2ratiochi2_onechi2_maxcorrrchi2_rhochi2s                     r   chi2_from_phikrP      s_   < 
z[QQ''	z[QQ''}1b"%%}1b"%%u}*	BM+$>$>eqj
 
 
 rve}}$s2626***H$0sB##E5=Q&KIUk : :%1*  E 26%== H h&&D$$$r   rO   c                 R   |dk     rt          d          |t          j        dd|dz             }n|dk    rt          d          |t          j        dd|dz             }n|dk    rt          d          t          d||          }t          d||          }d|v rGt	          |          d	k    r4t          j        d
                    t	          |                               ||z
  dz  }	t          j        |	|t          j	        |	          |dk              }
|t          j
        |
          z  }|t          |dz
  |dz
            z  }||z
  |z  }| |k    rt          j        | |d          r|} | |k    rdS | |k    rdS t          j        t          dd|| |||||fd          }|S )a;  
    Correlation coefficient of bivariate gaussian derived from chi2-value

    Chi2-value gets converted into correlation coefficient of bivariate gauss
    with correlation value rho, assuming giving binning and number of records.
    Correlation coefficient value is between 0 and 1.

    Bivariate gaussian's range is set to [-5,5] by construction.

    :param float chi2: input chi2 value
    :param int n: number of records
    :param int nx: number of uniform bins on x-axis. alternative to sx.
    :param int ny: number of uniform bins on y-axis. alternative to sy.
    :param np.ndarray sx: bin edges array of x-axis. default is None.
    :param np.ndarray sy: bin edges array of y-axis. default is None.
    :param float pedestal: pedestal is added to the chi2 if set.
    :returns float: correlation coefficient
    r   z+noise pedestal should be greater than zero.Nr2   r3   r   z&number of bins along x-axis is unknownz&number of bins along y-axis is unknowni'  z8Many cells: {0:d}. Are interval variables set correctly?r!   rA   g+=)atolr   r   gh㈵>)argsxtol)r4   r   r5   r,   r   warningswarnformatrD   rE   rF   rG   iscloser   brentqrP   )rO   r0   r.   r/   r   r   r?   r=   rH   rI   rJ   rK   rL   r>   r   s                  r   phik_from_chi2rZ      s   8 !||FGGG	z[QQ''	qABBB	z[QQ''	qABBBq"b!!E q"b!!EEzzc%jj5((FMME

 	
 	
 	
 5=Q&KIUk : :%1*  E 26%== H3rAvrAv&&&H H,Eh2:dH5AAA xs			s
/1AtUE2r8#LSW  C Jr   )r&   r&   r   NN)r   NNNNr   r&   r&   )NNr   )__doc__rU   numpyr   scipyr   scipy.stats._mvnr   r8   tupler   ndarraylistr,   r%   intr;   rP   rZ   r#   r   r   <module>rc      s`                " " " " " " u U u     )E )rz )rz )d ) ) ) )X) ) ) ) )	)) 	) 	)
 	
) 	
) Z) ) ) )^ !"=% =%	=%
=% =% 	=%
 =% 	
=% 	
=% =% 	=% 	=% =% =% =% =%J H H
H
H 	H 		H
 	
H 	
H H H H H H H Hr   