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

Implement Multivariate Normal Distribution with nalgebra #100

Merged
merged 24 commits into from Jan 9, 2020

Conversation

sphqxe
Copy link
Contributor

@sphqxe sphqxe commented Jan 2, 2020

This uses another crate "nalgebra-mvn" as a dependency, which is on an older version of nalgebra that depends on an older version of rand. I've opened a PR to update it to nalgebra v0.19, but for now Cargo.toml reflects that I'm compiling with a local version of nalgebra-mvn.

My first PR, please comment! :D

Copy link
Collaborator

@boxtown boxtown 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 submitting a PR! I think this is good work and it looks like the nalgebra crate is in a state where we can safely depend on it for linear algebra/matrix operations.

That being said, my preference would be to provide an implementation of the multivariate normal distribution from within statrs itself rather than depending on nalgebra-mvn. This will allow us to provide an API that's more consistent with the rest of the distributions in the crate and also enable us to evolve the API in a more consistent manner since we won't have to depend on how the distribution is implemented in nalgebra-mvn.

I'm open to counter-arguments and questions though if you have any thoughts!

@sphqxe
Copy link
Contributor Author

sphqxe commented Jan 3, 2020

Ok, if you prefer that I'll work on implementing the rest of the functionality within statrs and remove nalgebra-mvn as a dependency.

Copy link
Collaborator

@boxtown boxtown left a comment

Choose a reason for hiding this comment

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

This is excellent work! Only a couple things:

  1. Could we add some tests? You can probably draw from Math.Net Numerics as an example, most of the other distribution tests draw from Math.NET for all if not the majority of their tests

  2. I think from an API perspective it makes more sense to enforce f64 as the numerical type instead of Real since the rest of the distributions currently only support f64. I think it makes more sense to introduce support for generic numerical types across the board in a separate effort rather than have the API diverge in this PR. I'm definitely open to counter-arguments/discussion though.

Overall super appreciative of the contribution!

///
/// where `μ` is the mean, `inv(Σ)` is the precision matrix, `det(Σ)` is the determinant
/// of the covariance matrix, and `k` is the dimension of the distribution
fn ln_pdf(&self, x: VectorN<Real, N>) -> Real {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be expressed in terms of pdf?

@sphqxe
Copy link
Contributor Author

sphqxe commented Jan 7, 2020

  1. Sure I'll add some tests with reference to the Math.NET lib.

  2. That's true, and sampling from this distribution actually only supports f64 since it draws a vector of normals from the univariate distribution within this crate, I only realized that when I actually implemented sampling though. I'll make the changes so it only supports f64 for the rest of the APIs. I suppose having one less type parameter also reduces the complexity for users.

@sphqxe
Copy link
Contributor Author

sphqxe commented Jan 8, 2020

I'm just about done with this, let me know if you think I should add more test cases.

Copy link
Collaborator

@boxtown boxtown left a comment

Choose a reason for hiding this comment

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

Looks great! Appreciate the work, feel free to merge it when you're ready. I'll update the changelog

Cargo.toml Outdated
@@ -19,3 +19,5 @@ path = "src/lib.rs"

[dependencies]
rand = "0.7"
nalgebra = "0.19"
num-traits = "0.2.10"
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still need the explicit dependency on num-traits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, removed it.

@sphqxe
Copy link
Contributor Author

sphqxe commented Jan 8, 2020

Should be all done but I don't think I have permission to merge? Not quite sure how this works... XD

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.

None yet

2 participants