-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: support generation of BH-type lambda sequence
Also add tests for the new sequence and exceptions to check for reasonable input.
- Loading branch information
Showing
5 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,19 +1,27 @@ | ||
#include "regularization_sequence.h" | ||
#include "qnorm.h" | ||
#include <Eigen/Core> | ||
#include <cassert> | ||
|
||
namespace slope { | ||
|
||
Eigen::ArrayXd | ||
lambdaSequence(const int p, const double q) | ||
lambdaSequence(const int p, const double q, const std::string& type) | ||
{ | ||
Eigen::ArrayXd lambda_sequence(p); | ||
Eigen::ArrayXd lambda(p); | ||
|
||
for (int j = 0; j < p; ++j) { | ||
lambda_sequence[j] = normalQuantile(1.0 - (j + 1.0) * q / (2.0 * p)); | ||
if (type == "bh") { | ||
if (q < 0 || q > 1) { | ||
throw std::invalid_argument("q must be between 0 and 1"); | ||
} | ||
|
||
for (int j = 0; j < p; ++j) { | ||
lambda[j] = normalQuantile(1.0 - (j + 1.0) * q / (2.0 * p)); | ||
} | ||
} | ||
|
||
return lambda_sequence; | ||
assert(lambda.minCoeff() > 0); | ||
|
||
return lambda; | ||
} | ||
|
||
} // namespace slope |
This file contains 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 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 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,29 @@ | ||
#include "test_helpers.hpp" | ||
#include <Eigen/Core> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/matchers/catch_matchers.hpp> | ||
#include <slope/regularization_sequence.h> | ||
|
||
TEST_CASE("Test that regularization sequence generation works", | ||
"[regularization sequence]") | ||
{ | ||
SECTION("Test that BH sequence works") | ||
{ | ||
Eigen::ArrayXd lambda1 = slope::lambdaSequence(4, 0.1, "bh"); | ||
|
||
std::vector<double> lambda1_expected = { | ||
2.24140272760495, 1.95996398454005, 1.78046434169203, 1.64485362695147 | ||
}; | ||
|
||
REQUIRE_THAT(lambda1, VectorApproxEqual(lambda1_expected, 1e-6)); | ||
|
||
Eigen::ArrayXd lambda2 = slope::lambdaSequence(6, 0.5, "bh"); | ||
|
||
std::vector<double> lambda2_expected = { | ||
1.73166439612225, 1.38299412710064, 1.15034938037601, | ||
0.967421566101701, 0.812217801499913, 0.674489750196082 | ||
}; | ||
|
||
REQUIRE_THAT(lambda2, VectorApproxEqual(lambda2_expected, 1e-6)); | ||
} | ||
} |