Skip to content

Commit

Permalink
Add function to return Pearso correlation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik1704 committed Dec 6, 2023
1 parent 0625f98 commit a7c201f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/source/classes/flow/PCAFlow/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ PCAFlow
.. autoclass:: PCAFlow

.. automethod:: PCAFlow.differential_flow
.. automethod:: PCAFlow.Pearson_correlation
1 change: 0 additions & 1 deletion docs/source/classes/flow/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Flow
ReactionPlaneFlow/index
ScalarProductFlow/index
EventPlaneFlow/index
ReactionPlaneFlow/index
LeeYangZeroFlow/index
QCumulantFlow/index
PCAFlow/index
Expand Down
63 changes: 61 additions & 2 deletions src/sparkx/flow/PCAFlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class PCAFlow(FlowInterface.FlowInterface):
-------
differential_flow:
Computes the differential flow.
Pearson_correlation:
Returns the Pearson coefficient and its uncertainty if the flow is
already computed.
Attributes
----------
Expand Down Expand Up @@ -77,6 +80,8 @@ class PCAFlow(FlowInterface.FlowInterface):
An array containing the uncertainty of the flow.
Pearson_r_ : None or numpy.ndarray
An array containing the Pearson correlation between two bins.
Pearson_r_uncertainty_ : None or numpy.ndarray
An array containing the Pearson correlation uncertainty between two bins.
Raises
------
Expand Down Expand Up @@ -131,6 +136,7 @@ def __init__(self,n=2,alpha=2,number_subcalc=4):
self.FlowUncertainty_ = None

self.Pearson_r_ = None
self.Pearson_r_uncertainty_ = None


def integrated_flow(self, particle_data):
Expand Down Expand Up @@ -328,7 +334,7 @@ def __compute_uncertainty(self,bins):
for sub in range(self.number_subcalc_):
for bin in range(number_bins):
for alpha in range(self.alpha_):
self.FlowUncertainty_[bin][alpha] += (self.FlowSubCalc_[sub][bin][alpha]-self.Flow_[bin][alpha])**2./(self.number_subcalc_-1)
self.FlowUncertainty_[bin][alpha] += (self.FlowSubCalc_[sub][bin][alpha]-self.Flow_[bin][alpha])**2. / (self.number_subcalc_-1)

# take the sqrt to obtain the standard deviation
for bin in range(number_bins):
Expand All @@ -343,6 +349,19 @@ def __compute_uncertainty(self,bins):
self.Flow_[bin][alpha] = np.nan

def __compute_factorization_breaking(self,bins):
"""
Compute the factorization breaking using the Pearson correlation
coefficient Eq. (12), Ref. [1].
Parameters
----------
bins : list or np.ndarray
Bins used for the flow calculation.
Returns
-------
None
"""
# compute the Pearson coefficient Eq. (12), Ref. [1]
number_bins = len(bins)-1
r = np.zeros((number_bins,number_bins))
Expand All @@ -352,6 +371,27 @@ def __compute_factorization_breaking(self,bins):

self.Pearson_r_ = r

# do this for each sub calculation
r_sub = np.zeros((number_bins,number_bins,self.number_subcalc_))
for sub in range(self.number_subcalc_):
for a in range(number_bins):
for b in range(number_bins):
r_sub[a][b][sub] = self.SigmaVnDelta_total_[a][b][sub] / np.sqrt(self.SigmaVnDelta_total_[a][a][sub]*self.SigmaVnDelta_total_[b][b][sub])

r_err = np.zeros((number_bins,number_bins))
# sum the squared deviation from the mean
for sub in range(self.number_subcalc_):
for a in range(number_bins):
for b in range(number_bins):
r_err[a][b] += (r_sub[a][b][sub]-r[a][b])**2. / (self.number_subcalc_-1)

# take the sqrt to obtain the standard deviation
for a in range(number_bins):
for b in range(number_bins):
r_err[a][b] = np.sqrt(r_err[a][b])

self.Pearson_r_uncertainty_ = r_err

def differential_flow(self, particle_data, bins, flow_as_function_of):
"""
Compute the differential flow.
Expand Down Expand Up @@ -397,4 +437,23 @@ def differential_flow(self, particle_data, bins, flow_as_function_of):
self.__compute_uncertainty(bins)
self.__compute_factorization_breaking(bins)

return [self.Flow_, self.FlowUncertainty_]
return [self.Flow_, self.FlowUncertainty_]

def Pearson_correlation(self):
"""
Retrieve the Pearson correlation coefficient and its uncertainty
(Eq. (12), Ref. [1]).
Returns
-------
list
A list containing the Pearson correlation coefficient and its uncertainty.
- Pearson_r (numpy.ndarray): The Pearson correlation coefficient for factorization breaking between each pair of bins.
- Pearson_r_uncertainty (numpy.ndarray): The uncertainty of the Pearson correlation coefficient for factorization breaking between each pair of bins.
Notes
-----
The values in the list can be accessed by name[bin1][bin2].
"""
return [self.Pearson_r_, self.Pearson_r_uncertainty_]

0 comments on commit a7c201f

Please sign in to comment.