markrogoyski / math-php

Powerful modern math library for PHP: Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra
MIT License
2.32k stars 238 forks source link

Expand parameter checks to include member set. #374

Open Beakerboy opened 4 years ago

Beakerboy commented 4 years ago

Since we have both discrete and continuous distributions, where parameters can be members of the integer set, natural numbers, or real numbers, it would be nice to be able to specify this the same way we specify parameter limits. I propose that we make the parameter definition an array, and the second element can be the member set. We could even have fun with it and use the standard characters of ℕ, ℝ, and ℤ.

Beakerboy commented 4 years ago

In addition, would there be a simple enough way to define the limits of a support variable in terms of a parameter. In the Multivariate Hypergeometric distribution k ∈ [0,K]. I guess the constructor could create the ‘constant’.

markrogoyski commented 4 years ago

I like the idea. I'm not sure that we can't already do this though. Right now the limits check is a bounds check, high and low. If you have an int type declaration in your constructor and you define the bounds limit as (0,∞) then haven't you defined the set of natural numbers? int with no bounds is all integers. And float with no bounds is real numbers.

The use case I see that we cannot currently do is for some multivariate distribution like Hypergeometric where the constructor parameter is an array of an unspecified number of values. The PHP type system fails to help in this instance, although there are tricks you can do with the ... splat operator if you don't mind making your interface harder to deal with.

First steps: What is and isn't currently possible. What would we like to do. Then there is enough information to consider implementation options.

Beakerboy commented 4 years ago

You’re right about type hints on the ‘standard’ distributions. This popped in my head when looking at the Multivariate Hypergeometric, where I had to put in a bunch of if (!is_int($value)) nonsense.

markrogoyski commented 4 years ago

All the multivariate distributions seem to take in an array of parameters. It'd be nice to have a standard self-documenting mechanism for validating these like the univariate ones. And whatever the solution is, it could probably be generalized so it works with the univariate distributions as well.