-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18929][ML] Add Tweedie distribution in GLM #16344
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
Changes from 3 commits
952887e
4f184ec
7fe3910
bfcc4fb
a8feea7
233e2d3
17c5581
0b41825
6e8e607
8d7d34e
6da7e30
9a71e89
f461c09
a839c46
fab2652
651ea62
0310e85
6f4abeb
7b5d4cd
5a40073
a6fe665
83deee3
995e88f
54da2cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| /** | ||
| * Param for the name of family which is a description of the error distribution | ||
| * to be used in the model. | ||
| * Supported options: "gaussian", "binomial", "poisson" and "gamma". | ||
| * Supported options: "gaussian", "binomial", "poisson", "gamma" and "tweedie". | ||
| * Default is "gaussian". | ||
| * | ||
| * @group param | ||
|
|
@@ -63,6 +63,27 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| @Since("2.0.0") | ||
| def getFamily: String = $(family) | ||
|
|
||
| /** | ||
| * Param for the power in the variance function of the Tweedie distribution which provides | ||
| * the relationship between the variance and mean of the distribution. | ||
| * Used only for the tweedie family. | ||
| * (see <a href="https://en.wikipedia.org/wiki/Tweedie_distribution"> | ||
| * Tweedie Distribution (Wikipedia)</a>) | ||
| * Supported value: (1, 2) and (2, Inf). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Why we don't allow |
||
| * | ||
| * @group param | ||
| */ | ||
| @Since("2.2.0") | ||
| final val variancePower: Param[Double] = new Param(this, "variancePower", | ||
| "The power in the variance function of the Tweedie distribution which characterizes " + | ||
| "the relationship between the variance and mean of the distribution. " + | ||
| "Used only for the tweedie family. Supported value: (1, 2) and (2, Inf).", | ||
| (x: Double) => if (x > 1.0 && x != 2.0) true else false) | ||
|
||
|
|
||
| /** @group getParam */ | ||
| @Since("2.2.0") | ||
| def getVariancePower: Double = $(variancePower) | ||
|
|
||
| /** | ||
| * Param for the name of link function which provides the relationship | ||
| * between the linear predictor and the mean of the distribution function. | ||
|
|
@@ -108,8 +129,9 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| featuresDataType: DataType): StructType = { | ||
| if (isDefined(link)) { | ||
| require(supportedFamilyAndLinkPairs.contains( | ||
| Family.fromName($(family)) -> Link.fromName($(link))), "Generalized Linear Regression " + | ||
| s"with ${$(family)} family does not support ${$(link)} link function.") | ||
| Family.fromName($(family), $(variancePower)) -> Link.fromName($(link))), | ||
| s"Generalized Linear Regression with ${$(family)} family " + | ||
| s"does not support ${$(link)} link function.") | ||
| } | ||
| val newSchema = super.validateAndTransformSchema(schema, fitting, featuresDataType) | ||
| if (hasLinkPredictionCol) { | ||
|
|
@@ -128,13 +150,14 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| * Generalized linear model (Wikipedia)</a>) | ||
| * specified by giving a symbolic description of the linear | ||
| * predictor (link function) and a description of the error distribution (family). | ||
| * It supports "gaussian", "binomial", "poisson" and "gamma" as family. | ||
| * It supports "gaussian", "binomial", "poisson", "gamma" and "tweedie" as family. | ||
| * Valid link functions for each family is listed below. The first link function of each family | ||
| * is the default one. | ||
| * - "gaussian" : "identity", "log", "inverse" | ||
| * - "binomial" : "logit", "probit", "cloglog" | ||
| * - "poisson" : "log", "identity", "sqrt" | ||
| * - "gamma" : "inverse", "identity", "log" | ||
| * - "tweedie" : "identity", "log" | ||
|
||
| */ | ||
| @Experimental | ||
| @Since("2.0.0") | ||
|
|
@@ -157,6 +180,16 @@ class GeneralizedLinearRegression @Since("2.0.0") (@Since("2.0.0") override val | |
| def setFamily(value: String): this.type = set(family, value) | ||
| setDefault(family -> Gaussian.name) | ||
|
|
||
| /** | ||
| * Sets the value of param [[variancePower]]. | ||
| * Used only when family is "tweedie". | ||
| * | ||
| * @group setParam | ||
| */ | ||
| @Since("2.2.0") | ||
| def setVariancePower(value: Double): this.type = set(variancePower, value) | ||
| setDefault(variancePower -> 1.5) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why set the default value to 1.5, AFAIK, R set the default produces the same model with h2o.glm has the consistent default values with R, should we keep consistent with them?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. change default variancePower to 0.0, which will use Gaussian (with default identity link) |
||
|
|
||
| /** | ||
| * Sets the value of param [[link]]. | ||
| * | ||
|
|
@@ -242,7 +275,7 @@ class GeneralizedLinearRegression @Since("2.0.0") (@Since("2.0.0") override val | |
| def setLinkPredictionCol(value: String): this.type = set(linkPredictionCol, value) | ||
|
|
||
| override protected def train(dataset: Dataset[_]): GeneralizedLinearRegressionModel = { | ||
| val familyObj = Family.fromName($(family)) | ||
| val familyObj = Family.fromName($(family), $(variancePower)) | ||
|
||
| val linkObj = if (isDefined(link)) { | ||
| Link.fromName($(link)) | ||
| } else { | ||
|
|
@@ -306,7 +339,8 @@ object GeneralizedLinearRegression extends DefaultParamsReadable[GeneralizedLine | |
| Gaussian -> Identity, Gaussian -> Log, Gaussian -> Inverse, | ||
| Binomial -> Logit, Binomial -> Probit, Binomial -> CLogLog, | ||
| Poisson -> Log, Poisson -> Identity, Poisson -> Sqrt, | ||
| Gamma -> Inverse, Gamma -> Identity, Gamma -> Log | ||
| Gamma -> Inverse, Gamma -> Identity, Gamma -> Log, | ||
| Tweedie -> Identity, Tweedie -> Log | ||
| ) | ||
|
|
||
| /** Set of family names that GeneralizedLinearRegression supports. */ | ||
|
|
@@ -404,14 +438,17 @@ object GeneralizedLinearRegression extends DefaultParamsReadable[GeneralizedLine | |
| /** | ||
| * Gets the [[Family]] object from its name. | ||
| * | ||
| * @param name family name: "gaussian", "binomial", "poisson" or "gamma". | ||
| * @param name family name: "gaussian", "binomial", "poisson", "gamma" or "tweedie". | ||
| */ | ||
| def fromName(name: String): Family = { | ||
| def fromName(name: String, variancePower: Double): Family = { | ||
| name match { | ||
| case Gaussian.name => Gaussian | ||
| case Binomial.name => Binomial | ||
| case Poisson.name => Poisson | ||
| case Gamma.name => Gamma | ||
| case Tweedie.name => | ||
| Tweedie.variancePower = variancePower | ||
| Tweedie | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -591,6 +628,59 @@ object GeneralizedLinearRegression extends DefaultParamsReadable[GeneralizedLine | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tweedie exponential family distribution. | ||
| * The default link for the Tweedie family is the log link. | ||
| */ | ||
| private[regression] object Tweedie extends Family("tweedie") { | ||
|
|
||
| val defaultLink: Link = Log | ||
|
|
||
| var variancePower: Double = 1.5 | ||
|
||
|
|
||
| override def initialize(y: Double, weight: Double): Double = { | ||
| if (variancePower > 1.0 && variancePower < 2.0) { | ||
| require(y >= 0.0, "The response variable of the specified Tweedie distribution " + | ||
| s"should be non-negative, but got $y") | ||
| math.max(y, 0.1) | ||
|
||
| } else { | ||
| require(y > 0.0, "The response variable of the specified Tweedie distribution " + | ||
| s"should be non-negative, but got $y") | ||
| y | ||
| } | ||
| } | ||
|
|
||
| override def variance(mu: Double): Double = math.pow(mu, variancePower) | ||
|
|
||
| private def yp(y: Double, mu: Double, p: Double): Double = { | ||
| (math.pow(y, p) - math.pow(mu, p)) / p | ||
| } | ||
|
|
||
| // Force y >= 0.1 for deviance to work for (1 - variancePower). see tweedie()$dev.resid | ||
| override def deviance(y: Double, mu: Double, weight: Double): Double = { | ||
| 2.0 * weight * | ||
| (y * yp(math.max(y, 0.1), mu, 1.0 - variancePower) - yp(y, mu, 2.0 - variancePower)) | ||
| } | ||
|
|
||
| // This depends on the density of the tweedie distribution. Not yet implemented. | ||
| override def aic( | ||
| predictions: RDD[(Double, Double, Double)], | ||
| deviance: Double, | ||
| numInstances: Double, | ||
| weightSum: Double): Double = { | ||
| 0.0 | ||
|
||
| } | ||
|
|
||
| override def project(mu: Double): Double = { | ||
| if (mu < epsilon) { | ||
| epsilon | ||
| } else if (mu.isInfinity) { | ||
| Double.MaxValue | ||
|
||
| } else { | ||
| mu | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * A description of the link function to be used in the model. | ||
| * The link function provides the relationship between the linear predictor | ||
|
|
@@ -720,7 +810,7 @@ class GeneralizedLinearRegressionModel private[ml] ( | |
|
|
||
| import GeneralizedLinearRegression._ | ||
|
|
||
| private lazy val familyObj = Family.fromName($(family)) | ||
| private lazy val familyObj = Family.fromName($(family), $(variancePower)) | ||
| private lazy val linkObj = if (isDefined(link)) { | ||
| Link.fromName($(link)) | ||
| } else { | ||
|
|
@@ -905,7 +995,8 @@ class GeneralizedLinearRegressionSummary private[regression] ( | |
| */ | ||
| @Since("2.0.0") @transient val predictions: DataFrame = model.transform(dataset) | ||
|
|
||
| private[regression] lazy val family: Family = Family.fromName(model.getFamily) | ||
| private[regression] lazy val family: Family = | ||
| Family.fromName(model.getFamily, model.getVariancePower) | ||
| private[regression] lazy val link: Link = if (model.isDefined(model.link)) { | ||
| Link.fromName(model.getLink) | ||
| } else { | ||
|
|
@@ -1054,7 +1145,11 @@ class GeneralizedLinearRegressionSummary private[regression] ( | |
| case Row(label: Double, pred: Double, weight: Double) => | ||
| (label, pred, weight) | ||
| } | ||
| family.aic(t, deviance, numInstances, weightSum) + 2 * rank | ||
| if (model.getFamily == Tweedie.name) { | ||
| throw new UnsupportedOperationException("No AIC available for the tweedie family") | ||
| } else { | ||
| family.aic(t, deviance, numInstances, weightSum) + 2 * rank | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Nits: Param -> parameter, tweedie -> Tweedie (two lines below).
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.
changed tweedie. but other docs have been using Param..