Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-2514] [mllib] Random RDD generator #1520

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.mllib.random

import cern.jet.random.Poisson
import cern.jet.random.engine.DRand

import org.apache.spark.annotation.Experimental
import org.apache.spark.util.random.{XORShiftRandom, Pseudorandom}

/**
* :: Experimental ::
* Trait for random number generators that generate i.i.d. values from a distribution.
*/
@Experimental
trait DistributionGenerator extends Pseudorandom with Serializable {

/**
* Returns an i.i.d. sample as a Double from an underlying distribution.
*/
def nextValue(): Double

/**
* Returns a copy of the DistributionGenerator with a new instance of the rng object used in the
* class when applicable for non-locking concurrent usage.
*/
def copy(): DistributionGenerator
}

/**
* :: Experimental ::
* Generates i.i.d. samples from U[0.0, 1.0]
*/
@Experimental
class UniformGenerator extends DistributionGenerator {

// XORShiftRandom for better performance. Thread safety isn't necessary here.
private val random = new XORShiftRandom()

override def nextValue(): Double = {
random.nextDouble()
}

override def setSeed(seed: Long) = random.setSeed(seed)

override def copy(): UniformGenerator = new UniformGenerator()
}

/**
* :: Experimental ::
* Generates i.i.d. samples from the standard normal distribution.
*/
@Experimental
class StandardNormalGenerator extends DistributionGenerator {

// XORShiftRandom for better performance. Thread safety isn't necessary here.
private val random = new XORShiftRandom()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it allowed to use a DistributionGenerator before calling setSeed? It would seem simpler to disallow that, but it seems to be something it got from trait Pseudorandom.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with most random objects, the DistributionGenerator should be created with a default seed (so using it before calling setSeed is legal). I like how in Colt it's called reseed instead, but setSeed is also widely adopted.


override def nextValue(): Double = {
random.nextGaussian()
}

override def setSeed(seed: Long) = random.setSeed(seed)

override def copy(): StandardNormalGenerator = new StandardNormalGenerator()
}

/**
* :: Experimental ::
* Generates i.i.d. samples from the Poisson distribution with the given mean.
*
* @param mean mean for the Poisson distribution.
*/
@Experimental
class PoissonGenerator(val mean: Double) extends DistributionGenerator {

private var rng = new Poisson(mean, new DRand)

override def nextValue(): Double = rng.nextDouble()

override def setSeed(seed: Long) {
rng = new Poisson(mean, new DRand(seed.toInt))
}

override def copy(): PoissonGenerator = new PoissonGenerator(mean)
}
Loading