{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/91982251","pull_request_review_id":12491363,"id":91982251,"diff_hunk":"@@ -467,10 +469,12 @@ object GeneralizedLinearRegression extends DefaultParamsReadable[GeneralizedLine\n \n     override def variance(mu: Double): Double = mu * (1.0 - mu)\n \n+    private def ylogy(y: Double, mu: Double): Double = {\n+      if (y == 0) 0.0 else y * math.log(y / mu)\n+    }\n+\n     override def deviance(y: Double, mu: Double, weight: Double): Double = {","path":"mllib/src/main/scala/org/apache/spark/ml/regression/GeneralizedLinearRegression.scala","position":17,"original_position":17,"commit_id":"ece6eb232a6bdeef16d95f3aeb6ecaf91dd76283","original_commit_id":"ece6eb232a6bdeef16d95f3aeb6ecaf91dd76283","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"body":"Fair enough. It's a minor point and not worth delaying for, thanks for your feedback!","created_at":"2016-12-12T16:26:11Z","updated_at":"2016-12-12T16:26:11Z","html_url":"https://github.com/apache/spark/pull/16149#discussion_r91982251","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/16149","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/91982251"},"html":{"href":"https://github.com/apache/spark/pull/16149#discussion_r91982251"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/16149"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/16149","id":96585287,"html_url":"https://github.com/apache/spark/pull/16149","diff_url":"https://github.com/apache/spark/pull/16149.diff","patch_url":"https://github.com/apache/spark/pull/16149.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/16149","number":16149,"state":"open","locked":false,"title":"[SPARK-18715][ML]Fix AIC calculations in Binomial GLM","user":{"login":"actuaryzhang","id":11082368,"avatar_url":"https://avatars.githubusercontent.com/u/11082368?v=3","gravatar_id":"","url":"https://api.github.com/users/actuaryzhang","html_url":"https://github.com/actuaryzhang","followers_url":"https://api.github.com/users/actuaryzhang/followers","following_url":"https://api.github.com/users/actuaryzhang/following{/other_user}","gists_url":"https://api.github.com/users/actuaryzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/actuaryzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/actuaryzhang/subscriptions","organizations_url":"https://api.github.com/users/actuaryzhang/orgs","repos_url":"https://api.github.com/users/actuaryzhang/repos","events_url":"https://api.github.com/users/actuaryzhang/events{/privacy}","received_events_url":"https://api.github.com/users/actuaryzhang/received_events","type":"User","site_admin":false},"body":"The AIC calculation in Binomial GLM seems to be off when the response or weight is non-integer: the result is different from that in R. This issue arises when one models rates, i.e, num of successes normalized over num of trials, and uses num of trials as weights. In this case, the effective likelihood is  weight * label ~ binomial(weight, mu), where weight = number of trials, and weight * label = number of successes and mu = is the success rate.  \r\n\r\n@srowen @sethah @yanboliang @HyukjinKwon @zhengruifeng \r\n\r\n## What changes were proposed in this pull request?\r\nI suggest changing the current aic calculation for the Binomial family from \r\n```\r\n-2.0 * predictions.map { case (y: Double, mu: Double, weight: Double) =>\r\n        weight * dist.Binomial(1, mu).logProbabilityOf(math.round(y).toInt)\r\n      }.sum()\r\n```\r\nto the following which generalizes to the case of real-valued response and weights. \r\n```\r\n      -2.0 * predictions.map { case (y: Double, mu: Double, weight: Double) =>\r\n        val wt = math.round(weight).toInt\r\n        if (wt == 0){\r\n          0.0\r\n        } else {\r\n          dist.Binomial(wt, mu).logProbabilityOf(math.round(y * weight).toInt)\r\n        }\r\n      }.sum()\r\n``` \r\n## How was this patch tested?\r\nI will write the unit test once the community wants to include the proposed change. For now, the following modifies existing tests in weighted Binomial GLM to illustrate the issue. The second label is changed from 0 to 0.5. \r\n\r\n```\r\nval datasetWithWeight = Seq(\r\n    (1.0, 1.0, 0.0, 5.0),\r\n    (0.5, 2.0, 1.0, 2.0),\r\n    (1.0, 3.0, 2.0, 1.0),\r\n    (0.0, 4.0, 3.0, 3.0)\r\n  ).toDF(\"y\", \"w\", \"x1\", \"x2\")\r\n\r\nval formula = (new RFormula()\r\n  .setFormula(\"y ~ x1 + x2\")\r\n  .setFeaturesCol(\"features\")\r\n  .setLabelCol(\"label\"))\r\nval output = formula.fit(datasetWithWeight).transform(datasetWithWeight).select(\"features\", \"label\", \"w\")\r\n\r\nval glr = new GeneralizedLinearRegression()\r\n    .setFamily(\"binomial\")\r\n    .setWeightCol(\"w\")\r\n    .setFitIntercept(false)\r\n    .setRegParam(0)\r\n\r\nval model = glr.fit(output)\r\nmodel.summary.aic\r\n```\r\nThe AIC from Spark is 17.3227, and the AIC from R is 15.66454. ","created_at":"2016-12-05T18:41:10Z","updated_at":"2016-12-12T16:26:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1c116034e1c2150d833b0e12c21c7d85cc26fd4b","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/16149/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/16149/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/16149/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/ece6eb232a6bdeef16d95f3aeb6ecaf91dd76283","head":{"label":"actuaryzhang:aic","ref":"aic","sha":"ece6eb232a6bdeef16d95f3aeb6ecaf91dd76283","user":{"login":"actuaryzhang","id":11082368,"avatar_url":"https://avatars.githubusercontent.com/u/11082368?v=3","gravatar_id":"","url":"https://api.github.com/users/actuaryzhang","html_url":"https://github.com/actuaryzhang","followers_url":"https://api.github.com/users/actuaryzhang/followers","following_url":"https://api.github.com/users/actuaryzhang/following{/other_user}","gists_url":"https://api.github.com/users/actuaryzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/actuaryzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/actuaryzhang/subscriptions","organizations_url":"https://api.github.com/users/actuaryzhang/orgs","repos_url":"https://api.github.com/users/actuaryzhang/repos","events_url":"https://api.github.com/users/actuaryzhang/events{/privacy}","received_events_url":"https://api.github.com/users/actuaryzhang/received_events","type":"User","site_admin":false},"repo":{"id":72317900,"name":"spark","full_name":"actuaryzhang/spark","owner":{"login":"actuaryzhang","id":11082368,"avatar_url":"https://avatars.githubusercontent.com/u/11082368?v=3","gravatar_id":"","url":"https://api.github.com/users/actuaryzhang","html_url":"https://github.com/actuaryzhang","followers_url":"https://api.github.com/users/actuaryzhang/followers","following_url":"https://api.github.com/users/actuaryzhang/following{/other_user}","gists_url":"https://api.github.com/users/actuaryzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/actuaryzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/actuaryzhang/subscriptions","organizations_url":"https://api.github.com/users/actuaryzhang/orgs","repos_url":"https://api.github.com/users/actuaryzhang/repos","events_url":"https://api.github.com/users/actuaryzhang/events{/privacy}","received_events_url":"https://api.github.com/users/actuaryzhang/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/actuaryzhang/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/actuaryzhang/spark","forks_url":"https://api.github.com/repos/actuaryzhang/spark/forks","keys_url":"https://api.github.com/repos/actuaryzhang/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/actuaryzhang/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/actuaryzhang/spark/teams","hooks_url":"https://api.github.com/repos/actuaryzhang/spark/hooks","issue_events_url":"https://api.github.com/repos/actuaryzhang/spark/issues/events{/number}","events_url":"https://api.github.com/repos/actuaryzhang/spark/events","assignees_url":"https://api.github.com/repos/actuaryzhang/spark/assignees{/user}","branches_url":"https://api.github.com/repos/actuaryzhang/spark/branches{/branch}","tags_url":"https://api.github.com/repos/actuaryzhang/spark/tags","blobs_url":"https://api.github.com/repos/actuaryzhang/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/actuaryzhang/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/actuaryzhang/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/actuaryzhang/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/actuaryzhang/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/actuaryzhang/spark/languages","stargazers_url":"https://api.github.com/repos/actuaryzhang/spark/stargazers","contributors_url":"https://api.github.com/repos/actuaryzhang/spark/contributors","subscribers_url":"https://api.github.com/repos/actuaryzhang/spark/subscribers","subscription_url":"https://api.github.com/repos/actuaryzhang/spark/subscription","commits_url":"https://api.github.com/repos/actuaryzhang/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/actuaryzhang/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/actuaryzhang/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/actuaryzhang/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/actuaryzhang/spark/contents/{+path}","compare_url":"https://api.github.com/repos/actuaryzhang/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/actuaryzhang/spark/merges","archive_url":"https://api.github.com/repos/actuaryzhang/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/actuaryzhang/spark/downloads","issues_url":"https://api.github.com/repos/actuaryzhang/spark/issues{/number}","pulls_url":"https://api.github.com/repos/actuaryzhang/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/actuaryzhang/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/actuaryzhang/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/actuaryzhang/spark/labels{/name}","releases_url":"https://api.github.com/repos/actuaryzhang/spark/releases{/id}","deployments_url":"https://api.github.com/repos/actuaryzhang/spark/deployments","created_at":"2016-10-30T01:09:15Z","updated_at":"2016-10-30T01:09:39Z","pushed_at":"2016-12-11T05:40:44Z","git_url":"git://github.com/actuaryzhang/spark.git","ssh_url":"git@github.com:actuaryzhang/spark.git","clone_url":"https://github.com/actuaryzhang/spark.git","svn_url":"https://github.com/actuaryzhang/spark","homepage":null,"size":256360,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"410b7898661f77e748564aaee6a5ab7747ce34ad","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2016-12-12T13:10:19Z","pushed_at":"2016-12-12T15:52:28Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":622207,"stargazers_count":11046,"watchers_count":11046,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":10349,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":447,"forks":10349,"open_issues":447,"watchers":11046,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/16149"},"html":{"href":"https://github.com/apache/spark/pull/16149"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/16149"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/16149/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/16149/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/16149/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/ece6eb232a6bdeef16d95f3aeb6ecaf91dd76283"}}}}
{"actor":{"display_login":"sethah"}}