The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
pearson.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file pearson.c
5 
6  \version V1.3
7  \date 07.07.14
8  \brief
9 
10  \copyright (c) Dr. Andrew C. R. Martin, UCL, 1996-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40 **************************************************************************
41 
42  Usage:
43  ======
44 
45 **************************************************************************
46 
47  Revision History:
48  =================
49 - V1.0 29.01.96 Original By: ACRM
50 - V1.3 07.07.14 Use bl prefix for functions By: CTP
51 
52 
53 *************************************************************************/
54 /* Doxygen
55  -------
56  #GROUP Maths
57  #SUBGROUP Statistics
58  #FUNCTION blPearson()
59  Calculates the Pearson correlation coefficient making two
60  passes through the data
61 */
62 /************************************************************************/
63 /* Includes
64 */
65 #include <math.h>
66 #include "MathType.h"
67 
68 
69 /************************************************************************/
70 /* Defines and macros
71 */
72 
73 /************************************************************************/
74 /* Globals
75 */
76 
77 /************************************************************************/
78 /* Prototypes
79 */
80 
81 
82 /************************************************************************/
83 /*>REAL blPearson(REAL *x, REAL *y, int NItem)
84  -------------------------------------------
85 *//**
86 
87  \param[in] *x Array of x items
88  \param[in] *y Array of y items
89  \param[in] NItem Number of items
90  \return Pearson correlation coefficient
91 
92  Calculates the Pearson correlation coefficient
93  This version makes 2 passes through the data
94 
95 - 15.07.94 Original By: ACRM
96 - 07.07.14 Use bl prefix for functions By: CTP
97 
98 */
99 REAL blPearson(REAL *x, REAL *y, int NItem)
100 {
101  REAL MeanX,
102  MeanY,
103  SumXSq,
104  SumYSq,
105  SumXY,
106  xt, yt,
107  r;
108  int i;
109 
110  /* Calculate means of x and y */
111  MeanX = MeanY = (REAL)0.0;
112  for(i=0; i<NItem; i++)
113  {
114  MeanX += x[i];
115  MeanY += y[i];
116  }
117  MeanX /= NItem;
118  MeanY /= NItem;
119 
120  /* Calculate correlation coefficient */
121  SumXSq = SumYSq = SumXY = (REAL)0.0;
122  for(i=0; i<NItem; i++)
123  {
124  xt = x[i] - MeanX;
125  yt = y[i] - MeanY;
126  SumXSq += (xt * xt);
127  SumYSq += (yt * yt);
128  SumXY += (xt * yt);
129  }
130 
131  r = SumXY / (REAL)sqrt((double)(SumXSq * SumYSq));
132 
133  return(r);
134 }
135 
136 
double REAL
Definition: MathType.h:67
REAL blPearson(REAL *x, REAL *y, int NItem)
Definition: pearson.c:99
Type definitions for maths.