-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8881444
RandomRDDGenerator: initial design
dorx 7cb0e40
fix for data inconsistency
dorx 49ed20d
alternative poisson distribution generator
dorx f46d928
WIP
dorx df5bcff
Merge branch 'generator' into randomRDD
dorx 92d6f1c
solution for Cloneable
dorx d56cacb
impl with RandomRDD
dorx bc90234
units passed.
dorx aec68eb
newline
dorx 063ea0b
Merge branch 'master' into randomRDD
dorx a8ea92d
Reviewer comments
dorx 6bf27d8
Merge branch 'master' into randomRDD
dorx 01121ac
reviewer comments
dorx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
mllib/src/main/scala/org/apache/spark/mllib/random/DistributionGenerator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 calledreseed
instead, butsetSeed
is also widely adopted.