{
  "id": 235394,
  "title": "Math for Post-processing by Cost Minimization",
  "url": "/competitions/indoor-location-navigation/discussion/235394",
  "author_name": "",
  "post_date": "2021-04-29T09:58:55.713863900Z",
  "votes": 31,
  "comment_count": 8,
  "views": 0,
  "content": "<p>I would like to explain mathematic background for <a href=\"https://www.kaggle.com/saitodevel01/indoor-post-processing-by-cost-minimization\" target=\"_blank\">Akio Saito's excellent notebook</a> here.<br>\nI hope that understanding math for it may lead you to improve more sophisticated postprocessing!</p>\n<p>This notebook aims to minimize the cost function below.</p>\n<p>$$<br>\nL(X_{1:N}) = \\sum_{i=1}^{N} \\alpha_i | X_i - \\hat{X}<em>i |^2 + \\sum</em>{i=1}^{N-1} \\beta_i | (X_{i+1} - X_{i}) - \\Delta \\hat{X}_i |^2<br>\n$$</p>\n<p>where \\(\\hat{X}_i\\) is absolute position predicted by your model and \\(\\Delta \\hat{X}_i\\) is relative position predicted by sensor data.</p>\n<p>The above function can be written as below by matrix.<br>\n$$<br>\n(X-\\hat{X})^T A (X-\\hat{X}) + (DX-\\Delta \\hat{X})^T B (DX-\\Delta \\hat{X})<br>\n$$<br>\nwhere<br>\n$$<br>\n  A = \\left(<br>\n    \\begin{array}{ccc}<br>\n      \\alpha_1 &amp; 0 &amp; 0 \\\\<br>\n      0 &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; \\alpha_n<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>$$<br>\n  B = \\left(<br>\n    \\begin{array}{ccc}<br>\n      \\beta_1 &amp; 0 &amp; 0 \\\\<br>\n      0 &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; \\beta_{n-1}<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>$$<br>\n  D = \\left(<br>\n    \\begin{array}{cccc}<br>\n      -1 &amp; 1 &amp; 0 &amp; 0\\\\<br>\n      0 &amp; \\ddots &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; -1 &amp; 1<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>You can get optimal X for cost function by differentiate the above function and find an X which makes the function equals to  0.<br>\nThat is, you need to solve the below equation<br>\n$$<br>\n(A+A^T)(X-\\hat{X}) + D^T(B+B^T)(DX-\\Delta \\hat{X}) = 0.<br>\n$$<br>\nSolve it and you get<br>\n$$<br>\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X},<br>\n$$<br>\nwhich matches to Saito's notebook.<br>\nNote that \\(A = A^T, B = B^T \\).</p>\n<p>I hope this explanation help you to improve your score!</p>",
  "messages": [
    {
      "id": "1287689",
      "postDate": "04/29/2021 09:58:55",
      "content": "<p>I would like to explain mathematic background for <a href=\"https://www.kaggle.com/saitodevel01/indoor-post-processing-by-cost-minimization\" target=\"_blank\">Akio Saito's excellent notebook</a> here.<br>\nI hope that understanding math for it may lead you to improve more sophisticated postprocessing!</p>\n<p>This notebook aims to minimize the cost function below.</p>\n<p>$$<br>\nL(X_{1:N}) = \\sum_{i=1}^{N} \\alpha_i | X_i - \\hat{X}<em>i |^2 + \\sum</em>{i=1}^{N-1} \\beta_i | (X_{i+1} - X_{i}) - \\Delta \\hat{X}_i |^2<br>\n$$</p>\n<p>where \\(\\hat{X}_i\\) is absolute position predicted by your model and \\(\\Delta \\hat{X}_i\\) is relative position predicted by sensor data.</p>\n<p>The above function can be written as below by matrix.<br>\n$$<br>\n(X-\\hat{X})^T A (X-\\hat{X}) + (DX-\\Delta \\hat{X})^T B (DX-\\Delta \\hat{X})<br>\n$$<br>\nwhere<br>\n$$<br>\n  A = \\left(<br>\n    \\begin{array}{ccc}<br>\n      \\alpha_1 &amp; 0 &amp; 0 \\\\<br>\n      0 &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; \\alpha_n<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>$$<br>\n  B = \\left(<br>\n    \\begin{array}{ccc}<br>\n      \\beta_1 &amp; 0 &amp; 0 \\\\<br>\n      0 &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; \\beta_{n-1}<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>$$<br>\n  D = \\left(<br>\n    \\begin{array}{cccc}<br>\n      -1 &amp; 1 &amp; 0 &amp; 0\\\\<br>\n      0 &amp; \\ddots &amp; \\ddots &amp; 0 \\\\<br>\n      0 &amp; 0 &amp; -1 &amp; 1<br>\n    \\end{array}<br>\n  \\right),<br>\n$$</p>\n<p>You can get optimal X for cost function by differentiate the above function and find an X which makes the function equals to  0.<br>\nThat is, you need to solve the below equation<br>\n$$<br>\n(A+A^T)(X-\\hat{X}) + D^T(B+B^T)(DX-\\Delta \\hat{X}) = 0.<br>\n$$<br>\nSolve it and you get<br>\n$$<br>\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X},<br>\n$$<br>\nwhich matches to Saito's notebook.<br>\nNote that \\(A = A^T, B = B^T \\).</p>\n<p>I hope this explanation help you to improve your score!</p>",
      "rawMarkdown": "I would like to explain mathematic background for [Akio Saito's excellent notebook](https://www.kaggle.com/saitodevel01/indoor-post-processing-by-cost-minimization) here.\nI hope that understanding math for it may lead you to improve more sophisticated postprocessing!\n\nThis notebook aims to minimize the cost function below.\n\n$$\nL(X_{1:N}) = \\sum_{i=1}^{N} \\alpha_i \\| X_i - \\hat{X}_i \\|^2 + \\sum_{i=1}^{N-1} \\beta_i \\| (X_{i+1} - X_{i}) - \\Delta \\hat{X}_i \\|^2\n$$\n\nwhere \\\\(\\hat{X}_i\\\\) is absolute position predicted by your model and \\\\(\\Delta \\hat{X}_i\\\\) is relative position predicted by sensor data.\n\nThe above function can be written as below by matrix.\n$$\n(X-\\hat{X})^T A (X-\\hat{X}) + (DX-\\Delta \\hat{X})^T B (DX-\\Delta \\hat{X})\n$$\nwhere\n$$\n  A = \\left(\n    \\begin{array}{ccc}\n      \\alpha_1 & 0 & 0 \\\\\\\\\n      0 & \\ddots & 0 \\\\\\\\\n      0 & 0 & \\alpha_n\n    \\end{array}\n  \\right),\n$$\n\n$$\n  B = \\left(\n    \\begin{array}{ccc}\n      \\beta_1 & 0 & 0 \\\\\\\\\n      0 & \\ddots & 0 \\\\\\\\\n      0 & 0 & \\beta_{n-1}\n    \\end{array}\n  \\right),\n$$\n\n$$\n  D = \\left(\n    \\begin{array}{cccc}\n      -1 & 1 & 0 & 0\\\\\\\\\n      0 & \\ddots & \\ddots & 0 \\\\\\\\\n      0 & 0 & -1 & 1\n    \\end{array}\n  \\right),\n$$\n\nYou can get optimal X for cost function by differentiate the above function and find an X which makes the function equals to  0.\nThat is, you need to solve the below equation\n$$\n(A+A^T)(X-\\hat{X}) + D^T(B+B^T)(DX-\\Delta \\hat{X}) = 0.\n$$\nSolve it and you get\n$$\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X},\n$$\nwhich matches to Saito's notebook.\nNote that \\\\(A = A^T, B = B^T \\\\).\n\nI hope this explanation help you to improve your score!",
      "votes": null
    },
    {
      "id": "1287702",
      "postDate": "04/29/2021 10:06:43",
      "content": "<p>my latex display is sadly broken…</p>",
      "rawMarkdown": "my latex display is sadly broken...",
      "votes": null
    },
    {
      "id": "1287824",
      "postDate": "04/29/2021 12:50:17",
      "content": "<p>Thank you for the post. Is the latex broken when you preview the post? I just copied your latex and it looks good in preview mode.</p>",
      "rawMarkdown": "Thank you for the post. Is the latex broken when you preview the post? I just copied your latex and it looks good in preview mode.",
      "votes": null
    },
    {
      "id": "1287940",
      "postDate": "04/29/2021 14:44:28",
      "content": "<p>Thank you for investigation.<br>\nActually it looks broken in my preview mode and I have no idea what to do…</p>",
      "rawMarkdown": "Thank you for investigation.\nActually it looks broken in my preview mode and I have no idea what to do...",
      "votes": null
    },
    {
      "id": "1288039",
      "postDate": "04/29/2021 16:14:25",
      "content": "<p>it is fixed though I don't know why.</p>",
      "rawMarkdown": "it is fixed though I don't know why.",
      "votes": null
    },
    {
      "id": "1289746",
      "postDate": "05/01/2021 10:28:07",
      "content": "<p>Thanks for the explanations. It seems that the last formula has a small error. It should rather be</p>\n<p>$$<br>\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X}<br>\n$$</p>",
      "rawMarkdown": "Thanks for the explanations. It seems that the last formula has a small error. It should rather be\n\n$$\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X}\n$$",
      "votes": null
    },
    {
      "id": "1290037",
      "postDate": "05/01/2021 15:14:07",
      "content": "<p>Thank you for your comment!<br>\nYou are right and it is important point.<br>\nI fixed it.</p>",
      "rawMarkdown": "Thank you for your comment!\nYou are right and it is important point.\nI fixed it.",
      "votes": null
    },
    {
      "id": "1302883",
      "postDate": "05/11/2021 19:03:08",
      "content": "<p>I was a bit confused by the following fact:  ML-predicted coordinates are in the coordinate system of the building, however the deltaX predicted by the sensor are related to the compass direction. Can we compare them? Or did I misunderstand anything? Thanks!</p>",
      "rawMarkdown": "I was a bit confused by the following fact:  ML-predicted coordinates are in the coordinate system of the building, however the deltaX predicted by the sensor are related to the compass direction. Can we compare them? Or did I misunderstand anything? Thanks!",
      "votes": null
    },
    {
      "id": "1302889",
      "postDate": "05/11/2021 19:07:27",
      "content": "<p>It looks like the floor plans are scaled such that they match the compass directions. So yes, I think they are directly comparable.</p>",
      "rawMarkdown": "It looks like the floor plans are scaled such that they match the compass directions. So yes, I think they are directly comparable.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1287702,
      "author_name": "iwatatakuya",
      "author_url": "",
      "post_date": "04/29/2021 10:06:43",
      "content": "<p>my latex display is sadly broken…</p>",
      "votes": null,
      "replies": [
        {
          "id": 1287824,
          "author_name": "jiweiliu",
          "author_url": "",
          "post_date": "04/29/2021 12:50:17",
          "content": "<p>Thank you for the post. Is the latex broken when you preview the post? I just copied your latex and it looks good in preview mode.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1287940,
          "author_name": "iwatatakuya",
          "author_url": "",
          "post_date": "04/29/2021 14:44:28",
          "content": "<p>Thank you for investigation.<br>\nActually it looks broken in my preview mode and I have no idea what to do…</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1288039,
          "author_name": "iwatatakuya",
          "author_url": "",
          "post_date": "04/29/2021 16:14:25",
          "content": "<p>it is fixed though I don't know why.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1289746,
      "author_name": "helgith",
      "author_url": "",
      "post_date": "05/01/2021 10:28:07",
      "content": "<p>Thanks for the explanations. It seems that the last formula has a small error. It should rather be</p>\n<p>$$<br>\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X}<br>\n$$</p>",
      "votes": null,
      "replies": [
        {
          "id": 1290037,
          "author_name": "iwatatakuya",
          "author_url": "",
          "post_date": "05/01/2021 15:14:07",
          "content": "<p>Thank you for your comment!<br>\nYou are right and it is important point.<br>\nI fixed it.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1302883,
      "author_name": "fjodorfomin",
      "author_url": "",
      "post_date": "05/11/2021 19:03:08",
      "content": "<p>I was a bit confused by the following fact:  ML-predicted coordinates are in the coordinate system of the building, however the deltaX predicted by the sensor are related to the compass direction. Can we compare them? Or did I misunderstand anything? Thanks!</p>",
      "votes": null,
      "replies": [
        {
          "id": 1302889,
          "author_name": "tvdwiele",
          "author_url": "",
          "post_date": "05/11/2021 19:07:27",
          "content": "<p>It looks like the floor plans are scaled such that they match the compass directions. So yes, I think they are directly comparable.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1287689": "I would like to explain mathematic background for [Akio Saito's excellent notebook](https://www.kaggle.com/saitodevel01/indoor-post-processing-by-cost-minimization) here.\nI hope that understanding math for it may lead you to improve more sophisticated postprocessing!\n\nThis notebook aims to minimize the cost function below.\n\n$$\nL(X_{1:N}) = \\sum_{i=1}^{N} \\alpha_i \\| X_i - \\hat{X}_i \\|^2 + \\sum_{i=1}^{N-1} \\beta_i \\| (X_{i+1} - X_{i}) - \\Delta \\hat{X}_i \\|^2\n$$\n\nwhere \\\\(\\hat{X}_i\\\\) is absolute position predicted by your model and \\\\(\\Delta \\hat{X}_i\\\\) is relative position predicted by sensor data.\n\nThe above function can be written as below by matrix.\n$$\n(X-\\hat{X})^T A (X-\\hat{X}) + (DX-\\Delta \\hat{X})^T B (DX-\\Delta \\hat{X})\n$$\nwhere\n$$\n  A = \\left(\n    \\begin{array}{ccc}\n      \\alpha_1 & 0 & 0 \\\\\\\\\n      0 & \\ddots & 0 \\\\\\\\\n      0 & 0 & \\alpha_n\n    \\end{array}\n  \\right),\n$$\n\n$$\n  B = \\left(\n    \\begin{array}{ccc}\n      \\beta_1 & 0 & 0 \\\\\\\\\n      0 & \\ddots & 0 \\\\\\\\\n      0 & 0 & \\beta_{n-1}\n    \\end{array}\n  \\right),\n$$\n\n$$\n  D = \\left(\n    \\begin{array}{cccc}\n      -1 & 1 & 0 & 0\\\\\\\\\n      0 & \\ddots & \\ddots & 0 \\\\\\\\\n      0 & 0 & -1 & 1\n    \\end{array}\n  \\right),\n$$\n\nYou can get optimal X for cost function by differentiate the above function and find an X which makes the function equals to  0.\nThat is, you need to solve the below equation\n$$\n(A+A^T)(X-\\hat{X}) + D^T(B+B^T)(DX-\\Delta \\hat{X}) = 0.\n$$\nSolve it and you get\n$$\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X},\n$$\nwhich matches to Saito's notebook.\nNote that \\\\(A = A^T, B = B^T \\\\).\n\nI hope this explanation help you to improve your score!",
    "1287702": "my latex display is sadly broken...",
    "1287824": "Thank you for the post. Is the latex broken when you preview the post? I just copied your latex and it looks good in preview mode.",
    "1287940": "Thank you for investigation.\nActually it looks broken in my preview mode and I have no idea what to do...",
    "1288039": "it is fixed though I don't know why.",
    "1289746": "Thanks for the explanations. It seems that the last formula has a small error. It should rather be\n\n$$\n(A + D^TBD)X = A\\hat{X}+D^TB\\Delta \\hat{X}\n$$",
    "1290037": "Thank you for your comment!\nYou are right and it is important point.\nI fixed it.",
    "1302883": "I was a bit confused by the following fact:  ML-predicted coordinates are in the coordinate system of the building, however the deltaX predicted by the sensor are related to the compass direction. Can we compare them? Or did I misunderstand anything? Thanks!",
    "1302889": "It looks like the floor plans are scaled such that they match the compass directions. So yes, I think they are directly comparable."
  },
  "source": "meta"
}