{
  "id": 23322,
  "title": "AUC vs Matthews correlation coefficient",
  "url": "/competitions/bosch-production-line-performance/discussion/23322",
  "author_name": "Dalaska",
  "post_date": "2016-09-03T18:13:17.317000",
  "votes": 0,
  "comment_count": 12,
  "views": 2436,
  "content": "<p>Is it a good idea to first build a model with the best AUC score. \nThen do grid_search to find the best cut-off probability for the best Matthews correlation coefficient?</p>\n\n<p>Does a better AUC score always guarantee a better Matthews correlation coefficient?</p>",
  "messages": [
    {
      "id": 133804,
      "postDate": "2016-09-03T21:30:06.830Z",
      "content": "<p>I don't exactly get what you mean with that, but the special thing about MCC is, that it describes the confusion matrix regardless of the class sizes. Try it out:</p>\n\n<pre>TN &lt;-50\nTP &lt;-5000\nFN &lt;-8\nFP &lt;-123\n\n(TP * TN - FP * FN) / sqrt((TP+FP) * (TP+FN) * (FP+TN) * (TN+FN))</pre>\n\n<p>Versus:</p>\n\n<pre>TP &lt;-50\nTN &lt;-5000\nFP &lt;-8\nFN &lt;-123\n\n(TP * TN - FP * FN) / sqrt((TP+FP) * (TP+FN) * (FP+TN) * (TN+FN))</pre>",
      "rawMarkdown": "I don't exactly get what you mean with that, but the special thing about MCC is, that it describes the confusion matrix regardless of the class sizes. Try it out:\r\n\r\n<pre><code class=\"r\"><span class=\"identifier\">TN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">50</span>\r\n<span class=\"identifier\">TP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">5000</span>\r\n<span class=\"identifier\">FN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">8</span>\r\n<span class=\"identifier\">FP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">123</span>\r\n\r\n<span class=\"paren\">(</span><span class=\"identifier\">TP</span> <span class=\"operator\">*</span> <span class=\"identifier\">TN</span> <span class=\"operator\">-</span> <span class=\"identifier\">FP</span> <span class=\"operator\">*</span> <span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">/</span> <span class=\"identifier\">sqrt</span><span class=\"paren\">(</span><span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FP</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">FP</span><span class=\"operator\">+</span><span class=\"identifier\">TN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TN</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span><span class=\"paren\">)</span></code></pre>\r\n\r\nVersus:\r\n\r\n<pre><code class=\"r\"><span class=\"identifier\">TP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">50</span>\r\n<span class=\"identifier\">TN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">5000</span>\r\n<span class=\"identifier\">FP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">8</span>\r\n<span class=\"identifier\">FN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">123</span>\r\n\r\n<span class=\"paren\">(</span><span class=\"identifier\">TP</span> <span class=\"operator\">*</span> <span class=\"identifier\">TN</span> <span class=\"operator\">-</span> <span class=\"identifier\">FP</span> <span class=\"operator\">*</span> <span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">/</span> <span class=\"identifier\">sqrt</span><span class=\"paren\">(</span><span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FP</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">FP</span><span class=\"operator\">+</span><span class=\"identifier\">TN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TN</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span><span class=\"paren\">)</span></code></pre>\r\n\r\n",
      "votes": 1
    },
    {
      "id": 133792,
      "postDate": "2016-09-03T20:02:09.137Z",
      "content": "<p>1) Try and find out, you will learn in the process</p>\n\n<p>2) No</p>",
      "rawMarkdown": "1) Try and find out, you will learn in the process\r\n\r\n2) No",
      "votes": -1
    },
    {
      "id": 135047,
      "postDate": "2016-09-10T14:43:47.650Z",
      "content": "<p>mcc &lt;- function(preds, real) {\n  preds[preds&gt;0.5] &lt;- 1\n  preds[preds&lt;=0.5] &lt;- 0\n  TN &lt;- sum(real==0 &amp; preds==0)\n  TP &lt;- sum(real==1 &amp; preds==1)\n  FN &lt;- sum(real==0 &amp; preds==1)\n  FP &lt;- sum(real==1 &amp; preds==0)\n  N &lt;- TN+TP+FN+FP\n  S &lt;- (TP+FN)/N\n  P &lt;- (TP+FP)/N\n  mcc &lt;- as.numeric((TP/N-S*P) / (sqrt(P*S*(1-S)*(1-P))))\n  return(list(metric = &quot;error&quot;, value = mcc))\n}</p>",
      "rawMarkdown": "mcc <- function(preds, real) {\r\n  preds[preds>0.5] <- 1\r\n  preds[preds<=0.5] <- 0\r\n  TN <- sum(real==0 & preds==0)\r\n  TP <- sum(real==1 & preds==1)\r\n  FN <- sum(real==0 & preds==1)\r\n  FP <- sum(real==1 & preds==0)\r\n  N <- TN+TP+FN+FP\r\n  S <- (TP+FN)/N\r\n  P <- (TP+FP)/N\r\n  mcc <- as.numeric((TP/N-S*P) / (sqrt(P*S*(1-S)*(1-P))))\r\n  return(list(metric = \"error\", value = mcc))\r\n}"
    },
    {
      "id": 134880,
      "postDate": "2016-09-09T01:10:57.240Z",
      "content": "<p>@Jesse - Thanks for sharing your insights in to the metric. Much appreciated.</p>",
      "rawMarkdown": "@Jesse - Thanks for sharing your insights in to the metric. Much appreciated."
    },
    {
      "id": 133839,
      "postDate": "2016-09-04T00:00:35.427Z",
      "content": "<p>Sure, you have to optimize the threshhold to maximize MCC!</p>",
      "rawMarkdown": "Sure, you have to optimize the threshhold to maximize MCC!"
    },
    {
      "id": 133828,
      "postDate": "2016-09-03T23:00:05.307Z",
      "content": "<p>Hi Charly:\nThat's interesting. I didn't know that before.</p>\n\n<p>What I mean is if you predict in terms of probability instead of 0,1. You need to choose a threshold to get TP,TN, FP,FN.  You can tune the probability to get better Matthews correlation coefficient.</p>",
      "rawMarkdown": "Hi Charly:\r\nThat's interesting. I didn't know that before.\r\n\r\nWhat I mean is if you predict in terms of probability instead of 0,1. You need to choose a threshold to get TP,TN, FP,FN.  You can tune the probability to get better Matthews correlation coefficient."
    },
    {
      "id": 133798,
      "postDate": "2016-09-03T20:45:55.040Z",
      "content": "<p>[quote=NxGTR;133792]</p>\n\n<p>1) Try and find out, you will learn in the process</p>\n\n<p>2) No</p>\n\n<p>[/quote]</p>\n\n<p>Thanks NxGTR\nThe Matthews correlation coefficient is kind of weird. \nUsually when the training data size increases, the performance of the classifier increases. <br>\nBut Matthews correlation coefficient doesn't aways increase with the training data set. </p>",
      "rawMarkdown": "[quote=NxGTR;133792]\r\n\r\n1) Try and find out, you will learn in the process\r\n\r\n2) No\r\n\r\n[/quote]\r\n\r\nThanks NxGTR\r\nThe Matthews correlation coefficient is kind of weird. \r\nUsually when the training data size increases, the performance of the classifier increases.  \r\nBut Matthews correlation coefficient doesn't aways increase with the training data set. "
    },
    {
      "id": 133772,
      "postDate": "2016-09-03T18:13:17.317Z",
      "content": "<p>Is it a good idea to first build a model with the best AUC score. \nThen do grid_search to find the best cut-off probability for the best Matthews correlation coefficient?</p>\n\n<p>Does a better AUC score always guarantee a better Matthews correlation coefficient?</p>",
      "rawMarkdown": "Is it a good idea to first build a model with the best AUC score. \r\nThen do grid_search to find the best cut-off probability for the best Matthews correlation coefficient?\r\n\r\nDoes a better AUC score always guarantee a better Matthews correlation coefficient?"
    },
    {
      "id": 135068,
      "postDate": "2016-09-10T18:30:33.677Z",
      "rawMarkdown": "",
      "votes": 1,
      "isDeleted": true
    },
    {
      "id": 134891,
      "postDate": "2016-09-09T03:14:31.387Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 134874,
      "postDate": "2016-09-09T00:49:46.150Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 134546,
      "postDate": "2016-09-07T07:12:30.237Z",
      "rawMarkdown": "",
      "votes": 1,
      "isDeleted": true
    },
    {
      "id": 133983,
      "postDate": "2016-09-04T16:41:15.587Z",
      "rawMarkdown": "",
      "votes": 3,
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 133804,
      "author_name": "Charly",
      "author_url": "",
      "post_date": "2016-09-03T21:30:06.830000",
      "content": "<p>I don't exactly get what you mean with that, but the special thing about MCC is, that it describes the confusion matrix regardless of the class sizes. Try it out:</p>\n\n<pre>TN &lt;-50\nTP &lt;-5000\nFN &lt;-8\nFP &lt;-123\n\n(TP * TN - FP * FN) / sqrt((TP+FP) * (TP+FN) * (FP+TN) * (TN+FN))</pre>\n\n<p>Versus:</p>\n\n<pre>TP &lt;-50\nTN &lt;-5000\nFP &lt;-8\nFN &lt;-123\n\n(TP * TN - FP * FN) / sqrt((TP+FP) * (TP+FN) * (FP+TN) * (TN+FN))</pre>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 133792,
      "author_name": "NxGTR",
      "author_url": "",
      "post_date": "2016-09-03T20:02:09.137000",
      "content": "<p>1) Try and find out, you will learn in the process</p>\n\n<p>2) No</p>",
      "votes": -1,
      "replies": []
    },
    {
      "id": 135047,
      "author_name": "goodman",
      "author_url": "",
      "post_date": "2016-09-10T14:43:47.650000",
      "content": "<p>mcc &lt;- function(preds, real) {\n  preds[preds&gt;0.5] &lt;- 1\n  preds[preds&lt;=0.5] &lt;- 0\n  TN &lt;- sum(real==0 &amp; preds==0)\n  TP &lt;- sum(real==1 &amp; preds==1)\n  FN &lt;- sum(real==0 &amp; preds==1)\n  FP &lt;- sum(real==1 &amp; preds==0)\n  N &lt;- TN+TP+FN+FP\n  S &lt;- (TP+FN)/N\n  P &lt;- (TP+FP)/N\n  mcc &lt;- as.numeric((TP/N-S*P) / (sqrt(P*S*(1-S)*(1-P))))\n  return(list(metric = &quot;error&quot;, value = mcc))\n}</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 134880,
      "author_name": "inversion",
      "author_url": "",
      "post_date": "2016-09-09T01:10:57.240000",
      "content": "<p>@Jesse - Thanks for sharing your insights in to the metric. Much appreciated.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 133839,
      "author_name": "Charly",
      "author_url": "",
      "post_date": "2016-09-04T00:00:35.427000",
      "content": "<p>Sure, you have to optimize the threshhold to maximize MCC!</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 133828,
      "author_name": "Dalaska",
      "author_url": "",
      "post_date": "2016-09-03T23:00:05.307000",
      "content": "<p>Hi Charly:\nThat's interesting. I didn't know that before.</p>\n\n<p>What I mean is if you predict in terms of probability instead of 0,1. You need to choose a threshold to get TP,TN, FP,FN.  You can tune the probability to get better Matthews correlation coefficient.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 133798,
      "author_name": "Dalaska",
      "author_url": "",
      "post_date": "2016-09-03T20:45:55.040000",
      "content": "<p>[quote=NxGTR;133792]</p>\n\n<p>1) Try and find out, you will learn in the process</p>\n\n<p>2) No</p>\n\n<p>[/quote]</p>\n\n<p>Thanks NxGTR\nThe Matthews correlation coefficient is kind of weird. \nUsually when the training data size increases, the performance of the classifier increases. <br>\nBut Matthews correlation coefficient doesn't aways increase with the training data set. </p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 135068,
      "author_name": "",
      "author_url": "",
      "post_date": "2016-09-10T18:30:33.677000",
      "content": "",
      "votes": 1,
      "replies": []
    },
    {
      "id": 134891,
      "author_name": "",
      "author_url": "",
      "post_date": "2016-09-09T03:14:31.387000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 134874,
      "author_name": "",
      "author_url": "",
      "post_date": "2016-09-09T00:49:46.150000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 134546,
      "author_name": "",
      "author_url": "",
      "post_date": "2016-09-07T07:12:30.237000",
      "content": "",
      "votes": 1,
      "replies": []
    },
    {
      "id": 133983,
      "author_name": "",
      "author_url": "",
      "post_date": "2016-09-04T16:41:15.587000",
      "content": "",
      "votes": 3,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "133804": "I don't exactly get what you mean with that, but the special thing about MCC is, that it describes the confusion matrix regardless of the class sizes. Try it out:\r\n\r\n<pre><code class=\"r\"><span class=\"identifier\">TN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">50</span>\r\n<span class=\"identifier\">TP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">5000</span>\r\n<span class=\"identifier\">FN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">8</span>\r\n<span class=\"identifier\">FP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">123</span>\r\n\r\n<span class=\"paren\">(</span><span class=\"identifier\">TP</span> <span class=\"operator\">*</span> <span class=\"identifier\">TN</span> <span class=\"operator\">-</span> <span class=\"identifier\">FP</span> <span class=\"operator\">*</span> <span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">/</span> <span class=\"identifier\">sqrt</span><span class=\"paren\">(</span><span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FP</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">FP</span><span class=\"operator\">+</span><span class=\"identifier\">TN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TN</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span><span class=\"paren\">)</span></code></pre>\r\n\r\nVersus:\r\n\r\n<pre><code class=\"r\"><span class=\"identifier\">TP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">50</span>\r\n<span class=\"identifier\">TN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">5000</span>\r\n<span class=\"identifier\">FP</span> <span class=\"operator\">&lt;-</span><span class=\"number\">8</span>\r\n<span class=\"identifier\">FN</span> <span class=\"operator\">&lt;-</span><span class=\"number\">123</span>\r\n\r\n<span class=\"paren\">(</span><span class=\"identifier\">TP</span> <span class=\"operator\">*</span> <span class=\"identifier\">TN</span> <span class=\"operator\">-</span> <span class=\"identifier\">FP</span> <span class=\"operator\">*</span> <span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">/</span> <span class=\"identifier\">sqrt</span><span class=\"paren\">(</span><span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FP</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TP</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">FP</span><span class=\"operator\">+</span><span class=\"identifier\">TN</span><span class=\"paren\">)</span> <span class=\"operator\">*</span> <span class=\"paren\">(</span><span class=\"identifier\">TN</span><span class=\"operator\">+</span><span class=\"identifier\">FN</span><span class=\"paren\">)</span><span class=\"paren\">)</span></code></pre>\r\n\r\n",
    "133792": "1) Try and find out, you will learn in the process\r\n\r\n2) No",
    "135047": "mcc <- function(preds, real) {\r\n  preds[preds>0.5] <- 1\r\n  preds[preds<=0.5] <- 0\r\n  TN <- sum(real==0 & preds==0)\r\n  TP <- sum(real==1 & preds==1)\r\n  FN <- sum(real==0 & preds==1)\r\n  FP <- sum(real==1 & preds==0)\r\n  N <- TN+TP+FN+FP\r\n  S <- (TP+FN)/N\r\n  P <- (TP+FP)/N\r\n  mcc <- as.numeric((TP/N-S*P) / (sqrt(P*S*(1-S)*(1-P))))\r\n  return(list(metric = \"error\", value = mcc))\r\n}",
    "134880": "@Jesse - Thanks for sharing your insights in to the metric. Much appreciated.",
    "133839": "Sure, you have to optimize the threshhold to maximize MCC!",
    "133828": "Hi Charly:\r\nThat's interesting. I didn't know that before.\r\n\r\nWhat I mean is if you predict in terms of probability instead of 0,1. You need to choose a threshold to get TP,TN, FP,FN.  You can tune the probability to get better Matthews correlation coefficient.",
    "133798": "[quote=NxGTR;133792]\r\n\r\n1) Try and find out, you will learn in the process\r\n\r\n2) No\r\n\r\n[/quote]\r\n\r\nThanks NxGTR\r\nThe Matthews correlation coefficient is kind of weird. \r\nUsually when the training data size increases, the performance of the classifier increases.  \r\nBut Matthews correlation coefficient doesn't aways increase with the training data set. ",
    "133772": "Is it a good idea to first build a model with the best AUC score. \r\nThen do grid_search to find the best cut-off probability for the best Matthews correlation coefficient?\r\n\r\nDoes a better AUC score always guarantee a better Matthews correlation coefficient?",
    "135068": "",
    "134891": "",
    "134874": "",
    "134546": "",
    "133983": ""
  }
}