{
  "id": 381747,
  "title": "Derivation of Line-fit least squares ",
  "url": "/competitions/icecube-neutrinos-in-deep-ice/discussion/381747",
  "author_name": "",
  "post_date": "2023-01-28T01:53:06.864807700Z",
  "votes": 40,
  "comment_count": 5,
  "views": 0,
  "content": "<p>From the references listed below, a formula is given for the best least square line fit to a traveling charged particle with sensor detections.  This is fairly simple and does not take into account Cherenkov radiation and its cone of radiation, but it is a good starting point.  I will show the derivation of the formula.<br>\nEd:  The notebook implementing this scores 1.214 <a href=\"https://www.kaggle.com/code/solverworld/icecube-neutrino-path-least-squares-1-214\" target=\"_blank\">least squares notebook</a><br>\nWe assume that the particle is traveling such that its position at time t is<br>\n$$pos(t) = r - v*t$$<br>\nwhere \\(r\\) and \\(v\\) are 3-dimensional vectors representing the position at time 0 and the velocity, respectively.<br>\nThen we have a series of \\(N\\) measurements from sensors at positions \\(r_i\\) at times \\(t_i\\).  We assume that the particle reaches nearest to the sensor at that time (this is the simplification).  Therefore, the total error in 3-dimensions across all the sensors is<br>\n$$E =  \\sum_{i=0}^{N}(r_i-r-vt_i)^T(r_i-r-vt_i)$$<br>\nTo find the values of v and r that make this minimal, we take the derivative with respect to \\(r\\) and \\(v\\):<br>\n$$\\frac {\\partial E}{\\partial r} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-1) = 0$$<br>\nand so<br>\n$$r = \\frac {1}{N}\\sum_{i=0}^{N}(r_i - vt_i) = \\langle r_i \\rangle - v \\langle t_i \\rangle$$<br>\nwhere we use the \\(\\langle \\rangle \\) notation to denote the average over the data.<br>\nTaking the derivative with respect to \\(v\\):<br>\n$$\\frac {\\partial E}{\\partial v} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-t_i) = 0$$<br>\nor<br>\n$$\\sum t_i r = \\sum (r_i t_i - v t_i^2) $$<br>\nor<br>\n$$\\langle t_i \\rangle r = \\langle r_i t_i \\rangle - v \\langle t_i^2 \\rangle$$<br>\nSubstituting for r in this equation with the value obtained above and some simple manipulations produces<br>\nthe answer</p>\n<p>$$v_{est} = \\frac{ \\langle r_i * t_i \\rangle -  \\langle r_i \\rangle * \\langle t_i \\rangle}{\\langle t_i^2 \\rangle - \\langle t_i \\rangle ^2}$$</p>\n<p>References:<br>\nMirco Hunnefield Masters Thesis, Online Reconstruction of Muon-Neutrino Events in IceCube using Deep Learning Techniques<br>\nKai Schatto, PhD Thesis, Stacked searches for high-energy neutrinos from blazars with IceCube</p>",
  "messages": [
    {
      "id": "2118414",
      "postDate": "01/28/2023 01:53:06",
      "content": "<p>From the references listed below, a formula is given for the best least square line fit to a traveling charged particle with sensor detections.  This is fairly simple and does not take into account Cherenkov radiation and its cone of radiation, but it is a good starting point.  I will show the derivation of the formula.<br>\nEd:  The notebook implementing this scores 1.214 <a href=\"https://www.kaggle.com/code/solverworld/icecube-neutrino-path-least-squares-1-214\" target=\"_blank\">least squares notebook</a><br>\nWe assume that the particle is traveling such that its position at time t is<br>\n$$pos(t) = r - v*t$$<br>\nwhere \\(r\\) and \\(v\\) are 3-dimensional vectors representing the position at time 0 and the velocity, respectively.<br>\nThen we have a series of \\(N\\) measurements from sensors at positions \\(r_i\\) at times \\(t_i\\).  We assume that the particle reaches nearest to the sensor at that time (this is the simplification).  Therefore, the total error in 3-dimensions across all the sensors is<br>\n$$E =  \\sum_{i=0}^{N}(r_i-r-vt_i)^T(r_i-r-vt_i)$$<br>\nTo find the values of v and r that make this minimal, we take the derivative with respect to \\(r\\) and \\(v\\):<br>\n$$\\frac {\\partial E}{\\partial r} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-1) = 0$$<br>\nand so<br>\n$$r = \\frac {1}{N}\\sum_{i=0}^{N}(r_i - vt_i) = \\langle r_i \\rangle - v \\langle t_i \\rangle$$<br>\nwhere we use the \\(\\langle \\rangle \\) notation to denote the average over the data.<br>\nTaking the derivative with respect to \\(v\\):<br>\n$$\\frac {\\partial E}{\\partial v} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-t_i) = 0$$<br>\nor<br>\n$$\\sum t_i r = \\sum (r_i t_i - v t_i^2) $$<br>\nor<br>\n$$\\langle t_i \\rangle r = \\langle r_i t_i \\rangle - v \\langle t_i^2 \\rangle$$<br>\nSubstituting for r in this equation with the value obtained above and some simple manipulations produces<br>\nthe answer</p>\n<p>$$v_{est} = \\frac{ \\langle r_i * t_i \\rangle -  \\langle r_i \\rangle * \\langle t_i \\rangle}{\\langle t_i^2 \\rangle - \\langle t_i \\rangle ^2}$$</p>\n<p>References:<br>\nMirco Hunnefield Masters Thesis, Online Reconstruction of Muon-Neutrino Events in IceCube using Deep Learning Techniques<br>\nKai Schatto, PhD Thesis, Stacked searches for high-energy neutrinos from blazars with IceCube</p>",
      "rawMarkdown": "From the references listed below, a formula is given for the best least square line fit to a traveling charged particle with sensor detections.  This is fairly simple and does not take into account Cherenkov radiation and its cone of radiation, but it is a good starting point.  I will show the derivation of the formula.\n\nEd:  The notebook implementing this scores 1.214 [least squares notebook](https://www.kaggle.com/code/solverworld/icecube-neutrino-path-least-squares-1-214)\n\nWe assume that the particle is traveling such that its position at time t is\n$$pos(t) = r - v*t$$\nwhere \\\\(r\\\\) and \\\\(v\\\\) are 3-dimensional vectors representing the position at time 0 and the velocity, respectively.\nThen we have a series of \\\\(N\\\\) measurements from sensors at positions \\\\(r_i\\\\) at times \\\\(t_i\\\\).  We assume that the particle reaches nearest to the sensor at that time (this is the simplification).  Therefore, the total error in 3-dimensions across all the sensors is\n$$E =  \\sum_{i=0}^{N}(r_i-r-vt_i)^T(r_i-r-vt_i)$$\nTo find the values of v and r that make this minimal, we take the derivative with respect to \\\\(r\\\\) and \\\\(v\\\\):\n$$\\frac {\\partial E}{\\partial r} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-1) = 0$$\nand so\n$$r = \\frac {1}{N}\\sum_{i=0}^{N}(r_i - vt_i) = \\langle r_i \\rangle - v \\langle t_i \\rangle$$\nwhere we use the \\\\(\\langle \\rangle \\\\) notation to denote the average over the data.\nTaking the derivative with respect to \\\\(v\\\\):\n\n$$\\frac {\\partial E}{\\partial v} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-t_i) = 0$$\nor\n$$\\sum t_i r = \\sum (r_i t_i - v t_i^2) $$\nor\n$$\\langle t_i \\rangle r = \\langle r_i t_i \\rangle - v \\langle t_i^2 \\rangle$$\nSubstituting for r in this equation with the value obtained above and some simple manipulations produces\nthe answer\n    \n    \n$$v_{est} = \\frac{ \\langle r_i * t_i \\rangle -  \\langle r_i \\rangle * \\langle t_i \\rangle}{\\langle t_i^2 \\rangle - \\langle t_i \\rangle ^2}$$\n    \nReferences:\n\nMirco Hunnefield Masters Thesis, Online Reconstruction of Muon-Neutrino Events in IceCube using Deep Learning Techniques\nKai Schatto, PhD Thesis, Stacked searches for high-energy neutrinos from blazars with IceCube",
      "votes": null
    },
    {
      "id": "2119872",
      "postDate": "01/29/2023 07:03:17",
      "content": "<p>A couple interesting things I found when comparing this formula to the one I was using:</p>\n<ol>\n<li>You can weight it - for instance weight by 'charge' column - if you convert the  from avg(expression_i) to sum(w_i*expression_i)/sum(w_i). </li>\n<li>Then I was surprised to find that this formula is exactly equivalent to the formula I was using, which was to simply find the weighted center all points, first by linear weighting towards early timestamps (t_min = 1, t_max = 0), then by weighting towards late timestamps (t_min = 0, t_max = 1). So the formula looks a little different, but converts to the identical result.</li>\n</ol>\n<p>See this and much more in <a href=\"https://www.kaggle.com/code/roberthatch/lb-1-183-lightning-fast-baseline-with-polars\" target=\"_blank\">my new notebook.</a>. I can run the above formula in just 7 seconds per batch file! And can get a better score (though takes about 70-90 seconds per batch file) by slightly optimizing which points we treat as aux=False.</p>",
      "rawMarkdown": "A couple interesting things I found when comparing this formula to the one I was using:\n\n1. You can weight it - for instance weight by 'charge' column - if you convert the <expression_i> from avg(expression_i) to sum(w_i*expression_i)/sum(w_i). \n2. Then I was surprised to find that this formula is exactly equivalent to the formula I was using, which was to simply find the weighted center all points, first by linear weighting towards early timestamps (t_min = 1, t_max = 0), then by weighting towards late timestamps (t_min = 0, t_max = 1). So the formula looks a little different, but converts to the identical result.\n\nSee this and much more in [my new notebook.](https://www.kaggle.com/code/roberthatch/lb-1-183-lightning-fast-baseline-with-polars). I can run the above formula in just 7 seconds per batch file! And can get a better score (though takes about 70-90 seconds per batch file) by slightly optimizing which points we treat as aux=False.",
      "votes": null
    },
    {
      "id": "2151619",
      "postDate": "02/20/2023 07:57:14",
      "content": "<p>There is an easier derivation of the formula.</p>\n<p>Let the charge move along the trajectory <strong>r</strong>=<strong>q</strong>+ <strong>u</strong> t and N sensors fire only along it (t,<strong>r</strong>). Then (the second equation is the multiplication of the first by t):<br>\n$$<br>\n\\mathbf{r}_i =\\mathbf{q} + \\mathbf{u}\\,t_i,\\,\\,\\,\\,\\,\\,\\mathbf{r}_i\\,t_i= \\mathbf{q}\\,t_i + \\mathbf{u}\\,t^2_i<br>\n$$<br>\nAveraging over i and solving the system of equations for <strong>u</strong>, <strong>q</strong>,  we obtain what we are looking for.</p>",
      "rawMarkdown": "There is an easier derivation of the formula.\n\nLet the charge move along the trajectory **r**=**q**+ **u** t and N sensors fire only along it (t<sub>i</sub>,**r**<sub>i</sub>). Then (the second equation is the multiplication of the first by t<sub>i</sub>):\n$$\n\\mathbf{r}_i =\\mathbf{q} + \\mathbf{u}\\,t_i,\\,\\,\\,\\,\\,\\,\\mathbf{r}_i\\,t_i= \\mathbf{q}\\,t_i + \\mathbf{u}\\,t^2_i\n$$\nAveraging over i and solving the system of equations for **u**, **q**,  we obtain what we are looking for.",
      "votes": null
    },
    {
      "id": "2152072",
      "postDate": "02/20/2023 14:47:34",
      "content": "<p>Interesting.  While your derivation ends up with the same formula, you are solving a different problem.  I was solving for the q, u that gives the smallest error.   You are solving the problem where the error is always 0.  You wrote \\(r_i = q + u t_i\\) as an equality.</p>",
      "rawMarkdown": "Interesting.  While your derivation ends up with the same formula, you are solving a different problem.  I was solving for the q, u that gives the smallest error.   You are solving the problem where the error is always 0.  You wrote \\\\(r_i = q + u t_i\\\\) as an equality.",
      "votes": null
    },
    {
      "id": "2152185",
      "postDate": "02/20/2023 16:26:46",
      "content": "<p>Of course, the method of least squares (which you used) is a mathematically more correct procedure. But sometimes you can make your life easier :)</p>",
      "rawMarkdown": "Of course, the method of least squares (which you used) is a mathematically more correct procedure. But sometimes you can make your life easier :)",
      "votes": null
    },
    {
      "id": "2222242",
      "postDate": "04/15/2023 04:41:06",
      "content": "<p>interesting work </p>",
      "rawMarkdown": "interesting work",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2119872,
      "author_name": "roberthatch",
      "author_url": "",
      "post_date": "01/29/2023 07:03:17",
      "content": "<p>A couple interesting things I found when comparing this formula to the one I was using:</p>\n<ol>\n<li>You can weight it - for instance weight by 'charge' column - if you convert the  from avg(expression_i) to sum(w_i*expression_i)/sum(w_i). </li>\n<li>Then I was surprised to find that this formula is exactly equivalent to the formula I was using, which was to simply find the weighted center all points, first by linear weighting towards early timestamps (t_min = 1, t_max = 0), then by weighting towards late timestamps (t_min = 0, t_max = 1). So the formula looks a little different, but converts to the identical result.</li>\n</ol>\n<p>See this and much more in <a href=\"https://www.kaggle.com/code/roberthatch/lb-1-183-lightning-fast-baseline-with-polars\" target=\"_blank\">my new notebook.</a>. I can run the above formula in just 7 seconds per batch file! And can get a better score (though takes about 70-90 seconds per batch file) by slightly optimizing which points we treat as aux=False.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2151619,
      "author_name": "synset",
      "author_url": "",
      "post_date": "02/20/2023 07:57:14",
      "content": "<p>There is an easier derivation of the formula.</p>\n<p>Let the charge move along the trajectory <strong>r</strong>=<strong>q</strong>+ <strong>u</strong> t and N sensors fire only along it (t,<strong>r</strong>). Then (the second equation is the multiplication of the first by t):<br>\n$$<br>\n\\mathbf{r}_i =\\mathbf{q} + \\mathbf{u}\\,t_i,\\,\\,\\,\\,\\,\\,\\mathbf{r}_i\\,t_i= \\mathbf{q}\\,t_i + \\mathbf{u}\\,t^2_i<br>\n$$<br>\nAveraging over i and solving the system of equations for <strong>u</strong>, <strong>q</strong>,  we obtain what we are looking for.</p>",
      "votes": null,
      "replies": [
        {
          "id": 2152072,
          "author_name": "solverworld",
          "author_url": "",
          "post_date": "02/20/2023 14:47:34",
          "content": "<p>Interesting.  While your derivation ends up with the same formula, you are solving a different problem.  I was solving for the q, u that gives the smallest error.   You are solving the problem where the error is always 0.  You wrote \\(r_i = q + u t_i\\) as an equality.</p>",
          "votes": null,
          "replies": [
            {
              "id": 2152185,
              "author_name": "synset",
              "author_url": "",
              "post_date": "02/20/2023 16:26:46",
              "content": "<p>Of course, the method of least squares (which you used) is a mathematically more correct procedure. But sometimes you can make your life easier :)</p>",
              "votes": null,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2222242,
      "author_name": "hassaandaoud",
      "author_url": "",
      "post_date": "04/15/2023 04:41:06",
      "content": "<p>interesting work </p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2118414": "From the references listed below, a formula is given for the best least square line fit to a traveling charged particle with sensor detections.  This is fairly simple and does not take into account Cherenkov radiation and its cone of radiation, but it is a good starting point.  I will show the derivation of the formula.\n\nEd:  The notebook implementing this scores 1.214 [least squares notebook](https://www.kaggle.com/code/solverworld/icecube-neutrino-path-least-squares-1-214)\n\nWe assume that the particle is traveling such that its position at time t is\n$$pos(t) = r - v*t$$\nwhere \\\\(r\\\\) and \\\\(v\\\\) are 3-dimensional vectors representing the position at time 0 and the velocity, respectively.\nThen we have a series of \\\\(N\\\\) measurements from sensors at positions \\\\(r_i\\\\) at times \\\\(t_i\\\\).  We assume that the particle reaches nearest to the sensor at that time (this is the simplification).  Therefore, the total error in 3-dimensions across all the sensors is\n$$E =  \\sum_{i=0}^{N}(r_i-r-vt_i)^T(r_i-r-vt_i)$$\nTo find the values of v and r that make this minimal, we take the derivative with respect to \\\\(r\\\\) and \\\\(v\\\\):\n$$\\frac {\\partial E}{\\partial r} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-1) = 0$$\nand so\n$$r = \\frac {1}{N}\\sum_{i=0}^{N}(r_i - vt_i) = \\langle r_i \\rangle - v \\langle t_i \\rangle$$\nwhere we use the \\\\(\\langle \\rangle \\\\) notation to denote the average over the data.\nTaking the derivative with respect to \\\\(v\\\\):\n\n$$\\frac {\\partial E}{\\partial v} =  \\sum_{i=0}^{N}2(r_i-r-vt_i)(-t_i) = 0$$\nor\n$$\\sum t_i r = \\sum (r_i t_i - v t_i^2) $$\nor\n$$\\langle t_i \\rangle r = \\langle r_i t_i \\rangle - v \\langle t_i^2 \\rangle$$\nSubstituting for r in this equation with the value obtained above and some simple manipulations produces\nthe answer\n    \n    \n$$v_{est} = \\frac{ \\langle r_i * t_i \\rangle -  \\langle r_i \\rangle * \\langle t_i \\rangle}{\\langle t_i^2 \\rangle - \\langle t_i \\rangle ^2}$$\n    \nReferences:\n\nMirco Hunnefield Masters Thesis, Online Reconstruction of Muon-Neutrino Events in IceCube using Deep Learning Techniques\nKai Schatto, PhD Thesis, Stacked searches for high-energy neutrinos from blazars with IceCube",
    "2119872": "A couple interesting things I found when comparing this formula to the one I was using:\n\n1. You can weight it - for instance weight by 'charge' column - if you convert the <expression_i> from avg(expression_i) to sum(w_i*expression_i)/sum(w_i). \n2. Then I was surprised to find that this formula is exactly equivalent to the formula I was using, which was to simply find the weighted center all points, first by linear weighting towards early timestamps (t_min = 1, t_max = 0), then by weighting towards late timestamps (t_min = 0, t_max = 1). So the formula looks a little different, but converts to the identical result.\n\nSee this and much more in [my new notebook.](https://www.kaggle.com/code/roberthatch/lb-1-183-lightning-fast-baseline-with-polars). I can run the above formula in just 7 seconds per batch file! And can get a better score (though takes about 70-90 seconds per batch file) by slightly optimizing which points we treat as aux=False.",
    "2151619": "There is an easier derivation of the formula.\n\nLet the charge move along the trajectory **r**=**q**+ **u** t and N sensors fire only along it (t<sub>i</sub>,**r**<sub>i</sub>). Then (the second equation is the multiplication of the first by t<sub>i</sub>):\n$$\n\\mathbf{r}_i =\\mathbf{q} + \\mathbf{u}\\,t_i,\\,\\,\\,\\,\\,\\,\\mathbf{r}_i\\,t_i= \\mathbf{q}\\,t_i + \\mathbf{u}\\,t^2_i\n$$\nAveraging over i and solving the system of equations for **u**, **q**,  we obtain what we are looking for.",
    "2152072": "Interesting.  While your derivation ends up with the same formula, you are solving a different problem.  I was solving for the q, u that gives the smallest error.   You are solving the problem where the error is always 0.  You wrote \\\\(r_i = q + u t_i\\\\) as an equality.",
    "2152185": "Of course, the method of least squares (which you used) is a mathematically more correct procedure. But sometimes you can make your life easier :)",
    "2222242": "interesting work"
  },
  "source": "meta"
}