-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New functions and updated README.md 🚀
* R-squared (rsq) with the possibility of adjusting for parameters - Unit tests have been made. * Huber loss (huberloss) - Unit tests have been made. * README.md have been rewritten and the layout have been updated. - It needs a logo too. NOTE: All the functions where inputs are not altered could benefit from adding a const declaration. This will happen at a later point.
- Loading branch information
Showing
10 changed files
with
277 additions
and
15 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
//' Huber Loss | ||
//' | ||
//' Calculate the huber loss of two <[numeric]> vectors. | ||
//' | ||
//' @param actual A <[numeric]>-vector of length N. | ||
//' @param predicted A <[numeric]>-vector of length N. | ||
//' @param delta A <[numeric]>-vector of length 1. 1 by default. | ||
//' | ||
//' @returns A <[numeric]>-value of length 1. | ||
// [[Rcpp::export]] | ||
double huberloss( | ||
const NumericVector& actual, | ||
const NumericVector& predicted, | ||
const double delta = 1) { | ||
|
||
// Get the size of the vectors | ||
const std::size_t n = actual.size(); | ||
|
||
// Initialize the output variable | ||
double output = 0.0; | ||
|
||
// Iterate over the vectors and compute the Huber loss | ||
for (std::size_t i = 0; i < n; ++i) { | ||
double diff = actual[i] - predicted[i]; | ||
double abs_diff = std::abs(diff); | ||
|
||
if (abs_diff <= delta) { | ||
output += 0.5 * diff * diff; | ||
} else { | ||
output += delta * (abs_diff - 0.5 * delta); | ||
} | ||
} | ||
|
||
// Return the mean Huber loss | ||
return output / n; | ||
} |
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,40 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
//' R squared | ||
//' | ||
//' Calculate the R squared of two <[numeric]> vectors. | ||
//' | ||
//' @param actual A <[numeric]>-vector of length N. | ||
//' @param predicted A <[numeric]>-vector of length N. | ||
//' @param k A <[numeric]>-vector of length 1. 0 by default. If k>0 | ||
//' the function returns the adjusted R squared. | ||
//' | ||
//' @returns A <[numeric]>-value of length 1. | ||
// [[Rcpp::export]] | ||
double rsq( | ||
const NumericVector& actual, | ||
const NumericVector& predicted, | ||
const double k = 0) { | ||
|
||
// Get the size of the vectors | ||
const std::size_t n = actual.size(); | ||
|
||
// Calculate the mean of actual values | ||
double mean_actual = std::accumulate(actual.begin(), actual.end(), 0.0) / n; | ||
|
||
// Calculate SSE and SSR | ||
double SSE = 0.0; | ||
double SST = 0.0; | ||
|
||
for (std::size_t i = 0; i < n; ++i) { | ||
const double actual_val = actual[i]; | ||
const double predicted_val = predicted[i]; | ||
|
||
SSE += (actual_val - predicted_val) * (actual_val - predicted_val); | ||
SST += (actual_val - mean_actual) * (actual_val - mean_actual); | ||
} | ||
|
||
// Calculate R-squared | ||
return 1.0 - (SSE / SST) * ((n - 1))/(n - (k + 1)); | ||
} |
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,43 @@ | ||
# script: Huber Loss test | ||
# author: Serkan Korkmaz, [email protected] | ||
# date: 2024-08-26 | ||
# objective: Test that the huber loss function | ||
# returns a non-zero value | ||
# script start; | ||
|
||
|
||
testthat::test_that( | ||
desc = "`huberloss`-function returns non-zero postive values", | ||
code = { | ||
|
||
# 0) generate values | ||
# from a normal distribution | ||
actual <- rnorm( | ||
n = 1e2 | ||
) | ||
|
||
predicted <- actual + rnorm( | ||
n = 1e2 | ||
) | ||
|
||
|
||
# 1) calculate the | ||
# rsq using rsq()-function | ||
output <- testthat::expect_no_condition( | ||
huberloss( | ||
predicted, | ||
actual, | ||
delta = 5 | ||
) | ||
) | ||
|
||
# 2) test that the value | ||
# is greater than 0 | ||
testthat::expect_true( | ||
output > 0 | ||
) | ||
|
||
} | ||
) | ||
|
||
# script end; |
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,41 @@ | ||
# script: RSQ Tests | ||
# author: Serkan Korkmaz, [email protected] | ||
# date: 2024-08-26 | ||
# objective: Test that rsq returns a non-zero | ||
# positive value for all values passed into it | ||
# script start; | ||
|
||
testthat::test_that( | ||
desc = "`rsq`-function returns non-zero postive values", | ||
code = { | ||
|
||
# 0) generate values | ||
# from a normal distribution | ||
actual <- rnorm( | ||
n = 1e2 | ||
) | ||
|
||
predicted <- actual + rnorm( | ||
n = 1e2 | ||
) | ||
|
||
|
||
# 1) calculate the | ||
# rsq using rsq()-function | ||
output <- testthat::expect_no_condition( | ||
rsq( | ||
predicted, | ||
actual | ||
) | ||
) | ||
|
||
# 2) test that the value | ||
# is greater than 0 | ||
testthat::expect_true( | ||
output > 0 | ||
) | ||
|
||
} | ||
) | ||
|
||
# script end; |