covariance Interface

public interface covariance

Calculates the sample covariance between two variables given in two vectors of any size with one dimension applying the following equation:

Where:

  • and are vectors with real numbers.
  • is how many numbers are included in and .

Syntax

z = covariance(x,y)

Where:

  • x and y = vectors of rank 1 with real numbers. See this example to use an array of rank larger than 1.
  • z = real number of the same kind as x and y with the sample covariance of x and y.

Example

The following program calculates the covariance between two variables:

 PROGRAM covarianceExample
    USE FU_Statistics, ONLY: covariance
    IMPLICIT NONE
    REAL, DIMENSION(5) :: x = [1., 2., 3., 4., 5.]
    REAL, DIMENSION(5) :: y = [6., 7., 8., 9., 10.]
    WRITE(*,*) covariance(x, y)
 END PROGRAM covarianceExample

Contents


Module Procedures

private pure function covariance_sp(x, y) result(res)

Arguments

Type IntentOptional Attributes Name
real(kind=sp), intent(in), DIMENSION(:) :: x

Vector of real numbers with the values of the first variable. It can have any size and it must have one dimension.

real(kind=sp), intent(in), DIMENSION(:) :: y

Vector of real numbers with the values of the second variable. It can have any size and it must have one dimension.

Return Value real(kind=sp)

Real number with the sample covariance between both variables.

private pure function covariance_dp(x, y) result(res)

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in), DIMENSION(:) :: x

Vector of real numbers with the values of the first variable. It can have any size and it must have one dimension.

real(kind=dp), intent(in), DIMENSION(:) :: y

Vector of real numbers with the values of the second variable. It can have any size and it must have one dimension.

Return Value real(kind=dp)

Real number with the sample covariance between both variables.

private pure function covariance_qp(x, y) result(res)

Arguments

Type IntentOptional Attributes Name
real(kind=qp), intent(in), DIMENSION(:) :: x

Vector of real numbers with the values of the first variable. It can have any size and it must have one dimension.

real(kind=qp), intent(in), DIMENSION(:) :: y

Vector of real numbers with the values of the second variable. It can have any size and it must have one dimension.

Return Value real(kind=qp)

Real number with the sample covariance between both variables.