stan-dev / stan-dev/math

sample mean, covariance, and correlation functions

Open
#75 5 comments 0 reactions 1 assignee View on GitHub

@bob-carpenter is already working on this.

Since Jul 6, 2015.

feature good first issue new function
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

From @bob-carpenter on February 7, 2015 0:36

It'd be nice to have sample covariance and correlation functions.

matrix[K,K] cov(matrix[N,K] x);

matrix[K,K] corr(matrix[N,K] x);

along with a matching multivariate mean

vector[K] mean(matrix[N,K] x);

Should we have the following three signatures or maybe just the first two?

matrix[K,K] cov(vector[K] x[N]);  
matrix[K,K] cov(row_vector[K] x[N]);  
matrix[K,K] cov(real x[N,K]);

and

vector[K] mean(vector[K] x[N]);

I don't think there should be a mean returning a row vector --- I think it'd be confusing.

I'd suggest a simple implementation in stan/math based on Eigen::Matrix that we'd just autodiff through for var and fvar:

template <typename T>
Matrix<T,Dynamic,Dynamic> cov(const Matrix<T,Dynamic,Dynamic>& x);

template <typename T>
Matrix<T,Dynamic,Dynamic> cov(const std::vector<Matrix<T,Dynamic,1> >& x);

template <typename T>
Matrix<T,Dynamic,Dynamic> cov(const std::vector<Matrix<T,1,Dynamic> >& x);

The last two implementations could probably be combined, but I don't see how to combine the first one with a matrix input.

It'd be a bonus if the implementation of the function could be shared with the ones using the Welford algorithm in the chains analysis.

Another bonus would be not creating more vari than necessary in the autodiff implementations by taking advantage of the symmetry in the result.

Copied from original issue: stan-dev/stan#1284

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.