You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ori ziv <zi...@gmail.com> on 2012/07/15 13:36:56 UTC

[math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Hi,

I've wrote two basic but useful utilities for probability and tests
for them. I would like to ask if the developers of the Apache Commons
Math project are interested in me submitting them. I still have some
touching up to do so that they fit the style guidelines.

The first is isProbability(double)
which returns true if and only if x is a probability, i.e., not
smaller than zero and not bigger than one.
The other is isSampleSpace(double[]) which returns true if and only if
the values are a sample space, i.e., each value is a probability and
the sum of the values in the input array is one.

Prototype is attached.

Thanks,

Ori Ziv.


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Tue, Jul 17, 2012 at 02:04:43AM +0300, ori ziv wrote:
> * I updated the files (at
> http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip)
> * I added the checkProbability method . Do you think that the
> isProbability should not be public?
> * I added an exception with a customized message.
> * I didn't spot yet a place in CM that could use this is/check sample
> space function. I thought it could be useful for any method the gets
> probabilities which supposed to be a sample space. Don't you think
> this case is common?
> For example, in another class, InfoTheoryUtils, I've made a method
> that calculates the mutual information and one of it arguments is
> supposed to be a sample space.
> * Any other remarks?
> * Am I on the right track?

Thanks for your interest in contributing to Commons Math.
However, you might want to start by focusing on a specific issue.
E.g. this can be an improvement that is meant to simplify some of the code
(like "checkProbability"), or the correction of bug.

In most cases, you should provide a patch against the development version of
Commons Math which you can retrieve from here:
  https://svn.apache.org/repos/asf/commons/proper/math/trunk
This will enable the reviewers to check one thing at a time.

[Feel free to ask more questions if this not clear.]


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Tue, Jul 17, 2012 at 06:13:17PM +0300, ori ziv wrote:
> Don't you the following as disadvantages of the Preconditions way ?
> * It forces you to write "p >= 0 && p <= 1" anytime. even it is a
> trivial condition, you might mistake and get the inequality sign in
> the wrong way, or forget the equals sign.

That's a good point.

> * You are depended on guava, and I guess you can't do that on
> functions inside the CM.

Indeed, at this point, CM has no dependencies.


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by ori ziv <zi...@gmail.com>.
Don't you the following as disadvantages of the Preconditions way ?
* It forces you to write "p >= 0 && p <= 1" anytime. even it is a
trivial condition, you might mistake and get the inequality sign in
the wrong way, or forget the equals sign.
* You are depended on guava, and I guess you can't do that on
functions inside the CM.

On Tue, Jul 17, 2012 at 4:15 PM, Gilles Sadowski
<gi...@harfang.homelinux.org> wrote:
> On Mon, Jul 16, 2012 at 05:16:30PM -0700, Ted Dunning wrote:
>> I ask about trivial routines like isProbability().  Why is that any better
>> than just saying
>>
>>        Preconditions.checkArgument(p >= 0 && p <= 1);
>>
>> or checkState if it isn't an argument?
>>
>> I would argue that checkArgument is more transparent.
>
> I'd say that it's not better or worse; it's different. :-)
>
> If the purpose is to throw a basic exception, I agree that Preconditions is
> an elegant way, and is more concise (in vertical space) than an "if" block.
>
> However, we need to throw a specific exception and create a customized
> and localizable error message. This already exists in CM and should be
> modified. I don't think that it's worth it just to look like
> "Preconditions".
> In some place the effect and the look are already quite similar: E.g. line 55
> of src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator
>
> ---
> MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);
> ---
>
>
> Regards,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Mon, Jul 16, 2012 at 05:16:30PM -0700, Ted Dunning wrote:
> I ask about trivial routines like isProbability().  Why is that any better
> than just saying
> 
>        Preconditions.checkArgument(p >= 0 && p <= 1);
> 
> or checkState if it isn't an argument?
> 
> I would argue that checkArgument is more transparent.

I'd say that it's not better or worse; it's different. :-)

If the purpose is to throw a basic exception, I agree that Preconditions is
an elegant way, and is more concise (in vertical space) than an "if" block.

However, we need to throw a specific exception and create a customized
and localizable error message. This already exists in CM and should be
modified. I don't think that it's worth it just to look like
"Preconditions".
In some place the effect and the look are already quite similar: E.g. line 55
of src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator

---
MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);
---


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Ted Dunning <te...@gmail.com>.
I ask about trivial routines like isProbability().  Why is that any better
than just saying

       Preconditions.checkArgument(p >= 0 && p <= 1);

or checkState if it isn't an argument?

I would argue that checkArgument is more transparent.

On Mon, Jul 16, 2012 at 4:53 PM, Gilles Sadowski <
gilles@harfang.homelinux.org> wrote:

> Hi.
>
> On Mon, Jul 16, 2012 at 04:15:46PM -0700, Ted Dunning wrote:
> > Why is this not just a special case of what Preconditions in guava.
>
> What do you mean? And whom do you ask the question?
>
>
> Regards,
> Gilles
>
> > On Mon, Jul 16, 2012 at 4:04 PM, ori ziv <zi...@gmail.com> wrote:
> >
> > > * I updated the files (at
> > > http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip)
> > > * I added the checkProbability method . Do you think that the
> > > isProbability should not be public?
> > > * I added an exception with a customized message.
> > > * I didn't spot yet a place in CM that could use this is/check sample
> > > space function. I thought it could be useful for any method the gets
> > > probabilities which supposed to be a sample space. Don't you think
> > > this case is common?
> > > For example, in another class, InfoTheoryUtils, I've made a method
> > > that calculates the mutual information and one of it arguments is
> > > supposed to be a sample space.
> > > * Any other remarks?
> > > * Am I on the right track?
> > >
> > > On Mon, Jul 16, 2012 at 2:36 AM, ori ziv <zi...@gmail.com> wrote:
> > > > Alright. I'll update soon. Meanwhile the drafts are in
> > > > http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip
> > > >
> > > > On Mon, Jul 16, 2012 at 1:12 AM, Gilles Sadowski
> > > > <gi...@harfang.homelinux.org> wrote:
> > > >> On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
> > > >>> Hi,
> > > >>>
> > > >>> I've wrote two basic but useful utilities for probability and tests
> > > >>> for them. I would like to ask if the developers of the Apache
> Commons
> > > >>> Math project are interested in me submitting them. I still have
> some
> > > >>> touching up to do so that they fit the style guidelines.
> > > >>>
> > > >>> The first is isProbability(double)
> > > >>> which returns true if and only if x is a probability, i.e., not
> > > >>> smaller than zero and not bigger than one.
> > > >>
> > > >> I'd see something like that as useful within CM if it can replace
> this
> > > >> kind of code
> > > >> ---
> > > >>   if (p < 0 || p > 1) {
> > > >>     throw new OutOfRangeException(p, 0, 1);
> > > >>   }
> > > >> ---
> > > >>
> > > >> There are examples in "MathUtils" (e.g. "checkFinite").
> > > >> I.e. it should come with an exception that inherits from
> > > >> "OutOfRangeException" with a customized error message.
> > > >>
> > > >>> The other is isSampleSpace(double[]) which returns true if and
> only if
> > > >>> the values are a sample space, i.e., each value is a probability
> and
> > > >>> the sum of the values in the input array is one.
> > > >>
> > > >> Did you spot places in CM where this could be used?
> > > >>
> > > >>>
> > > >>> Prototype is attached.
> > > >>
> > > >> I think that attachements are stripped by the ML manager. :-/
> > > >>
> > > >>
> > > >> Regards,
> > > >> Gilles
> > > >>
> > > >>
> ---------------------------------------------------------------------
> > > >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > >> For additional commands, e-mail: dev-help@commons.apache.org
> > > >>
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

On Mon, Jul 16, 2012 at 04:15:46PM -0700, Ted Dunning wrote:
> Why is this not just a special case of what Preconditions in guava.

What do you mean? And whom do you ask the question?


Regards,
Gilles

> On Mon, Jul 16, 2012 at 4:04 PM, ori ziv <zi...@gmail.com> wrote:
> 
> > * I updated the files (at
> > http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip)
> > * I added the checkProbability method . Do you think that the
> > isProbability should not be public?
> > * I added an exception with a customized message.
> > * I didn't spot yet a place in CM that could use this is/check sample
> > space function. I thought it could be useful for any method the gets
> > probabilities which supposed to be a sample space. Don't you think
> > this case is common?
> > For example, in another class, InfoTheoryUtils, I've made a method
> > that calculates the mutual information and one of it arguments is
> > supposed to be a sample space.
> > * Any other remarks?
> > * Am I on the right track?
> >
> > On Mon, Jul 16, 2012 at 2:36 AM, ori ziv <zi...@gmail.com> wrote:
> > > Alright. I'll update soon. Meanwhile the drafts are in
> > > http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip
> > >
> > > On Mon, Jul 16, 2012 at 1:12 AM, Gilles Sadowski
> > > <gi...@harfang.homelinux.org> wrote:
> > >> On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
> > >>> Hi,
> > >>>
> > >>> I've wrote two basic but useful utilities for probability and tests
> > >>> for them. I would like to ask if the developers of the Apache Commons
> > >>> Math project are interested in me submitting them. I still have some
> > >>> touching up to do so that they fit the style guidelines.
> > >>>
> > >>> The first is isProbability(double)
> > >>> which returns true if and only if x is a probability, i.e., not
> > >>> smaller than zero and not bigger than one.
> > >>
> > >> I'd see something like that as useful within CM if it can replace this
> > >> kind of code
> > >> ---
> > >>   if (p < 0 || p > 1) {
> > >>     throw new OutOfRangeException(p, 0, 1);
> > >>   }
> > >> ---
> > >>
> > >> There are examples in "MathUtils" (e.g. "checkFinite").
> > >> I.e. it should come with an exception that inherits from
> > >> "OutOfRangeException" with a customized error message.
> > >>
> > >>> The other is isSampleSpace(double[]) which returns true if and only if
> > >>> the values are a sample space, i.e., each value is a probability and
> > >>> the sum of the values in the input array is one.
> > >>
> > >> Did you spot places in CM where this could be used?
> > >>
> > >>>
> > >>> Prototype is attached.
> > >>
> > >> I think that attachements are stripped by the ML manager. :-/
> > >>
> > >>
> > >> Regards,
> > >> Gilles
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > >> For additional commands, e-mail: dev-help@commons.apache.org
> > >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Ted Dunning <te...@gmail.com>.
Why is this not just a special case of what Preconditions in guava.

On Mon, Jul 16, 2012 at 4:04 PM, ori ziv <zi...@gmail.com> wrote:

> * I updated the files (at
> http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip)
> * I added the checkProbability method . Do you think that the
> isProbability should not be public?
> * I added an exception with a customized message.
> * I didn't spot yet a place in CM that could use this is/check sample
> space function. I thought it could be useful for any method the gets
> probabilities which supposed to be a sample space. Don't you think
> this case is common?
> For example, in another class, InfoTheoryUtils, I've made a method
> that calculates the mutual information and one of it arguments is
> supposed to be a sample space.
> * Any other remarks?
> * Am I on the right track?
>
> On Mon, Jul 16, 2012 at 2:36 AM, ori ziv <zi...@gmail.com> wrote:
> > Alright. I'll update soon. Meanwhile the drafts are in
> > http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip
> >
> > On Mon, Jul 16, 2012 at 1:12 AM, Gilles Sadowski
> > <gi...@harfang.homelinux.org> wrote:
> >> On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
> >>> Hi,
> >>>
> >>> I've wrote two basic but useful utilities for probability and tests
> >>> for them. I would like to ask if the developers of the Apache Commons
> >>> Math project are interested in me submitting them. I still have some
> >>> touching up to do so that they fit the style guidelines.
> >>>
> >>> The first is isProbability(double)
> >>> which returns true if and only if x is a probability, i.e., not
> >>> smaller than zero and not bigger than one.
> >>
> >> I'd see something like that as useful within CM if it can replace this
> >> kind of code
> >> ---
> >>   if (p < 0 || p > 1) {
> >>     throw new OutOfRangeException(p, 0, 1);
> >>   }
> >> ---
> >>
> >> There are examples in "MathUtils" (e.g. "checkFinite").
> >> I.e. it should come with an exception that inherits from
> >> "OutOfRangeException" with a customized error message.
> >>
> >>> The other is isSampleSpace(double[]) which returns true if and only if
> >>> the values are a sample space, i.e., each value is a probability and
> >>> the sum of the values in the input array is one.
> >>
> >> Did you spot places in CM where this could be used?
> >>
> >>>
> >>> Prototype is attached.
> >>
> >> I think that attachements are stripped by the ML manager. :-/
> >>
> >>
> >> Regards,
> >> Gilles
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by ori ziv <zi...@gmail.com>.
* I updated the files (at
http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip)
* I added the checkProbability method . Do you think that the
isProbability should not be public?
* I added an exception with a customized message.
* I didn't spot yet a place in CM that could use this is/check sample
space function. I thought it could be useful for any method the gets
probabilities which supposed to be a sample space. Don't you think
this case is common?
For example, in another class, InfoTheoryUtils, I've made a method
that calculates the mutual information and one of it arguments is
supposed to be a sample space.
* Any other remarks?
* Am I on the right track?

On Mon, Jul 16, 2012 at 2:36 AM, ori ziv <zi...@gmail.com> wrote:
> Alright. I'll update soon. Meanwhile the drafts are in
> http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip
>
> On Mon, Jul 16, 2012 at 1:12 AM, Gilles Sadowski
> <gi...@harfang.homelinux.org> wrote:
>> On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
>>> Hi,
>>>
>>> I've wrote two basic but useful utilities for probability and tests
>>> for them. I would like to ask if the developers of the Apache Commons
>>> Math project are interested in me submitting them. I still have some
>>> touching up to do so that they fit the style guidelines.
>>>
>>> The first is isProbability(double)
>>> which returns true if and only if x is a probability, i.e., not
>>> smaller than zero and not bigger than one.
>>
>> I'd see something like that as useful within CM if it can replace this
>> kind of code
>> ---
>>   if (p < 0 || p > 1) {
>>     throw new OutOfRangeException(p, 0, 1);
>>   }
>> ---
>>
>> There are examples in "MathUtils" (e.g. "checkFinite").
>> I.e. it should come with an exception that inherits from
>> "OutOfRangeException" with a customized error message.
>>
>>> The other is isSampleSpace(double[]) which returns true if and only if
>>> the values are a sample space, i.e., each value is a probability and
>>> the sum of the values in the input array is one.
>>
>> Did you spot places in CM where this could be used?
>>
>>>
>>> Prototype is attached.
>>
>> I think that attachements are stripped by the ML manager. :-/
>>
>>
>> Regards,
>> Gilles
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by ori ziv <zi...@gmail.com>.
Alright. I'll update soon. Meanwhile the drafts are in
http://dl.dropbox.com/u/4481581/commons-math-suggestions.zip

On Mon, Jul 16, 2012 at 1:12 AM, Gilles Sadowski
<gi...@harfang.homelinux.org> wrote:
> On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
>> Hi,
>>
>> I've wrote two basic but useful utilities for probability and tests
>> for them. I would like to ask if the developers of the Apache Commons
>> Math project are interested in me submitting them. I still have some
>> touching up to do so that they fit the style guidelines.
>>
>> The first is isProbability(double)
>> which returns true if and only if x is a probability, i.e., not
>> smaller than zero and not bigger than one.
>
> I'd see something like that as useful within CM if it can replace this
> kind of code
> ---
>   if (p < 0 || p > 1) {
>     throw new OutOfRangeException(p, 0, 1);
>   }
> ---
>
> There are examples in "MathUtils" (e.g. "checkFinite").
> I.e. it should come with an exception that inherits from
> "OutOfRangeException" with a customized error message.
>
>> The other is isSampleSpace(double[]) which returns true if and only if
>> the values are a sample space, i.e., each value is a probability and
>> the sum of the values in the input array is one.
>
> Did you spot places in CM where this could be used?
>
>>
>> Prototype is attached.
>
> I think that attachements are stripped by the ML manager. :-/
>
>
> Regards,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] basic utilities for probability - isProbability(double) and isSampleSpace(double[])

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Sun, Jul 15, 2012 at 02:36:56PM +0300, ori ziv wrote:
> Hi,
> 
> I've wrote two basic but useful utilities for probability and tests
> for them. I would like to ask if the developers of the Apache Commons
> Math project are interested in me submitting them. I still have some
> touching up to do so that they fit the style guidelines.
> 
> The first is isProbability(double)
> which returns true if and only if x is a probability, i.e., not
> smaller than zero and not bigger than one.

I'd see something like that as useful within CM if it can replace this
kind of code
---
  if (p < 0 || p > 1) {
    throw new OutOfRangeException(p, 0, 1);
  }
---

There are examples in "MathUtils" (e.g. "checkFinite").
I.e. it should come with an exception that inherits from
"OutOfRangeException" with a customized error message.

> The other is isSampleSpace(double[]) which returns true if and only if
> the values are a sample space, i.e., each value is a probability and
> the sum of the values in the input array is one.

Did you spot places in CM where this could be used?

> 
> Prototype is attached.

I think that attachements are stripped by the ML manager. :-/


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org