You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gilles Sadowski <gi...@harfang.homelinux.org> on 2010/09/29 13:36:36 UTC
[math] Problem with immutable distribution
Hi.
While making the distribution classes immutable, I'm encountering a problem.
In class "SimpleRegression" (package "stat.regression"), there is (at line
140):
---CUT---
if (n > 2) {
distribution.setDegreesOfFreedom(n - 2);
}
---CUT---
and, similarly, in class "TTestImpl" (package "stat.inference"), there is
(at line 961):
---CUT---
distribution.setDegreesOfFreedom(n - 1);
---CUT---
The method "setDegreesOfFreedom" does not exist anymore (it was depreceated
in 2.1).
I would have created a new instance e.g. for the first example above:
---CUT---
if (n > 2) {
distribution = new TDistributionImpl(n - 2);
}
---CUT---
but the problem is the existence of a setter for the "distribution" instance
variable:
---CUT---
public void setDistribution(TDistribution value) {
distribution = value;
// modify degrees of freedom
if (n > 2) {
distribution.setDegreesOfFreedom(n - 2);
}
}
---CUt---
So that, with the proposed change, the "distribution" variable will contain
a new reference (and even possibly an instance of another type).
Note that this setter is dangerous; it is the same problem as in issue 349:
https://issues.apache.org/jira/browse/MATH-349
Hence I'd remove the setter entirely.
What do you think?
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [math] Problem with immutable distribution
Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
Hi,
2010/9/29 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> >> Just to be sure: You propose to remove
>> >> public void setDistribution(TDistribution value)
>> >> Is this correct?
>> >
>> > Yes.
>> >
>> >> If so, another proposal - I'm not sure it's a clever one - is
>> >>
>> >> ---CUT---
>> >> public TDistribution setDistribution(TDistribution value) {
>> >> double n = value.getDegreesOfFreedom();
>> >> if (n > 2) {
>> >> n -= 2;
>> >> }
>> >>
>> >> final TDistribution newDistribution = new TDistributionImpl(n - 2);
>> >> distribution = newDistribution;
>> >> return newDistribution;
>> >> }
>> >> ---CUT---
>> >>
>> >> So that the new instance gets returned. As mentioned, I'm not sure
>> >> it's a good idea, but it would maintain the possibility to change
>> >> distribution. I think I'd prefer deleting the setDistribution-method
>> >> instead of this psudo-solution.
>> >
>> > There would remain the problem that, if "value" was instantiated with the
>> > other constructor:
>> > ---CUT---
>> > public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) {
>> > // ...
>> > }
>> > ---CUT---
>> > one could potentially have inconsistent behaviour since the
>> > "inverseCumAccuracy" parameter cannot be recovered (it is only accessible
>> > through a "protected" accessor method).
>> You're right. Didn't think of that.
>> >
>> >> TTestImpl: Would it be an idea to change the constructor to pass a
>> >> double degreeOfFreedom instead of a TDistribution?
>> >
>> > Yes, that would solve the problem.
>> What do you think of doing that?
>
> +1
>
>> It would of course not be as nice
>> OO-wise, but still, the classes (as in t-test and the t-distribution)
>> are quite fixed (t-test would probably continue to use the
>> t-distribution etc.).
>
> I was just wondering if anyone needed the flexibility to pass an instance of
> the distribution. In fact, does it even make sense to have several
> implementations of this interface?
In general, yes it does: if somebody wants to implement the cumProb
(or density, but that's not relevant for the t-test) in another way
than done by TDistributionImpl. By changing the constructor, we
disallow this and dictate the usage of the included TDistributionImpl
- but I suspect that it won't be a problem in practise. I can't come
up with a "real" reason why not to use (cumProb from)
TDistributionImpl, so I won't have a problem with dictating the usage
of TDistributionImpl when immutability is obtain in return.
Cheers,
Mikkel.
>
>
> Regards,
> Gilles
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [math] Problem with immutable distribution
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
> >> Just to be sure: You propose to remove
> >> public void setDistribution(TDistribution value)
> >> Is this correct?
> >
> > Yes.
> >
> >> If so, another proposal - I'm not sure it's a clever one - is
> >>
> >> ---CUT---
> >> public TDistribution setDistribution(TDistribution value) {
> >> double n = value.getDegreesOfFreedom();
> >> if (n > 2) {
> >> n -= 2;
> >> }
> >>
> >> final TDistribution newDistribution = new TDistributionImpl(n - 2);
> >> distribution = newDistribution;
> >> return newDistribution;
> >> }
> >> ---CUT---
> >>
> >> So that the new instance gets returned. As mentioned, I'm not sure
> >> it's a good idea, but it would maintain the possibility to change
> >> distribution. I think I'd prefer deleting the setDistribution-method
> >> instead of this psudo-solution.
> >
> > There would remain the problem that, if "value" was instantiated with the
> > other constructor:
> > ---CUT---
> > public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) {
> > // ...
> > }
> > ---CUT---
> > one could potentially have inconsistent behaviour since the
> > "inverseCumAccuracy" parameter cannot be recovered (it is only accessible
> > through a "protected" accessor method).
> You're right. Didn't think of that.
> >
> >> TTestImpl: Would it be an idea to change the constructor to pass a
> >> double degreeOfFreedom instead of a TDistribution?
> >
> > Yes, that would solve the problem.
> What do you think of doing that?
+1
> It would of course not be as nice
> OO-wise, but still, the classes (as in t-test and the t-distribution)
> are quite fixed (t-test would probably continue to use the
> t-distribution etc.).
I was just wondering if anyone needed the flexibility to pass an instance of
the distribution. In fact, does it even make sense to have several
implementations of this interface?
Regards,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [math] Problem with immutable distribution
Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
Hi,
2010/9/29 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> Just to be sure: You propose to remove
>> public void setDistribution(TDistribution value)
>> Is this correct?
>
> Yes.
>
>> If so, another proposal - I'm not sure it's a clever one - is
>>
>> ---CUT---
>> public TDistribution setDistribution(TDistribution value) {
>> double n = value.getDegreesOfFreedom();
>> if (n > 2) {
>> n -= 2;
>> }
>>
>> final TDistribution newDistribution = new TDistributionImpl(n - 2);
>> distribution = newDistribution;
>> return newDistribution;
>> }
>> ---CUT---
>>
>> So that the new instance gets returned. As mentioned, I'm not sure
>> it's a good idea, but it would maintain the possibility to change
>> distribution. I think I'd prefer deleting the setDistribution-method
>> instead of this psudo-solution.
>
> There would remain the problem that, if "value" was instantiated with the
> other constructor:
> ---CUT---
> public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) {
> // ...
> }
> ---CUT---
> one could potentially have inconsistent behaviour since the
> "inverseCumAccuracy" parameter cannot be recovered (it is only accessible
> through a "protected" accessor method).
You're right. Didn't think of that.
>
>> TTestImpl: Would it be an idea to change the constructor to pass a
>> double degreeOfFreedom instead of a TDistribution?
>
> Yes, that would solve the problem.
What do you think of doing that? It would of course not be as nice
OO-wise, but still, the classes (as in t-test and the t-distribution)
are quite fixed (t-test would probably continue to use the
t-distribution etc.).
>
>
> Best,
> Gilles
>
Cheers,
Mikkel.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [math] Problem with immutable distribution
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
> Just to be sure: You propose to remove
> public void setDistribution(TDistribution value)
> Is this correct?
Yes.
> If so, another proposal - I'm not sure it's a clever one - is
>
> ---CUT---
> public TDistribution setDistribution(TDistribution value) {
> double n = value.getDegreesOfFreedom();
> if (n > 2) {
> n -= 2;
> }
>
> final TDistribution newDistribution = new TDistributionImpl(n - 2);
> distribution = newDistribution;
> return newDistribution;
> }
> ---CUT---
>
> So that the new instance gets returned. As mentioned, I'm not sure
> it's a good idea, but it would maintain the possibility to change
> distribution. I think I'd prefer deleting the setDistribution-method
> instead of this psudo-solution.
There would remain the problem that, if "value" was instantiated with the
other constructor:
---CUT---
public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) {
// ...
}
---CUT---
one could potentially have inconsistent behaviour since the
"inverseCumAccuracy" parameter cannot be recovered (it is only accessible
through a "protected" accessor method).
> TTestImpl: Would it be an idea to change the constructor to pass a
> double degreeOfFreedom instead of a TDistribution?
Yes, that would solve the problem.
Best,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [math] Problem with immutable distribution
Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
Hi,
Just to be sure: You propose to remove
public void setDistribution(TDistribution value)
Is this correct?
If so, another proposal - I'm not sure it's a clever one - is
---CUT---
public TDistribution setDistribution(TDistribution value) {
double n = value.getDegreesOfFreedom();
if (n > 2) {
n -= 2;
}
final TDistribution newDistribution = new TDistributionImpl(n - 2);
distribution = newDistribution;
return newDistribution;
}
---CUT---
So that the new instance gets returned. As mentioned, I'm not sure
it's a good idea, but it would maintain the possibility to change
distribution. I think I'd prefer deleting the setDistribution-method
instead of this psudo-solution.
TTestImpl: Would it be an idea to change the constructor to pass a
double degreeOfFreedom instead of a TDistribution?
Cheers, Mikkel.
2010/9/29 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hi.
>
> While making the distribution classes immutable, I'm encountering a problem.
> In class "SimpleRegression" (package "stat.regression"), there is (at line
> 140):
> ---CUT---
> if (n > 2) {
> distribution.setDegreesOfFreedom(n - 2);
> }
> ---CUT---
> and, similarly, in class "TTestImpl" (package "stat.inference"), there is
> (at line 961):
> ---CUT---
> distribution.setDegreesOfFreedom(n - 1);
> ---CUT---
>
> The method "setDegreesOfFreedom" does not exist anymore (it was depreceated
> in 2.1).
> I would have created a new instance e.g. for the first example above:
> ---CUT---
> if (n > 2) {
> distribution = new TDistributionImpl(n - 2);
> }
> ---CUT---
> but the problem is the existence of a setter for the "distribution" instance
> variable:
> ---CUT---
> public void setDistribution(TDistribution value) {
> distribution = value;
>
> // modify degrees of freedom
> if (n > 2) {
> distribution.setDegreesOfFreedom(n - 2);
> }
> }
> ---CUt---
> So that, with the proposed change, the "distribution" variable will contain
> a new reference (and even possibly an instance of another type).
> Note that this setter is dangerous; it is the same problem as in issue 349:
> https://issues.apache.org/jira/browse/MATH-349
> Hence I'd remove the setter entirely.
>
> What do you think?
>
>
> 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