{"id":49070301,"url":"https://github.com/pgdr/mse-potion","last_synced_at":"2026-04-20T07:05:43.034Z","repository":{"id":344833661,"uuid":"1183330100","full_name":"pgdr/mse-potion","owner":"pgdr","description":null,"archived":false,"fork":false,"pushed_at":"2026-03-16T14:10:32.000Z","size":1,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-03-17T01:57:05.022Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgdr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-16T13:57:03.000Z","updated_at":"2026-03-16T14:10:36.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/pgdr/mse-potion","commit_stats":null,"previous_names":["pgdr/mse-potion"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/pgdr/mse-potion","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgdr%2Fmse-potion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgdr%2Fmse-potion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgdr%2Fmse-potion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgdr%2Fmse-potion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgdr","download_url":"https://codeload.github.com/pgdr/mse-potion/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgdr%2Fmse-potion/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32036803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"online","status_checked_at":"2026-04-20T02:00:06.527Z","response_time":94,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-04-20T07:05:40.965Z","updated_at":"2026-04-20T07:05:43.016Z","avatar_url":"https://github.com/pgdr.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Squared Errors in Potion Mixtures --- a solution to some problem\n\nWe need to compute an optimal partition of a list of numbers into\ncontiguous parts, where each part contributes the sum of all pairwise\nsquared errors within that part.  The total cost is the sum over all\nparts.\n\n$$\\sum_{t=1}^{K} \\sum_{\\ell_t \\leq a \u003c b \u003c r_t} (\\text{data}[a] - \\text{data}[b])^2$$\n\nWe can precompute in linear time `prefix_sum` and `prefix_sum_squared`\nand thereby getting $O(1)$ time lookup for the sum of pairwise squared\nerrors for a block $[\\ell, r)$:\n\n```python\nprefix_sum = [0] * (n + 1)\nprefix_sum_sq = [0] * (n + 1)\nfor i, x in enumerate(data, 1):\n    prefix_sum[i] = prefix_sum[i - 1] + x\n    prefix_sum_sq[i] = prefix_sum_sq[i - 1] + x**2\n\n\ndef squared_error(l, r):\n    L_sq = prefix_sum_sq[l]\n    s = prefix_sum[l] - prefix_sum[r]\n    return (l - r) * (L_sq - prefix_sum_sq[r]) - s**2\n```\n\n\nNow, for indices $a \\leq b \\leq c \\leq d$, we have\n\n$$E[a,c)+E[b,d) \\leq E[a,d)+E[b,c),$$\n\nso the cost function satisfies the [Monge property](https://en.wikipedia.org/wiki/Monge_array).\n\nTherefore, we can use the [SMAWK algorithm](https://en.wikipedia.org/wiki/SMAWK_algorithm),\nor straight-forward dynamic programming with divide-and-conquer\nbased on monotonicity, to improve the running time from the naïve\n$\\Omega(n^2 \\cdot k)$ to $O(k \\cdot n \\log n)$.\n\n\n## Appendix\n\nComputing the sum of pairwise differences in a segment can be done in a cute way.\nFirst, since $(x_i-x_j)^2 = x_i^2 + x_j^2 - 2x_ix_j$, by summing over all $i \u003c j$, we get\n\n$\\sum_{i\u003cj}(x_i-x_j)^2 = (k-1)\\sum_{t} x_t^2 - 2\\sum_{i\u003cj} x_ix_j$\n\nwhere $k = j - i$, i.e., the number of elements we're summing over.  But note that\n\n$\\left(\\sum_t x_t\\right)^2 = \\sum_t x_t^2 + 2\\sum_{i\u003cj} x_ix_j$\n\nso\n\n$2\\sum_{i\u003cj} x_ix_j = \\left(\\sum_t x_t\\right)^2 - \\sum_t x_t^2$\n\nSubstituting in the above, we arrive at\n\n$\\sum_{i\u003cj}(x_i-x_j)^2 = k\\sum_t x_t^2 - \\left(\\sum_t x_t\\right)^2$\n\nHence, by _prefix sums_, we can compute, in **constant time**,\n\n1. $\\sum_{i \\leq t \u003c j}x_t^2 = \\sum_{t \u003c j}x_t^2 - \\sum_{t \u003c i}x_t^2$\n1. $\\left(\\sum_{i \\leq t \u003c j}x_t\\right)^2 = \\left(\\sum_{t \u003c j}x_t - \\sum_{t \u003c i}x_t\\right)^2$\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgdr%2Fmse-potion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgdr%2Fmse-potion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgdr%2Fmse-potion/lists"}