Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pearson correlation among elements of an array #191

Merged
merged 8 commits into from
Jun 8, 2020

Conversation

jvdp1
Copy link
Member

@jvdp1 jvdp1 commented May 17, 2020

Related to the functions cov and var

As usual, everything can/must be discussed.

Other implementations

Specs:

corr - Pearson correlation of array elements

Description

Returns the Pearson correlation of the elements of array along dimension dim if the corresponding element in mask is true.

The Pearson correlation between two rows (or columns), say x and y, of array is defined as:

 corr(x, y) = cov(x, y) / sqrt( var(x) * var(y))

Syntax

result = corr(array, dim [, mask])

Arguments

array: Shall be a rank-1 or a rank-2 array of type integer, real, or complex.

dim: Shall be a scalar of type integer with a value in the range from 1 to n, where n is the rank of array.

mask (optional): Shall be of type logical and either a scalar or an array of the same shape as array.

Return value

If array is of rank 1 and of type real or complex, the result is of type real corresponding to the type of array.
If array is of rank 2 and of type real or complex, the result is of the same type as array.
If array is of type integer, the result is of type real(dp).

If array is of rank 1 and of size larger than 1, a scalar equal to 1 is returned. Otherwise, IEEE NaN is returned.
If array is of rank 2, a rank-2 array with the corresponding correlations is returned.

If mask is specified, the result is the Pearson correlation of all elements of array corresponding to true elements of mask. If every element of mask is false, the result is IEEE
NaN.

Example

program demo_corr
    use stdlib_experimental_stats, only: corr
    implicit none
    real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ]
    real :: y(1:2, 1:3) = reshape([ -1., 40., -3., 4., 10., 6. ], [ 2, 3])
    print *, corr(x, 1)           !returns 1.
    print *, corr(y, 2)           !returns reshape([ 1., -.32480, -.32480, 1. ], [ 2, 3])
end program demo_corr

jvdp1 added 2 commits May 17, 2020 15:23
Squashed commit of the following:

commit 9cd7538
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 22:48:56 2020 +0200

    corr_dev: addition of specs

commit 4af1d2d
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 22:29:42 2020 +0200

    corr_dev: addition of tests for csp and int32

commit aef4416
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 22:29:08 2020 +0200

    corr_dev: addition of tests for csp and int32

commit 0e6ec5d
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 22:19:56 2020 +0200

    corr_dev: clarification

commit 1fdabd9
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 22:14:44 2020 +0200

    corr_dev: correction of an issue with complex

commit 1632355
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 21:41:12 2020 +0200

    corr_dev:completed impl

commit 1fe73b1
Merge: 9da1d97 ce987d2
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 13:13:46 2020 +0200

    Merge remote-tracking branch 'upstream/master' into corr_dev

commit 9da1d97
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 12:46:05 2020 +0200

    corr_dev: addition of tests for int64

commit d03d828
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Sat May 16 12:22:13 2020 +0200

    corr_dev: addition of test for dp complex numbers

commit 7901ee2
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Fri May 15 20:18:03 2020 +0200

    corr_dev: correction for vector with all elements false

commit 2a119c2
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Fri May 15 19:02:12 2020 +0200

    corr_dev: done until mask

commit 19c1f8e
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Fri May 15 16:42:47 2020 +0200

    corr_dev: implemented some correlation functions

commit ec16e9c
Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com>
Date:   Fri May 15 16:12:13 2020 +0200

    corr_dev: init
@jvdp1 jvdp1 marked this pull request as ready for review May 17, 2020 13:34
Copy link
Member

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me, thanks!

Copy link
Member

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise I think it looks good. Thanks!

src/tests/stats/test_corr.f90 Outdated Show resolved Hide resolved
Copy link
Member

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing it. I think it looks great now and ready to merge.

Copy link
Member

@milancurcic milancurcic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

jvdp1 and others added 2 commits June 5, 2020 18:24
Co-authored-by: Milan Curcic <caomaco@gmail.com>
Co-authored-by: Milan Curcic <caomaco@gmail.com>
@jvdp1
Copy link
Member Author

jvdp1 commented Jun 8, 2020

If there are no other objections/comments to this PR, I will then merge this evening.

@jvdp1
Copy link
Member Author

jvdp1 commented Jun 8, 2020

Thank you for your inputs. I'll merge.

@jvdp1 jvdp1 merged commit 4fbb208 into fortran-lang:master Jun 8, 2020
@jvdp1 jvdp1 deleted the corr branch June 8, 2020 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants