Pearson product-moment correlation coefficient in Java (simple correlation coefficient algorithm for Java)

Source: Internet
Author: User
Tags pow

First, what is Pearson product-moment correlation coefficient (simple correlation coefficient)?

Related tables andRelated diagramscan reflect the relationship between the two variables and their related directions, but it is not possible to indicate exactlyTwo variablesbetweenrelatedthe degree. So the famous statisticianCarl Piersonhas designedStatistical indicators--correlation coefficient (Correlation coefficient). Correlation coefficients are statistical indicators used to reflect the correlation between variables. The correlation coefficients are calculated by the product difference method, and the same two variables and their averageDeparture difference, the correlation degree between the two variables is reflected by multiplying two deviations, and the linear single correlation coefficients are studied emphatically.

Baidu Encyclopedia:

in theStatisticsin whichPearson accumulated moment correlation coefficient(English:Pearson product-moment correlation coefficient , also known asPPMCCorPCCs[1], which is commonly used in the article R or Pearson ' s R) to measure two variables x and yRelated(linearly correlated) with values between 1 and 1. In the field of natural science, this coefficient is widely used to measure the degree of correlation between two variables. It is made up ofCarl PiersonfromFrancis Galtona similar but slightly different idea evolved in the 1880s. [2][3] this correlation coefficient is also known as "Pearson correlation coefficient r".

Correlation coefficient of wikipedia:'s accumulation moment

second, simple correlation coefficient of the formula

The Pearson correlation coefficient between the two variables is defined as the quotient of the covariance between the two variables and the standard deviation:

The above equation defines the overall correlation coefficient, which is generally expressed as Greek letter ρ (rho). Based on the sample to estimate covariance and standard deviation, the sample correlation coefficient can be obtained, which is generally expressed as r:

An equivalent expression is the mean value expressed as a standard score. Based onthe sample point (X i, Yi), the Pearson coefficient of the sample is


, and

are standard points, sample averages, and sample standard deviations, respectively.

Correlation coefficient of wikipedia:'s accumulation moment

Correlation is a kind of non-deterministic relationship, and correlation coefficient is the amount of linear correlation between variables. Due to the different research objects, the correlation coefficients are defined in the following ways.

Correlation coefficient formula

Simple correlation coefficient: Also called correlation coefficient or linear correlation coefficient, usually denoted by the letter p, used to measure the linear relationship between two variables. Complex correlation coefficients: Also called multiple correlation coefficients. Complex correlation refers to the correlation between dependent variables and multiple independent variables. For example, there is a complex correlation between the seasonal demand of a commodity and its price level and the level of employee income. Typical correlation coefficient: The main component analysis of the original groups of variables, the new linear relationship of the comprehensive indicators, and then through the comprehensive index of the linear correlation coefficient to study the relationship between the original groups of variables.

Baidu Encyclopedia:

Third, the code implementation:(1)Numeratorcalculate classto achieve fractionalmoleculecalculation;(2)Denominatorcalculate classto achieve fractionalDenominatorcalculation;(3)Callclass classcall the method above.
code one: numeratorcalculate class
/** * */package numerator.pearson.conefficient;import java.util.arraylist;import Java.util.List;import;import;import;/** * @author alan-king * * Class is going to calculate the numerator; * * */public class Numeratorcalculate {//add global varietiesprotected list<string> xlist, Ylist;public NumeratorC Alculate (list<string> xlist,list<string> ylist) {this.xlist = Xlist;this.ylist = yList;}  /** * Add operate method */public double Calcutenumerator () {double result =0.0;double xaverage = 0.0;double temp = 0.0;int Xsize = Xlist.size (); for (int x=0;x<xsize;x++) {temp + = Double.parsedouble (Xlist.get (x));} Xaverage = temp/xsize;double Yaverage = 0.0;temp = 0.0;int ysize = Ylist.size (); for (int x=0;x<ysize;x++) {temp + = Double . parsedouble (Ylist.get (x));} Yaverage = temp/ysize;//double sum = 0.0;for (int x=0;x<xsize;x++) {result+= (Double.parsedouble (XList.get (x))- Xaverage) * (Double.parsedouble (Ylist.get (x))-yaverage);} return result;}}

Code two:Denominatorcalculate class
/** *  */package numerator.pearson.conefficient;import java.util.list;/** * @author alan-king * */public class denominatorcalculate {//add denominatorcalculate methodpublic double calculatedenominator (List<String> xList, List<string> ylist) {Double standarddifference = 0.0;int size = xlist.size ();d ouble xaverage = 0.0;double YAverage = 0.0;double xexception = 0.0;double yexception = 0.0;double temp = 0.0;for (int i=0;i<size;i++) {temp + = Double.parsedoubl E (Xlist.get (i));} Xaverage = temp/size;for (int i=0;i<size;i++) {temp + = double.parsedouble (Ylist.get (i));} Yaverage = temp/size;for (int i=0;i<size;i++) {xexception + = Math.pow (double.parsedouble (Xlist.get (i))-xAverage,2) ; Yexception + = Math.pow (double.parsedouble (Ylist.get (i))-yaverage, 2);} Calculate denominator of return standarddifference = Math.sqrt (xexception*yexception);}}

Code Three:C Allclass class
/** * */package numerator.pearson.conefficient;import;import;import;import java.util.arraylist;import java.util.list;/** * @author alan-king * */public class Callclass {public static void main (string[] args) throws ioexception{double CORR = 0.0; list<string> xlist = new arraylist<string> (); list<string> ylist = new arraylist<string> (); SYSTEM.OUT.PRINTLN ("Please input your X ' s varieties and Y's varieties\r" + "differnt line,then you should key into \" S\ "to End the inputing operator! "); /initial varieties for xlist,ylist; BufferedReader br = new BufferedReader (new InputStreamReader (; String str =null;boolean flag = False;while (!) ( Str=br.readline ()). Equals ("S")) {string[] Vstr = Str.split (","); int size = VSTR.LENGTH;IF (flag = = False) {for (int i=0;i <size;i++) {Xlist.add (I, Vstr[i]);} Flag = true;} else if (flag = = True) {for (int i=0;i<size;i++) {Ylist.add (I, Vstr[i]);} Flag = FALSE;}} NumeratorcAlculate NC = new Numeratorcalculate (xlist,ylist);d ouble numerator = Nc.calcutenumerator ();D enominatorcalculate dc = new Denominatorcalculate ();d ouble denominator = Dc.calculatedenominator (xlist, ylist); CORR = Numerator/denominator; System.out.println ("We got the result by calculating:"); System.out.printf ("CORR =" +corr);}}

Iv. Output Results: if

Pearson product-moment correlation coefficient in Java (simple correlation coefficient algorithm for Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.