commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alex Bertram (Commented) (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (MATH-764) New sample() API should accept RandomGenerator as parameter
Date Thu, 08 Mar 2012 09:34:59 GMT

    [ https://issues.apache.org/jira/browse/MATH-764?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13225109#comment-13225109
] 

Alex Bertram commented on MATH-764:
-----------------------------------

Hi Gilles,

Specifying the RandomGenerator is pretty important for my use case: I am working on the renjin
project, which is an implementation of the R language on the JVM that relies heavily on Commons
Math. We need to be able to generate random values for specific distributions using the random
number generator of the type and seed specified by the user.

What i'm proposing would actually promote immutability and functional-style programming: The
specific distribution classes would *not* implement Sampler, themselves, they would return
a functor that implements Sampler. 

Here's a few examples 

{code:java}

interface RealDistribution {
  
  /**
   * Factory method that returns a Sampler function object for this
   * distribution and the given {@code RandomGenerator}
   */
  RealSampler createSampler(RandomGenerator rng);
   
}

interface RealSampler {
  
  double sample();
  double[] sample(int sampleSize);
}

abstract class AbstractRealSampler implements RealSampler {

  RandomDataImpl randomData;
	
  AbstractRealSampler(RandomGenerator rng) {
    super();
    this.randomData = new RandomDataImpl(rng);
  }
	
  double[] sample(int sampleSize) {
    // loop impl
  }
    
  void reseedRandomGenerator(long seed) {
    randomData.reSeed();
  } 
}


class AbstractRealDistribution implements RealDistribution {

  RealSampler createSampler(RandomGenerator rng) {
    return new AbstractRealSampler(rng) {
      public double sample() { 
        return randomData.nextInversionDeviate(AbstractRealDistribution.this);
      }
    };
  }

  final RealSampler createSampler() {
    return createSampler(DefaultRandomGeneratorFactory.create());
  }
}

class ExponentialDistributionImpl extends AbstractRealDistribution {

  RealSampler createSampler(RandomGenerator rng) {
    return new AbstractSampler(rng) {
      double sample() {
        return randomData.nextExponential(); 
      }
    }
  }
}

{code}
                
> New sample() API should accept RandomGenerator as parameter
> -----------------------------------------------------------
>
>                 Key: MATH-764
>                 URL: https://issues.apache.org/jira/browse/MATH-764
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 3.0
>            Reporter: Alex Bertram
>   Original Estimate: 48h
>  Remaining Estimate: 48h
>
> This may come to late as I know the 3.0 release is nearing completion, but I had some
concerns about the new sample() method on the math3 RealDistribution interface. 
> Specifically, there doesn't seem to be a way to supply a random generator to the sampler.
Perhaps it would be better to have a factory method on the RealDistribution interface that
accepted a RandomGenerator and returns an instance of some new interface, Sampler, which contains
the sample() methods. 
> That is:
> interface RealDistribution {
>     Sampler createSampler(RandomGenerator generator);
>     Sample createSampler(); // uses default RandomGenerator
> }
> interface Sampler {
>     double sample();
>     double[] sample(int sampleSize);
> }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message