Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

45 lines (36 sloc) 2.386 kb
! http://en.wikipedia.org/wiki/Pearson_correlation#Geometric_interpretation
! Example, suppose 5 countries have gross national products of 1, 2, 3, 5, and 8 billion dollars, resp.
! Suppose these 5 countries (same order) are found to have 11%, 12%, 13%, 15%, and 18% poverty.
! Then let x and y be ordered 5-element vectors containing the above data: x = (1, 2, 3, 5, 8) and
! y = (0.11, 0.12, 0.13, 0.15, 0.18).
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! COPYRIGHT (C) 2011 Vid Ayer <svaksha@gmail.com>
! LICENSE: GNU AGPLv3 <http://www.gnu.org/licenses/agpl-3.0.html>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
program pearsonCorrelationAlGeo
IMPLICIT NONE ! require all variables to be explicitly declared
INTEGER :: Elements_Used = 5, n = 1
CHARACTER(LEN=*), PARAMETER :: FORMAT = "(F9.3, F9.3, 2F9.3 )"
integer, dimension(5) :: gnp = (/ 1, 2, 3, 5, 8 /)
REAL, dimension(5) :: poverty = (/ 0.11, 0.12, 0.13, 0.15, 0.18 /)
REAL :: vector_prod_gnpgnp = 0.0
REAL :: vector_prod_povertypoverty = 0.0
REAL :: vector_prod_gnppoverty = 0.0
REAL :: cos_theta = 0.0
! Scalar vector product (do loop) for gnp and poverty
DO n = 1, Elements_Used
vector_prod_gnpgnp = vector_prod_gnpgnp + gnp(n) * gnp(n)
vector_prod_povertypoverty = vector_prod_povertypoverty + poverty(n) * poverty(n)
vector_prod_gnppoverty = vector_prod_gnppoverty + gnp(n) * poverty(n)
END DO
cos_theta = vector_prod_gnppoverty / (SQRT (vector_prod_gnpgnp) * SQRT (vector_prod_povertypoverty))
! Print result to terminal
write (*,'(''Vector product value of Cos Theta = '', 1g12.4)') cos_theta
write (*,'(''Vector product of gnp and poverty coordinates = '', 1g12.4)') vector_prod_gnppoverty
write (*,'(''Vector product of GNP coordinates = '', 1g12.4)') vector_prod_gnpgnp
write (*,'(''Vector product of POVERTY coordinates = '', 1g12.4)') vector_prod_povertypoverty
! Note that the above data were deliberately chosen to be perfectly correlated: y = 0.10 + 0.01 x
! The Pearson correlation coefficient must therefore be exactly one. Centering the data (shifting x
! by E(x) = 3.8 and y by E(y) = 0.138) yields x = (−2.8, −1.8, −0.8, 1.2, 4.2) and
! y = (−0.028, −0.018, −0.008, 0.012, 0.042), from which cos_theta = 1, as expected.
END program pearsonCorrelationAlGeo
Jump to Line
Something went wrong with that request. Please try again.