The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
pearson1.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file pearson1.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 /* Doxygen
54  -------
55  #GROUP Maths
56  #SUBGROUP Statistics
57  #FUNCTION blPearson1()
58  Calculates the Pearson correlation coefficient making a single pass
59  through the data
60 */
61 /************************************************************************/
62 /* Includes
63 */
64 #include <math.h>
65 #include "MathType.h"
66 
67 /************************************************************************/
68 /* Defines and macros
69 */
70 
71 /************************************************************************/
72 /* Globals
73 */
74 
75 /************************************************************************/
76 /* Prototypes
77 */
78 
79 
80 /************************************************************************/
81 /*>REAL blPearson1(REAL *x, REAL *y, int NItem)
82  --------------------------------------------
83 *//**
84 
85  \param[in] *x Array of x items
86  \param[in] *y Array of y items
87  \param[in] NItem Number of items
88  \return Pearson correlation coefficient
89 
90  This version makes a single pass through the data
91 
92 - 15.07.94 Original By: ACRM
93 - 18.01.96 Alternative version which does a single pass through the
94  data. Method from page 191 of Statistical Methods in
95  Biology 2 ed. Norman TJ Bailey. Publ. Unibooks 1981 By: RM
96 - 07.07.14 Use bl prefix for functions By: CTP
97 */
98 REAL blPearson1(REAL *x, REAL *y, int NItem)
99 {
100  REAL SumX,
101  SumY,
102  SumXSq,
103  SumYSq,
104  SumXY,
105  Sx, Sy, c, n,
106  r;
107  int i;
108 
109  SumX = SumY = SumXSq = SumYSq = SumXY = (REAL)0.0;
110  for(i=0; i<NItem; i++)
111  {
112  SumX += x[i];
113  SumY += y[i];
114  SumXSq += x[i]*x[i];
115  SumYSq += y[i]*y[i];
116  SumXY += x[i]*y[i];
117  }
118  /* Calculate correlation coefficient */
119 
120  n = (REAL)NItem;
121 
122  Sx = SumXSq - SumX*SumX/n;
123  Sy = SumYSq - SumY*SumY/n;
124  c = SumXY - SumX*SumY/n;
125 
126  r = c / (REAL)sqrt((double)(Sx * Sy));
127 
128  return(r);
129 }
130 
131 
REAL blPearson1(REAL *x, REAL *y, int NItem)
Definition: pearson1.c:98
double REAL
Definition: MathType.h:67
Type definitions for maths.