Bug 181 - Normal Variable Infinite Value & Bounds
Normal Variable Infinite Value & Bounds
Status: RESOLVED FIXED
Product: ns-3
Classification: Unclassified
Component: core
pre-release
All All
: P1 minor
Assigned To: ns-bugs
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2008-05-16 06:26 UTC by Joe Kopena
Modified: 2008-07-01 13:32 UTC (History)
1 user (show)

See Also:


Attachments
Minor patch (2.67 KB, patch)
2008-05-23 12:39 UTC, Joe Kopena
Details | Diff
Proposed (untested fix) (3.13 KB, patch)
2008-06-06 13:27 UTC, Rajib Bhattacharjea
Details | Diff
test case for above patch (653 bytes, text/x-c++src)
2008-06-06 14:36 UTC, Rajib Bhattacharjea
Details
updated to throw away values outside of range and "try again" (3.38 KB, patch)
2008-06-10 15:22 UTC, Rajib Bhattacharjea
Details | Diff
fixes the static generator (1.91 KB, patch)
2008-06-19 16:14 UTC, Rajib Bhattacharjea
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joe Kopena 2008-05-16 06:26:18 UTC
Two quick points:

- NormalVariable::INFINITE_VALUE is no longer defined.  NormalVariableImpl defines and uses a value, but NormalVariable declares and uses a static const INFINITE_VALUE to define some default values, without ever defining it.  It should be defined, or probably more correctly, the two methods that use it should be changed such that there is no default but instead one version w/ no third parameter, and another w/ the bounds parameter, so that the underlying implementation can work with defaults however it wants.

- I am not totally sure what this would do to the current algorithm, but it seems reasonable that the valid region should be mean +/- bound rather than -bound to bound.  This makes it more convenient, for example, to create a normal variable w/ distribution 2, 0.1 bounded by 1--3, i.e. to jitter some periodic packet generation.  Taking a glance, it seems like this should not affect the algorithm, but I am not an RNG expert.

Thx
Comment 1 Joe Kopena 2008-05-23 12:39:52 UTC
Created attachment 135 [details]
Minor patch
Comment 2 Joe Kopena 2008-05-23 12:40:45 UTC
Comment on attachment 135 [details]
Minor patch

This patch only addresses the INFINITE_VALUE issue.  The bounds issue needs feedback as its more about expectations & usability, so it has been left alone here.
Comment 3 Rajib Bhattacharjea 2008-05-23 14:02:59 UTC
(In reply to comment #2)
> (From update of attachment 135 [details] [edit])
> This patch only addresses the INFINITE_VALUE issue.  The bounds issue needs
> feedback as its more about expectations & usability, so it has been left alone
> here.
> 

The bounds setting API is probably non optimal.  If anything, the range should extend [mean-bound,mean+bound], that way the distribution mean and mode are preserved.  Asymmetric bounds causes the mean of the bounded distribution to be different from what you specified. 
Comment 4 Rajib Bhattacharjea 2008-06-06 13:27:33 UTC
Created attachment 156 [details]
Proposed (untested fix)

Subsumes Joe's patch.  Does symmetrical bounds, I'm just doing some quick histogram plots to verify.
Comment 5 Rajib Bhattacharjea 2008-06-06 14:36:06 UTC
Created attachment 157 [details]
test case for above patch

This is a test case which generates both unbounded and bounded versions of a Normal(10,2).  Histogram plots verify that it works at bounding the distribution symmetrically about the mean.  The tail of the bounded distribution is skewed however:
http://www.prism.gatech.edu/~gtg037s/files/normal-variable.eps

This is due to the way the code is forcing values outside of the range to be on the range boundary.  Ideally, the code would drop values outside of the range and "try again" so that the boundary didn't have this behavrior.

Honestly though, this API was never intended to do this.  I conceived that the bound would be set somewhere like 5 standard deviations away, so that this tail effect is minimized.  What do people think?
Comment 6 Mathieu Lacage 2008-06-10 12:38:54 UTC
(In reply to comment #5)
> Created an attachment (id=157) [edit]
> test case for above patch
> 
> This is a test case which generates both unbounded and bounded versions of a
> Normal(10,2).  Histogram plots verify that it works at bounding the
> distribution symmetrically about the mean.  The tail of the bounded
> distribution is skewed however:
> http://www.prism.gatech.edu/~gtg037s/files/normal-variable.eps
> 
> This is due to the way the code is forcing values outside of the range to be on
> the range boundary.  Ideally, the code would drop values outside of the range
> and "try again" so that the boundary didn't have this behavrior.
> 
> Honestly though, this API was never intended to do this.  I conceived that the
> bound would be set somewhere like 5 standard deviations away, so that this tail
> effect is minimized.  What do people think?

From the perspective of the user, I think that forcing values outside of the range to be on the edge of the range is actively harmful. Can't you just drop values outside of the range and retry (from within the random variable code) and return a value when it is within the range only ?
Comment 7 Rajib Bhattacharjea 2008-06-10 15:22:54 UTC
Created attachment 160 [details]
updated to throw away values outside of range and "try again"

This does what I described in my last comment.  It throws away values outside of the range and keeps trying until it can return a valid number:
http://www.prism.gatech.edu/~gtg037s/files/normal-variable.eps

Same test case, same normal 10,2 distribution.  I would push, but this needs some feedback from our new maintainer Michele Weigle.
Comment 8 Michele Weigle 2008-06-11 10:56:07 UTC
(In reply to comment #7)
> Created an attachment (id=160) [edit]
> updated to throw away values outside of range and "try again"
> 
> This does what I described in my last comment.  It throws away values outside
> of the range and keeps trying until it can return a valid number:
> http://www.prism.gatech.edu/~gtg037s/files/normal-variable.eps
> 
> Same test case, same normal 10,2 distribution.  I would push, but this needs
> some feedback from our new maintainer Michele Weigle.

This looks like a good solution to the issue.
Comment 9 Mathieu Lacage 2008-06-13 20:23:13 UTC
changeset: d0c70dbe918e
Comment 10 Joe Kopena 2008-06-18 09:45:14 UTC
Hi all,

The fix for the bounds that was applied is incorrect, or I am seriously confused.  It currently, quite frequently, generates values outside the bounds.  I am not sure if the original code had this behavior or not.  The below snippet will bomb pretty much every run.  I suspect that the readjustment of m_static_next if found out-of-bounds is incorrect, but have not investigated further.

Thx


  bool bomb = 0;
  double mean = 1.0, variance = 0.2, bound = 0.75;
  for (int x = 0; x < 50; x++) {
    double x = NormalVariable::GetSingleValue(mean,variance,bound);
    if (x < mean-bound || x > mean+bound) {
      bomb = true;
      std::cout << "** ";
    }
    std::cout << x << std::endl;
  }
  if (bomb)
    std::cout << std::endl << "BOMBED" << std::endl;
Comment 11 Mathieu Lacage 2008-06-18 15:23:27 UTC
(In reply to comment #10)
> Hi all,
> 
> The fix for the bounds that was applied is incorrect, or I am seriously
> confused.  It currently, quite frequently, generates values outside the bounds.
>  I am not sure if the original code had this behavior or not.  The below
> snippet will bomb pretty much every run.  I suspect that the readjustment of
> m_static_next if found out-of-bounds is incorrect, but have not investigated
> further.
> 
> Thx
> 
> 
>   bool bomb = 0;
>   double mean = 1.0, variance = 0.2, bound = 0.75;
>   for (int x = 0; x < 50; x++) {
>     double x = NormalVariable::GetSingleValue(mean,variance,bound);
>     if (x < mean-bound || x > mean+bound) {
>       bomb = true;
>       std::cout << "** ";
>     }
>     std::cout << x << std::endl;
>   }
>   if (bomb)
>     std::cout << std::endl << "BOMBED" << std::endl;


The previous patch did modify GetValue but not GetSingleValue.
> 

Comment 12 Rajib Bhattacharjea 2008-06-19 16:14:02 UTC
Created attachment 176 [details]
fixes the static generator

This doesn't "BOMB" on Joe's test case, and uses the same logic that the non-static variants use to generate bounded distributions.  Joe's distribution with 300000 trials:
http://www.prism.gatech.edu/~gtg037s/files/joe-test-case.eps

Joe please do one final review, and lets push this bad boy.
Comment 13 Joe Kopena 2008-06-19 16:50:06 UTC
This looks good to me.

For the record though, several of the other distributions do follow the previously discussed policy of simply taking the bound as the value if it is surpassed, rather than resampling (as NormalVariable now does).  Exponential, Pareto, and Weibull all do that.

But, I would at least include this patch.

Thx Raj
Comment 14 Craig Dowell 2008-06-19 17:51:56 UTC
Applied patch from 2008-06-19 16:14