-
Notifications
You must be signed in to change notification settings - Fork 225
feat(linalg): Add weighted and generalized least squares solvers #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
aamrindersingh
wants to merge
3
commits into
fortran-lang:master
from
aamrindersingh:1047-weighted-lstsq
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
efd02d4
feat(linalg): add weighted and generalized least-squares solvers
aamrindersingh 2065886
fix: resolve compilation errors in weighted/generalized lstsq
aamrindersingh 42b28a4
fix(linalg): use column-major loops and harden input validation in we…
aamrindersingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| ! Generalized least-squares solver with correlated errors | ||
| program example_generalized_lstsq | ||
| use stdlib_linalg_constants, only: dp | ||
| use stdlib_linalg, only: generalized_lstsq | ||
| implicit none | ||
|
|
||
| real(dp) :: A(3,2), b(3), W(3,3) | ||
| real(dp), allocatable :: x(:) | ||
|
|
||
| ! Design matrix: intercept + slope | ||
| A(:,1) = 1.0_dp | ||
| A(:,2) = [1.0_dp, 2.0_dp, 3.0_dp] | ||
|
|
||
| ! Observations | ||
| b = [1.0_dp, 2.1_dp, 2.9_dp] | ||
|
|
||
| ! Covariance matrix (correlated errors) | ||
| W(1,:) = [1.0_dp, 0.5_dp, 0.25_dp] | ||
| W(2,:) = [0.5_dp, 1.0_dp, 0.5_dp] | ||
| W(3,:) = [0.25_dp, 0.5_dp, 1.0_dp] | ||
|
|
||
| ! Solve generalized least-squares | ||
| x = generalized_lstsq(W, A, b) | ||
|
|
||
| print '("GLS fit: intercept = ",f8.4,", slope = ",f8.4)', x(1), x(2) | ||
| ! GLS fit: intercept = 0.0500, slope = 0.9500 | ||
|
|
||
| end program example_generalized_lstsq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ! Weighted least-squares solver | ||
| program example_weighted_lstsq | ||
| use stdlib_linalg_constants, only: dp | ||
| use stdlib_linalg, only: weighted_lstsq | ||
| implicit none | ||
|
|
||
| real(dp) :: A(4,2), b(4), w(4) | ||
| real(dp), allocatable :: x(:) | ||
|
|
||
| ! Design matrix: intercept + slope | ||
| A(:,1) = 1.0_dp | ||
| A(:,2) = [1.0_dp, 2.0_dp, 3.0_dp, 4.0_dp] | ||
|
|
||
| ! Observations (one outlier at position 3) | ||
| b = [2.1_dp, 4.0_dp, 10.0_dp, 7.9_dp] | ||
|
|
||
| ! Weights: downweight the outlier | ||
| w = [1.0_dp, 1.0_dp, 0.1_dp, 1.0_dp] | ||
|
|
||
| ! Solve weighted least-squares | ||
| x = weighted_lstsq(w, A, b) | ||
|
|
||
| print '("Weighted fit: intercept = ",f8.4,", slope = ",f8.4)', x(1), x(2) | ||
| ! Weighted fit: intercept = 0.0667, slope = 1.9556 | ||
|
|
||
| end program example_weighted_lstsq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @aamrindersingh . Could you split this PR in two smaller one (
weighted_lstsqandgeneralized_lstsq), please? Or are these two implementations dependent of each other?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, they are not dependent on each other , Will start on splitting them into two PRs.
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jvdp1, Below are the Two PR's
weighted_lstsqgeneralized_lstsq