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

A multivariate lognormal using MvNormal internally. #455

Merged
merged 1 commit into from
Feb 6, 2016

Conversation

KrisDM
Copy link
Contributor

@KrisDM KrisDM commented Jan 30, 2016

A port of my private MvLogNormal class to the Distributions package. Implemented the minimum interface described in the documentation and tested using similar tests as MvNormal.

Implements the same constructors as MvNormal.

No changes to the existing code from the Distributions package (other than includes and exports).

@KrisDM
Copy link
Contributor Author

KrisDM commented Feb 2, 2016

One squashed commit with code, tests, and documentation all in one.

This is new functionality (a new multivariate distribution) that doesn't touch existing code.

@kmsquire or @andreasnoack would you mind checking and merging?

@@ -207,6 +209,7 @@ export
sqmahal, # squared Mahalanobis distance to Gaussian center
sqmahal!, # inplace evaluation of sqmahal
location, # get the location parameter
location!, # provide storage for the location parameter (used in multivariate distribution mvlognormal)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little hesitant to export this without support for more distributions, but it does follow the pattern of many other functions defined here. Same for params! and scale! below.

Can you open an issue for defining these for other distributions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kmsquire I opened an issue (#459). The in-place variants would only be relevant to multivariate distributions, and the other multivariates that have been implemented to date may not have a location or scale, but I'll double-check it.

I also realized that location and scale should be defined for some univariate distributions that use that terminology, yet some are missing. I opened #460 for that.

@kmsquire
Copy link
Contributor

kmsquire commented Feb 3, 2016

LGTM. If there aren't any other comments, I'll merge in the next day or two.

@@ -44,6 +44,7 @@ export
ContinuousMultivariateDistribution,
ContinuousMatrixDistribution,
SufficientStats,
AbstractMvLogNormal,
Copy link
Member

Choose a reason for hiding this comment

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

I think this can wait until there'll be more subtypes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, will remove.

@andreasnoack
Copy link
Member

@KrisDM Generally a good PR. The documentation and tests are great. I'm in doubt about the special location! and scale! methods. They seem very specific to the log-normal. I haven't done any work with the log-normal. Are these conversion methods frequently used in applications?

I also know it is a convention for log-normals, but it also a bit weird that location and scale are the location and scale parameters for all distributions in in Distributions.jl (for which a method is defined) but the log-normal distribution.

Finally, another time please remove whitespace in a separate request. It adds a lot of noise to the diff.

@KrisDM
Copy link
Contributor Author

KrisDM commented Feb 4, 2016

@andreasnoack Thanks.

I'll remove the whitespace, will resubmit later this afternoon (I blame Juno with its default setting of 2 :-) - I have changed it now to 4).

location! and scale!. As pattern it follows sqmahal!, which I think is also defined for one distribution only (MvNormal).

As for the conversions itself:

  • For mean and cov, the conversions are commonly known (both wiki and MATLAB documentation define them), but I don't know how commonly they are used, given the lognormal itself has a rather small niche (mostly in ecology as I understand from the literature).
  • Centering the lognormal on the median (not on the location or mean) is what I've seen used in the MCMC ecology literature, so for that case we have to calculate the location from median and scale.
  • I decided to implement the mode as well, as I may want to try it at a later point.

An alternative solution to exporting location! and scale! would be to provide additional constructors, and supply an optional symbol to signal how to interpret the parameters (e.g., :location as the default, :meancov, :mean, :median and :mode for the ones relying on the conversions). I would then leave the internal _location! and _scale! functions to do the actual calculations, not export the location! and scale! functions, and leave the location and scale functions exposed for use cases that need a conversion without creating a distribution.

I agree it's ironic that location and scale were not defined for univariate LogNormal. Opened an issue to look into it.

Implements the same constructors as MvNormal, as well as the interface
discussed in the Distributions documentation.

No changes to the existing code from Distributions.

The tests are very similar to the ones for MvNormal itself.

Also contains functionality to calculate the scale and location for a lognormal given
some desired statistics for the distribution.

This functionality is needed in e.g., MCMC methods, where you need to
center a distribution around e.g. the median or mode.

In particular, there are functions that allow to calculate:
(1) location and scale for a given mean and covariance
(2) location for a given scale and either mean, median or mode

(location and scale cannot be calculated analytically from e.g., mode and
covariance)

The added scale and location functions are the equivalent of static class
functions in C++ (typed on ::Type{MvLogNormal}) to ensure correct
dispatch.

I added tests to test/mvlognormal.jl for all functionality

Added documentation for the multivariate lognormal distribution.

Fixing indentation and removed AbstractMvLogNormal from exported types.
@KrisDM
Copy link
Contributor Author

KrisDM commented Feb 5, 2016

I cleaned up the indentation and the AbstractMvLogNormal. Ready to go in my view...

kmsquire added a commit that referenced this pull request Feb 6, 2016
A multivariate lognormal using MvNormal internally.
@kmsquire kmsquire merged commit 3fa2830 into JuliaStats:master Feb 6, 2016
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.

4 participants