{
  "id": 67206,
  "title": "Question on transforming back log1p predictions",
  "url": "/competitions/ga-customer-revenue-prediction/discussion/67206",
  "author_name": "",
  "post_date": "2018-09-30T01:51:24.045143200Z",
  "votes": 6,
  "comment_count": 8,
  "views": 0,
  "content": "<p>Hi all,</p>\n\n<p>I noticed that a lot of kernels were transforming the target to log1p(y) to fit a model, then transforming back using expm1. We are estimating E[log1p(y)|X] which is different than log1p(E[y|X]). I tried to take into account this difference by factoring my predictions by the arithmetic mean of the exponentiate residuals  of the log model (also known as Smearing estimate). It does something like this:\n$$\\widehat{y} = exp(\\widehat{log1p(y)}) \\times (\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})) - 1  \\\\ \\text{with} \\ e_{i} = log1p(y_{i}) - \\widehat{log1p(y_{i})},  \\ i=1...len(train)  $$</p>\n\n<p>I am missing something or not doing it right because my estimate (\\(\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})\\)) seems too high and blows off my predictions and my rmse CV. \nThat would be interesting if you had any thoughts on this, maybe it is just not adapted to non-linear models. Or maybe I am overthinking it... Thanks!</p>\n\n<p>Ideas:</p>\n\n<p><a href=\"https://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model\">https://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model</a>\n<a href=\"https://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation\">https://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation</a></p>",
  "messages": [
    {
      "id": "396079",
      "postDate": "09/30/2018 01:51:24",
      "content": "<p>Hi all,</p>\n\n<p>I noticed that a lot of kernels were transforming the target to log1p(y) to fit a model, then transforming back using expm1. We are estimating E[log1p(y)|X] which is different than log1p(E[y|X]). I tried to take into account this difference by factoring my predictions by the arithmetic mean of the exponentiate residuals  of the log model (also known as Smearing estimate). It does something like this:\n$$\\widehat{y} = exp(\\widehat{log1p(y)}) \\times (\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})) - 1  \\\\ \\text{with} \\ e_{i} = log1p(y_{i}) - \\widehat{log1p(y_{i})},  \\ i=1...len(train)  $$</p>\n\n<p>I am missing something or not doing it right because my estimate (\\(\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})\\)) seems too high and blows off my predictions and my rmse CV. \nThat would be interesting if you had any thoughts on this, maybe it is just not adapted to non-linear models. Or maybe I am overthinking it... Thanks!</p>\n\n<p>Ideas:</p>\n\n<p><a href=\"https://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model\">https://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model</a>\n<a href=\"https://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation\">https://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation</a></p>",
      "rawMarkdown": "Hi all,\n\nI noticed that a lot of kernels were transforming the target to log1p(y) to fit a model, then transforming back using expm1. We are estimating E[log1p(y)|X] which is different than log1p(E[y|X]). I tried to take into account this difference by factoring my predictions by the arithmetic mean of the exponentiate residuals  of the log model (also known as Smearing estimate). It does something like this:\n$$\\widehat{y} = exp(\\widehat{log1p(y)}) \\times (\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})) - 1  \\\\\\ \\text{with} \\ e_{i} = log1p(y_{i}) - \\widehat{log1p(y_{i})},  \\ i=1...len(train)  $$\n\n\nI am missing something or not doing it right because my estimate (\\\\(\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})\\\\)) seems too high and blows off my predictions and my rmse CV. \nThat would be interesting if you had any thoughts on this, maybe it is just not adapted to non-linear models. Or maybe I am overthinking it... Thanks!\n\nIdeas:\n\nhttps://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model\nhttps://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation",
      "votes": null
    },
    {
      "id": "396095",
      "postDate": "09/30/2018 02:35:45",
      "content": "<p>Two things:</p>\n\n<ol>\n<li>According to <a href=\"https://en.wikipedia.org/wiki/Smearing_retransformation\">https://en.wikipedia.org/wiki/Smearing_retransformation</a> , this retransformation is only valid if <code>log1p(y)</code> is normally distributed, but this is not the case, the data is severely right skewed.</li>\n<li>Even if it were distributed normally, you aren't using the formula given in that Wikipedia article. it says to use <code>exp(1/2 * sigma^2) = exp(1/(2*(N - 1)) * \\Sigma (yhat - mean(yhat))^2) = exp(1/2 * var(yhat))</code> but this is not what you are multiplying by. What if you change to</li>\n</ol>\n\n<p>$$\\exp{(\\mathrm{log1p}(y))} * \\exp{(\\frac{1}{2} \\mathrm{var}(y))} - 1$$</p>\n\n<p>Please reply back letting me know if predictions worsened, improved, or stayed the same..</p>",
      "rawMarkdown": "Two things:\n\n1. According to https://en.wikipedia.org/wiki/Smearing_retransformation , this retransformation is only valid if `log1p(y)` is normally distributed, but this is not the case, the data is severely right skewed.\n2. Even if it were distributed normally, you aren't using the formula given in that Wikipedia article. it says to use `exp(1/2 * sigma^2) = exp(1/(2*(N - 1)) * \\Sigma (yhat - mean(yhat))^2) = exp(1/2 * var(yhat))` but this is not what you are multiplying by. What if you change to\n\n$$\\exp{(\\mathrm{log1p}(y))} * \\exp{(\\frac{1}{2} \\mathrm{var}(y))} - 1$$\n\nPlease reply back letting me know if predictions worsened, improved, or stayed the same..",
      "votes": null
    },
    {
      "id": "396099",
      "postDate": "09/30/2018 02:46:36",
      "content": "<p>anyway i dont think this is the way the competition should be attempted, because it implies that you are log transforming everything at the transaction level, and then summing those predictions to an answer, as opposed to making 1 prediction for the aggregate revenue. I'm not trying to deter you, it's just that if your transaction prediction has an error <code>epsilon</code>, then if a user has <code>n</code> visits to the website, your total error in the sum is bounded by <code>n * epsilon</code>, but if you predict at an aggregate level your error can be <code>tau</code>, and it is likely that there exists a threshold amount of visits <code>N</code> such that for all <code>n &gt; N</code>, we have <code>tau &lt; n * epsilon</code>. Not trying to steer you away from a transaction-level model, but you need to keep this in mind, which in my mind makes that model unfavorable</p>",
      "rawMarkdown": "anyway i dont think this is the way the competition should be attempted, because it implies that you are log transforming everything at the transaction level, and then summing those predictions to an answer, as opposed to making 1 prediction for the aggregate revenue. I'm not trying to deter you, it's just that if your transaction prediction has an error `epsilon`, then if a user has `n` visits to the website, your total error in the sum is bounded by `n * epsilon`, but if you predict at an aggregate level your error can be `tau`, and it is likely that there exists a threshold amount of visits `N` such that for all `n &gt; N`, we have `tau &lt; n * epsilon`. Not trying to steer you away from a transaction-level model, but you need to keep this in mind, which in my mind makes that model unfavorable",
      "votes": null
    },
    {
      "id": "396127",
      "postDate": "09/30/2018 03:47:39",
      "content": "<p>Hey Corey,  very good points thanks. I am not exactly using the transformation stated on the wiki page but a non-parametric estimate (please  refer to 1st link, I did not find an open source version but the paper is Smearing Estimate: A Nonparametric Retransformation Method ). I totally agree that the log1p(y) may be too skewed to satisfy the assumption of normality. Anyway, I just tried with \\(exp(\\frac{1}{2}var(y)\\)) and it worsen my cv by 0.1. I agree that training at transaction level may not be optimal but I am still trying both approaches!</p>",
      "rawMarkdown": "Hey Corey,  very good points thanks. I am not exactly using the transformation stated on the wiki page but a non-parametric estimate (please  refer to 1st link, I did not find an open source version but the paper is Smearing Estimate: A Nonparametric Retransformation Method ). I totally agree that the log1p(y) may be too skewed to satisfy the assumption of normality. Anyway, I just tried with \\\\(exp(\\frac{1}{2}var(y)\\\\)) and it worsen my cv by 0.1. I agree that training at transaction level may not be optimal but I am still trying both approaches!",
      "votes": null
    },
    {
      "id": "396131",
      "postDate": "09/30/2018 04:05:19",
      "content": "<p>Maybe run an optimizer function to find out what value <code>c</code> minimizes RMSE(exp(yhat)*c - 1), and from there it's possible to discover the formula for <code>c</code>...but this could be overfit to trainng set at this point. I dont know, but I think your Discussion Topic is extremely important. Let me know if you make progress on it.</p>",
      "rawMarkdown": "Maybe run an optimizer function to find out what value `c` minimizes RMSE(exp(yhat)*c - 1), and from there it's possible to discover the formula for `c`...but this could be overfit to trainng set at this point. I dont know, but I think your Discussion Topic is extremely important. Let me know if you make progress on it.",
      "votes": null
    },
    {
      "id": "396536",
      "postDate": "10/01/2018 01:16:09",
      "content": "<p>Great point,  looking forward to further discussion on this. Would such a transformation apply to decision trees ? </p>",
      "rawMarkdown": "Great point,  looking forward to further discussion on this. Would such a transformation apply to decision trees ?",
      "votes": null
    },
    {
      "id": "396571",
      "postDate": "10/01/2018 02:40:27",
      "content": "<p>I have same question, because for a tree based algorithms we can't write \nYhat = (theta)*(input) + error.</p>\n\n<p>This is the assumption only for linear regression. So I don't think it is applicable to tree based methods. Even the adri says that 1/N * sum (exp(error)) is very high. I think this will not happen in case of linear regression as we assume that error terms have mean zero.</p>",
      "rawMarkdown": "I have same question, because for a tree based algorithms we can't write \nYhat = (theta)*(input) + error.\n\nThis is the assumption only for linear regression. So I don't think it is applicable to tree based methods. Even the adri says that 1/N * sum (exp(error)) is very high. I think this will not happen in case of linear regression as we assume that error terms have mean zero.",
      "votes": null
    },
    {
      "id": "396625",
      "postDate": "10/01/2018 05:13:42",
      "content": "<p>Yes, I have the same concerns</p>",
      "rawMarkdown": "Yes, I have the same concerns",
      "votes": null
    },
    {
      "id": "396640",
      "postDate": "10/01/2018 05:40:49",
      "content": "<p>So adri  I saw for some examples the actual revenue is 0 but the predicted value was near 20. So when you take the exp(20) it will be huge. So I think it's better for tree based models, you directly take the exp of predicted value and ignore that error term there.</p>",
      "rawMarkdown": "So adri  I saw for some examples the actual revenue is 0 but the predicted value was near 20. So when you take the exp(20) it will be huge. So I think it's better for tree based models, you directly take the exp of predicted value and ignore that error term there.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 396095,
      "author_name": "returnofsputnik",
      "author_url": "",
      "post_date": "09/30/2018 02:35:45",
      "content": "<p>Two things:</p>\n\n<ol>\n<li>According to <a href=\"https://en.wikipedia.org/wiki/Smearing_retransformation\">https://en.wikipedia.org/wiki/Smearing_retransformation</a> , this retransformation is only valid if <code>log1p(y)</code> is normally distributed, but this is not the case, the data is severely right skewed.</li>\n<li>Even if it were distributed normally, you aren't using the formula given in that Wikipedia article. it says to use <code>exp(1/2 * sigma^2) = exp(1/(2*(N - 1)) * \\Sigma (yhat - mean(yhat))^2) = exp(1/2 * var(yhat))</code> but this is not what you are multiplying by. What if you change to</li>\n</ol>\n\n<p>$$\\exp{(\\mathrm{log1p}(y))} * \\exp{(\\frac{1}{2} \\mathrm{var}(y))} - 1$$</p>\n\n<p>Please reply back letting me know if predictions worsened, improved, or stayed the same..</p>",
      "votes": null,
      "replies": [
        {
          "id": 396099,
          "author_name": "returnofsputnik",
          "author_url": "",
          "post_date": "09/30/2018 02:46:36",
          "content": "<p>anyway i dont think this is the way the competition should be attempted, because it implies that you are log transforming everything at the transaction level, and then summing those predictions to an answer, as opposed to making 1 prediction for the aggregate revenue. I'm not trying to deter you, it's just that if your transaction prediction has an error <code>epsilon</code>, then if a user has <code>n</code> visits to the website, your total error in the sum is bounded by <code>n * epsilon</code>, but if you predict at an aggregate level your error can be <code>tau</code>, and it is likely that there exists a threshold amount of visits <code>N</code> such that for all <code>n &gt; N</code>, we have <code>tau &lt; n * epsilon</code>. Not trying to steer you away from a transaction-level model, but you need to keep this in mind, which in my mind makes that model unfavorable</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 396127,
          "author_name": "tita1708",
          "author_url": "",
          "post_date": "09/30/2018 03:47:39",
          "content": "<p>Hey Corey,  very good points thanks. I am not exactly using the transformation stated on the wiki page but a non-parametric estimate (please  refer to 1st link, I did not find an open source version but the paper is Smearing Estimate: A Nonparametric Retransformation Method ). I totally agree that the log1p(y) may be too skewed to satisfy the assumption of normality. Anyway, I just tried with \\(exp(\\frac{1}{2}var(y)\\)) and it worsen my cv by 0.1. I agree that training at transaction level may not be optimal but I am still trying both approaches!</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 396131,
          "author_name": "returnofsputnik",
          "author_url": "",
          "post_date": "09/30/2018 04:05:19",
          "content": "<p>Maybe run an optimizer function to find out what value <code>c</code> minimizes RMSE(exp(yhat)*c - 1), and from there it's possible to discover the formula for <code>c</code>...but this could be overfit to trainng set at this point. I dont know, but I think your Discussion Topic is extremely important. Let me know if you make progress on it.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 396536,
      "author_name": "xavierbourretsicotte",
      "author_url": "",
      "post_date": "10/01/2018 01:16:09",
      "content": "<p>Great point,  looking forward to further discussion on this. Would such a transformation apply to decision trees ? </p>",
      "votes": null,
      "replies": [
        {
          "id": 396571,
          "author_name": "suchith0312",
          "author_url": "",
          "post_date": "10/01/2018 02:40:27",
          "content": "<p>I have same question, because for a tree based algorithms we can't write \nYhat = (theta)*(input) + error.</p>\n\n<p>This is the assumption only for linear regression. So I don't think it is applicable to tree based methods. Even the adri says that 1/N * sum (exp(error)) is very high. I think this will not happen in case of linear regression as we assume that error terms have mean zero.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 396625,
          "author_name": "tita1708",
          "author_url": "",
          "post_date": "10/01/2018 05:13:42",
          "content": "<p>Yes, I have the same concerns</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 396640,
          "author_name": "suchith0312",
          "author_url": "",
          "post_date": "10/01/2018 05:40:49",
          "content": "<p>So adri  I saw for some examples the actual revenue is 0 but the predicted value was near 20. So when you take the exp(20) it will be huge. So I think it's better for tree based models, you directly take the exp of predicted value and ignore that error term there.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "396079": "Hi all,\n\nI noticed that a lot of kernels were transforming the target to log1p(y) to fit a model, then transforming back using expm1. We are estimating E[log1p(y)|X] which is different than log1p(E[y|X]). I tried to take into account this difference by factoring my predictions by the arithmetic mean of the exponentiate residuals  of the log model (also known as Smearing estimate). It does something like this:\n$$\\widehat{y} = exp(\\widehat{log1p(y)}) \\times (\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})) - 1  \\\\\\ \\text{with} \\ e_{i} = log1p(y_{i}) - \\widehat{log1p(y_{i})},  \\ i=1...len(train)  $$\n\n\nI am missing something or not doing it right because my estimate (\\\\(\\frac{1}{N}\\sum_{i=1}^{N} exp(e_{i})\\\\)) seems too high and blows off my predictions and my rmse CV. \nThat would be interesting if you had any thoughts on this, maybe it is just not adapted to non-linear models. Or maybe I am overthinking it... Thanks!\n\nIdeas:\n\nhttps://stats.stackexchange.com/questions/55692/back-transformation-of-an-mlr-model\nhttps://stats.stackexchange.com/questions/260942/transforming-back-after-a-log-transformation",
    "396095": "Two things:\n\n1. According to https://en.wikipedia.org/wiki/Smearing_retransformation , this retransformation is only valid if `log1p(y)` is normally distributed, but this is not the case, the data is severely right skewed.\n2. Even if it were distributed normally, you aren't using the formula given in that Wikipedia article. it says to use `exp(1/2 * sigma^2) = exp(1/(2*(N - 1)) * \\Sigma (yhat - mean(yhat))^2) = exp(1/2 * var(yhat))` but this is not what you are multiplying by. What if you change to\n\n$$\\exp{(\\mathrm{log1p}(y))} * \\exp{(\\frac{1}{2} \\mathrm{var}(y))} - 1$$\n\nPlease reply back letting me know if predictions worsened, improved, or stayed the same..",
    "396099": "anyway i dont think this is the way the competition should be attempted, because it implies that you are log transforming everything at the transaction level, and then summing those predictions to an answer, as opposed to making 1 prediction for the aggregate revenue. I'm not trying to deter you, it's just that if your transaction prediction has an error `epsilon`, then if a user has `n` visits to the website, your total error in the sum is bounded by `n * epsilon`, but if you predict at an aggregate level your error can be `tau`, and it is likely that there exists a threshold amount of visits `N` such that for all `n &gt; N`, we have `tau &lt; n * epsilon`. Not trying to steer you away from a transaction-level model, but you need to keep this in mind, which in my mind makes that model unfavorable",
    "396127": "Hey Corey,  very good points thanks. I am not exactly using the transformation stated on the wiki page but a non-parametric estimate (please  refer to 1st link, I did not find an open source version but the paper is Smearing Estimate: A Nonparametric Retransformation Method ). I totally agree that the log1p(y) may be too skewed to satisfy the assumption of normality. Anyway, I just tried with \\\\(exp(\\frac{1}{2}var(y)\\\\)) and it worsen my cv by 0.1. I agree that training at transaction level may not be optimal but I am still trying both approaches!",
    "396131": "Maybe run an optimizer function to find out what value `c` minimizes RMSE(exp(yhat)*c - 1), and from there it's possible to discover the formula for `c`...but this could be overfit to trainng set at this point. I dont know, but I think your Discussion Topic is extremely important. Let me know if you make progress on it.",
    "396536": "Great point,  looking forward to further discussion on this. Would such a transformation apply to decision trees ?",
    "396571": "I have same question, because for a tree based algorithms we can't write \nYhat = (theta)*(input) + error.\n\nThis is the assumption only for linear regression. So I don't think it is applicable to tree based methods. Even the adri says that 1/N * sum (exp(error)) is very high. I think this will not happen in case of linear regression as we assume that error terms have mean zero.",
    "396625": "Yes, I have the same concerns",
    "396640": "So adri  I saw for some examples the actual revenue is 0 but the predicted value was near 20. So when you take the exp(20) it will be huge. So I think it's better for tree based models, you directly take the exp of predicted value and ignore that error term there."
  },
  "source": "meta"
}