{"id":23029638,"url":"https://github.com/antononcube/raku-math-fitting","last_synced_at":"2025-04-02T20:25:35.870Z","repository":{"id":244405262,"uuid":"815121060","full_name":"antononcube/Raku-Math-Fitting","owner":"antononcube","description":"Raku package for line and curve fitting into sets of points.","archived":false,"fork":false,"pushed_at":"2024-12-14T20:37:11.000Z","size":55,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-08T11:13:02.582Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antononcube.png","metadata":{"files":{"readme":"README-work.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-06-14T11:53:47.000Z","updated_at":"2024-12-14T20:37:15.000Z","dependencies_parsed_at":"2024-06-21T18:10:16.654Z","dependency_job_id":"cd31f915-5f8e-46a3-8494-490d1def56c0","html_url":"https://github.com/antononcube/Raku-Math-Fitting","commit_stats":null,"previous_names":["antononcube/raku-math-fitting"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-Fitting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-Fitting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-Fitting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antononcube%2FRaku-Math-Fitting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antononcube","download_url":"https://codeload.github.com/antononcube/Raku-Math-Fitting/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246886467,"owners_count":20849867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":"2024-12-15T14:16:23.750Z","updated_at":"2025-04-02T20:25:35.853Z","avatar_url":"https://github.com/antononcube.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Math::Fitting\n\nRaku package for line, curve, and hyper-plane fitting over sets of points.\n\n------\n\n## Installation\n\nFrom [Zef ecosystem](https://raku.land):\n\n```\nzef install Math::Fitting\n```\n\nFrom GitHub:\n\n```\nzef install https://github.com/antononcube/Raku-Math-Fitting.git\n```\n\n------\n\n## Usage examples\n\n### Linear regression (1D regressor)\n\nHere is data that is an array of pairs logarithms of GDP and electricity production of different countries:\n\n```raku\nmy @data =\n        (11.773906839650255e0, 11.253864992156679e0), (12.171286902172696e0, 12.009204578014298e0),\n        (12.107713405070312e0, 11.437962903093876e0), (12.696829332236298e0, 12.00401094268482e0),\n        (11.28266350477445e0, 11.499283586840601e0), (12.585056018658895e0, 11.775388329493465e0),\n        (10.723949075934968e0, 10.54745313463026e0), (11.389785159542825e0, 10.928944887584644e0),\n        (13.167988368224217e0, 12.856756553879103e0), (11.019424327650983e0, 10.472888033769635e0),\n        (10.198154686798192e0, 9.331143140569578e0), (11.395702941127277e0, 10.772811339725912e0),\n        (12.031878183906066e0, 11.516569745919236e0), (11.430963567030055e0, 10.855585778720567e0),\n        (10.21302480711734e0, 10.138888016785716e0), (11.857393542118762e0, 11.520472966745002e0),\n        (11.223299730942632e0, 10.933368427760566e0), (12.15978767072575e0, 11.78868016913858e0),\n        (11.190367918582032e0, 10.557867961568022e0), (11.733248321280731e0, 11.226857570288722e0),\n        (10.839510106614012e0, 10.677157910153495e0), (11.191959374358786e0, 11.17621400228234e0),\n        (12.215911663039382e0, 11.808235068507617e0), (12.420008212627797e0, 11.723537761532056e0),\n        (11.551553518127756e0, 10.519077512018512e0), (11.845171164859963e0, 11.528956530193586e0),\n        (12.212327463463307e0, 11.782617762483701e0), (11.479897117669006e0, 11.355095745306354e0),\n        (13.320906155798967e0, 12.6405552706939e0);\n        \n@data.elems;        \n```\n\n**Remark:** The data was taken from [\"Data::Geographics\"](https://raku.land/zef:antononcube/Data::Geographics), [AAp1].\n\nHere is a corresponding plot (using [\"Text::Plot\"](https://raku.land/zef:antononcube/Text::Plot), [AAp2]):\n\n```raku\nuse Text::Plot;\n\ntext-list-plot(@data, title =\u003e 'lg(GDP) vs lg(Electricity Production)', x-label =\u003e 'GDP', y-label =\u003e 'EP');\n```\n\nHere is corresponding linear model fit, a functor (i.e. objects that does `Callable`):\n\n```raku\nuse Math::Fitting;\nmy \u0026f = linear-model-fit(@data);\n```\n\nHere are the best fit parameters (fit coefficients):\n\n```raku\n\u0026f('BestFitParameters');\n```\n\nHere we call the functor over a range of values:\n\n```raku\n(10..13)».\u0026f\n```\n\n\nHere are the corresponding residuals:\n\n```raku\ntext-list-plot(\u0026f('residuals'))\n```\n\n### Multidimensional regression (2D regressor)\n\nHere is a matrix with 3D data:\n\n```perl6\nuse Data::Reshapers;\n\n(1..2) X (1..3)\n==\u003e { .map({ [|$_, sin($_.sum)] }) }()\n==\u003e my @data3D;\n\nto-pretty-table(@data3D);\n````\n\nHere is a functor for the multidimensional fit: \n\n```perl6\nmy \u0026mf = linear-model-fit(@data3D);\n```\n\nHere are the best parameters:\n\n```perl6\n\u0026mf('BestFitParameters');\n```\n\nHere is a predicated value:\n\n```perl6\n\u0026mf(2.5, 2.5);\n```\n\nHere is plot of the residuals:\n\n```perl6\ntext-list-plot(\u0026mf.fit-residuals);\n```\n\n### Using function basis\n\nData:\n\n```perl6\nuse Data::Generators;\n\nmy @data = (-1, -0.95 ... 1);\n@data = @data.map({ [$_, sqrt(abs($_/2)) + sin($_*2) + random-real((-0.25, 0.25))] });\n\ntext-list-plot(@data);\n```\n\nBasis functions:\n\n```perl6\nmy @basis = {1}, {$_}, {-1 + 2 * $_ **2}, {-3 * $_ + 4 * $_ **3}, {1 - 8 * $_ ** 2 + 8 * $_ **4};\n```\n\nCompute the fit and show the best fit parameters:\n\n```perl6\nmy \u0026mf = linear-model-fit(@data, :@basis);\n\nsay 'BestFitParameters : ', \u0026mf('BestFitParameters');\n```\n\nPlot the residuals:\n\n```perl6\ntext-list-plot(\u0026mf('FitResiduals'));\n```\n\n(Compare the plot values with the range of the added noise when `@data` is generated.)\n\n------\n\n## TODO\n\n- [ ] TODO Implementation\n  - [X] DONE Multi-dimensional Linear Model Fit (LMF)\n  - [X] DONE Using user specified function basis\n  - [ ] TODO More LMF diagnostics\n  - [ ] TODO CLI for most common data specs\n    - [ ] TODO Just a list of numbers\n    - [ ] TODO String that is Raku full array\n    - [ ] TODO String that is a JSON array\n\n------\n\n## References\n\n[AAp1] Anton Antonov, \n[Data::Geographics Raku package](https://github.com/antononcube/Raku-Data-Geographics),\n(2024),\n[GitHub/antononcube](https://github.com/antononcube).\n\n[AAp2] Anton Antonov, \n[Text::Plot Raku package](https://github.com/antononcube/Raku-Text-Plot),\n(2022-2023),\n[GitHub/antononcube](https://github.com/antononcube).\n\n[PSp1] Paweł Szulc,\n[Statistics::LinearRegression Raku package](https://github.com/hipek8/p6-Statistics-LinearRegression),\n(2017),\n[GitHub/hipek8](https://github.com/hipek8).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantononcube%2Fraku-math-fitting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantononcube%2Fraku-math-fitting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantononcube%2Fraku-math-fitting/lists"}