From be2403523d5060353bb18157ca3dca614640021e Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Wed, 29 Dec 2021 16:39:38 +0530 Subject: [PATCH 01/17] tried to remove quantile (revert if not working) --- src/mlpack/core/math/quantile.hpp | 87 +++++++++++++++++++ .../core/tree/cosine_tree/cosine_tree.cpp | 5 +- src/mlpack/methods/kde/kde_rules_impl.hpp | 10 +-- 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 src/mlpack/core/math/quantile.hpp diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp new file mode 100644 index 0000000000..4d9b702a32 --- /dev/null +++ b/src/mlpack/core/math/quantile.hpp @@ -0,0 +1,87 @@ +/** + * @file core/math/quantile.hpp + * @author Shubham Agrawal + * + * Miscellaneous math quantile-related routines. + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ +#ifndef MLPACK_CORE_MATH_QUANTILE_HPP +#define MLPACK_CORE_MATH_QUANTILE_HPP + +#include + +namespace mlpack { +namespace math /** Miscellaneous math routines. */ { + +/** + * Computes the inverse erf function using the rational approximation from + * Numerical Recipes. + * + * @param x Input value. + */ +double erfinv(double x) +{ + const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; + const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; + const double c[] = {-1.970840454, -1.62490649, 3.429567803, 1.641345311}; + const double d[] = {1.0, 3.543889200, 1.637067800}; + double x2, r, y; + int sign_x; + if (x < -1 || x > 1) + return NAN; + + if (x == 0) + return 0; + + if (x > 0) + { + sign_x = 1; + } + else + { + sign_x = -1; + x = -x; + } + + if (x <= 0.7) + { + x2 = x * x; + r = x * (((a[3] * x2 + a[2]) * x2 + a[1]) * x2 + a[0]); + r /= (((b[4] * x2 + b[3]) * x2 + b[2]) * x2 + b[1]) * x2 + b[0]; + } + else + { + y = std::sqrt (-std::log ((1 - x) / 2)); + r = (((c[3] * y + c[2]) * y + c[1]) * y + c[0]); + r /= ((d[2] * y + d[1]) * y + d[0]); + } + + r = r * sign_x; + x = x * sign_x; + + r -= (std::erf (r) - x) / (2 / std::sqrt (M_PI) * std::exp (-r * r)); + r -= (std::erf (r) - x) / (2 / std::sqrt (M_PI) * std::exp (-r * r)); + + return r; +} + +/** + * Computes the quantile function of Guassian distribution at given probability. + * + * @param p Probability value. + * @param mu Mean of the distribution. (Default 0) + * @param sigma Standard deviation of the distribution. (Default 1) + */ +inline double quantile(double p, double mu = 0.0, double sigma = 1.0) +{ + return mu + sigma * std::sqrt(2.0) * erfinv(2 * p - 1); +} + +} // namespace math +} // namespace mlpack + +#endif diff --git a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp index 1231850e85..c7e4c8d3cf 100644 --- a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp +++ b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp @@ -12,7 +12,7 @@ #include "cosine_tree.hpp" #include -#include +#include namespace mlpack { namespace tree { @@ -477,8 +477,7 @@ double CosineTree::MonteCarloError(CosineTree* node, // Fit a normal distribution using the calculated statistics, and calculate a // lower bound on the magnitudes for the passed 'delta' parameter. - boost::math::normal dist(mu, sigma); - double lowerBound = boost::math::quantile(dist, delta); + double lowerBound = math::quantile(delta, mu, sigma); // Upper bound on the subspace reconstruction error. node->L2Error(node->FrobNormSquared() - lowerBound); diff --git a/src/mlpack/methods/kde/kde_rules_impl.hpp b/src/mlpack/methods/kde/kde_rules_impl.hpp index 38a719d4ee..cd8200445c 100644 --- a/src/mlpack/methods/kde/kde_rules_impl.hpp +++ b/src/mlpack/methods/kde/kde_rules_impl.hpp @@ -17,7 +17,7 @@ #include "kde_rules.hpp" // Used for Monte Carlo estimation. -#include +#include namespace mlpack { namespace kde { @@ -191,9 +191,7 @@ Score(const size_t queryIndex, TreeType& referenceNode) // Monte Carlo probabilistic estimation. // Calculate z using accumulated alpha if possible. const double alpha = depthAlpha + accumMCAlpha(queryIndex); - const boost::math::normal normalDist; - const double z = - std::abs(boost::math::quantile(normalDist, alpha / 2)); + const double z = std::abs(math::quantile(alpha / 2.0)); // Auxiliary variables. arma::vec sample; @@ -400,9 +398,7 @@ Score(TreeType& queryNode, TreeType& referenceNode) // Monte Carlo probabilistic estimation. // Calculate z using accumulated alpha if possible. const double alpha = depthAlpha + queryStat.AccumAlpha(); - const boost::math::normal normalDist; - const double z = - std::abs(boost::math::quantile(normalDist, alpha / 2)); + const double z = std::abs(math::quantile(alpha / 2)); // Auxiliary variables. arma::vec sample; From 54e4e6c79ec308c530f1b3d0da0214f2fce10378 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Wed, 29 Dec 2021 16:50:29 +0530 Subject: [PATCH 02/17] added new file in cmake --- src/mlpack/core.hpp | 1 + src/mlpack/core/math/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp index d29a174724..a2275ad188 100644 --- a/src/mlpack/core.hpp +++ b/src/mlpack/core.hpp @@ -83,6 +83,7 @@ #include #include #include +#include #include #include #include diff --git a/src/mlpack/core/math/CMakeLists.txt b/src/mlpack/core/math/CMakeLists.txt index 6134d5f583..28131b2f31 100644 --- a/src/mlpack/core/math/CMakeLists.txt +++ b/src/mlpack/core/math/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES make_alias.hpp multiply_slices_impl.hpp multiply_slices.hpp + quantile.hpp random.hpp random.cpp random_basis.hpp From 6a7abfc5fddfb2b9806dc9c3beb862b8838a1656 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Wed, 29 Dec 2021 17:30:10 +0530 Subject: [PATCH 03/17] removed replace_all_copy --- src/mlpack/bindings/markdown/print_doc_functions.hpp | 5 +++++ .../bindings/markdown/print_doc_functions_impl.hpp | 11 +++++++++++ src/mlpack/bindings/markdown/print_docs.cpp | 12 +++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/mlpack/bindings/markdown/print_doc_functions.hpp b/src/mlpack/bindings/markdown/print_doc_functions.hpp index 6c5a7384a6..41426aa9e1 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions.hpp @@ -107,6 +107,11 @@ inline std::string ParamType(util::Params& p, util::ParamData& d); template inline bool IgnoreCheck(const std::string& bindingName, const T& t); +/** + * Replace string utility function. + */ +inline std::string ReplaceAll(std::string str, const std::string& from, const std::string& to); + } // namespace markdown } // namespace bindings } // namespace mlpack diff --git a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp index e5b6952ac2..cbe45590b0 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp @@ -766,6 +766,17 @@ inline bool IgnoreCheck(const std::string& bindingName, const T& t) } } +inline std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { + size_t start_pos = 0; + if (from.size() == 0) + return str; + while((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // Handles case where 'to' is a substring of 'from' + } + return str; +} + } // namespace markdown } // namespace bindings } // namespace mlpack diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 19f45b5b4f..3672b6dbec 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -12,8 +12,6 @@ #include #include -#include - #include "binding_info.hpp" #include "print_docs.hpp" #include "print_doc_functions.hpp" @@ -158,7 +156,7 @@ void PrintDocs(const std::string& bindingName, cout << ParamString(bindingName, it->second.name) << " | "; } cout << ParamType(params, it->second) << " | "; - string desc = boost::replace_all_copy(it->second.desc, "|", "\\|"); + string desc = ReplaceAll(it->second.desc, string("|"), string("\\|")); cout << desc; // just a string // Print whether or not it's a "special" language-only parameter. if (it->second.name == "copy_all_inputs" || it->second.name == "help" || @@ -240,16 +238,16 @@ void PrintDocs(const std::string& bindingName, cout << "{: #" << languages[i] << "_" << bindingName << "_detailed-documentation }" << endl; cout << endl; - string desc = boost::replace_all_copy(doc.longDescription(), - "|", "\\|"); + string desc = ReplaceAll(doc.longDescription(), + string("|"), string("\\|")); cout << desc << endl << endl; if (doc.example.size() > 0) cout << "### Example" << endl; for (size_t j = 0; j < doc.example.size(); ++j) { - string eg = boost::replace_all_copy(doc.example[j](), - "|", "\\|"); + string eg = ReplaceAll(doc.example[j](), + string("|"), string("\\|")); cout << eg << endl << endl; } cout << "### See also" << endl; From f11f2bfa108aab458b586c483ecc9a7bf1d4e0c7 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Wed, 29 Dec 2021 20:00:43 +0530 Subject: [PATCH 04/17] replaced format boost --- .../preprocess/preprocess_describe_main.cpp | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp index 04faf5fe9c..10c0f8f1f1 100644 --- a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp @@ -19,14 +19,12 @@ #include -#include -#include +#include using namespace mlpack; using namespace mlpack::data; using namespace mlpack::util; using namespace std; -using namespace boost; // Program Name. BINDING_USER_NAME("Descriptive Statistics"); @@ -85,6 +83,23 @@ PARAM_FLAG("row_major", "If specified, the program will calculate statistics " "across rows, not across columns. (Remember that in mlpack, a column " "represents a point, so this option is generally not necessary.)", "r"); +/** + * Formats string similar to printf. + * + * @param format Format of string desired. + * @param args Args for inserting as parameter. + * @return Desired Formatted String. + */ +template +std::string string_format(const std::string& format, Args ... args) +{ + size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' + if( size <= 0 ){ throw std::runtime_error( "Error during formatting." ); } + std::unique_ptr buf( new char[ size ] ); + snprintf( buf.get(), size, format.c_str(), args ... ); + return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside +} + /** * Calculates the sum of deviations to the Nth Power. * @@ -187,8 +202,8 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) // Load the data. arma::mat& data = params.Get("input"); - // Generate boost format recipe. - const string widthPrecision("%-" + to_string(width) + "." + + // Generate format recipe. + const string widthPrecision("%-" + to_string(width) + to_string(precision)); const string widthOnly("%-" + to_string(width) + "."); string stringFormat = ""; @@ -203,9 +218,8 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) timers.Start("statistics"); // Print the headers. - Log::Info << boost::format(stringFormat) - % "dim" % "var" % "mean" % "std" % "median" % "min" % "max" - % "range" % "skew" % "kurt" % "SE" << endl; + Log::Info << string_format(stringFormat, "dim", "var", "mean", + "std", "median", "min", "max", "range", "skew", "kurt", "SE") << endl; // Lambda function to print out the results. auto PrintStatResults = [&](size_t dim, bool rowMajor) @@ -223,19 +237,12 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const double fStd = arma::stddev(feature, population); // Print statistics of the given dimension. - Log::Info << boost::format(numberFormat) - % dim - % arma::var(feature, population) - % fMean - % fStd - % arma::median(feature) - % fMin - % fMax - % (fMax - fMin) // range - % Skewness(feature, fStd, fMean, population) - % Kurtosis(feature, fStd, fMean, population) - % StandardError(feature.n_elem, fStd) - << endl; + Log::Info << string_format(numberFormat, + dim, arma::var(feature, population), fMean, fStd, + arma::median(feature), fMin, fMax, (fMax - fMin), + Skewness(feature, fStd, fMean, population), + Kurtosis(feature, fStd, fMean, population), + StandardError(feature.n_elem, fStd)) << endl; }; // If the user specified dimension, describe statistics of the given From 7063b230c7019f0466490ca22f271ce3056144b3 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Wed, 29 Dec 2021 13:34:48 +0530 Subject: [PATCH 05/17] removed boost iround --- src/mlpack/tests/main_tests/emst_test.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mlpack/tests/main_tests/emst_test.cpp b/src/mlpack/tests/main_tests/emst_test.cpp index 3cf3a8802a..6240ad5f68 100644 --- a/src/mlpack/tests/main_tests/emst_test.cpp +++ b/src/mlpack/tests/main_tests/emst_test.cpp @@ -18,8 +18,6 @@ #include "../catch.hpp" -#include - using namespace mlpack; BINDING_TEST_FIXTURE(EMSTTestFixture); @@ -106,10 +104,10 @@ TEST_CASE_METHOD(EMSTTestFixture, "EMSTFirstTwoOutputRowsIntegerTest", for (size_t i = 0; i < params.Get("output").n_cols; ++i) { REQUIRE(params.Get("output")(0, i) == - Approx(boost::math::iround(params.Get("output")(0, i))). + Approx((int)std::round(params.Get("output")(0, i))). epsilon(1e-7)); REQUIRE(params.Get("output")(1, i) == - Approx(boost::math::iround(params.Get("output")(1, i))). + Approx((int)std::round(params.Get("output")(1, i))). epsilon(1e-7)); } } From b7ee32e876b431ffab61524166a2b61581aaa80e Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sat, 15 Jan 2022 21:08:55 +0530 Subject: [PATCH 06/17] indendation and type casting fix --- src/mlpack/core/math/quantile.hpp | 30 +++++++++++------------ src/mlpack/tests/main_tests/emst_test.cpp | 4 +-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index 4d9b702a32..af563d5260 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -25,36 +25,36 @@ namespace math /** Miscellaneous math routines. */ { */ double erfinv(double x) { - const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; - const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; - const double c[] = {-1.970840454, -1.62490649, 3.429567803, 1.641345311}; - const double d[] = {1.0, 3.543889200, 1.637067800}; - double x2, r, y; + const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; + const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; + const double c[] = {-1.970840454, -1.62490649, 3.429567803, 1.641345311}; + const double d[] = {1.0, 3.543889200, 1.637067800}; + double x2, r, y; int sign_x; - if (x < -1 || x > 1) - return NAN; - + if (x < -1 || x > 1) + return NAN; + if (x == 0) return 0; if (x > 0) - { + { sign_x = 1; - } + } else - { + { sign_x = -1; x = -x; } if (x <= 0.7) - { + { x2 = x * x; r = x * (((a[3] * x2 + a[2]) * x2 + a[1]) * x2 + a[0]); r /= (((b[4] * x2 + b[3]) * x2 + b[2]) * x2 + b[1]) * x2 + b[0]; } - else - { + else + { y = std::sqrt (-std::log ((1 - x) / 2)); r = (((c[3] * y + c[2]) * y + c[1]) * y + c[0]); r /= ((d[2] * y + d[1]) * y + d[0]); @@ -78,7 +78,7 @@ double erfinv(double x) */ inline double quantile(double p, double mu = 0.0, double sigma = 1.0) { - return mu + sigma * std::sqrt(2.0) * erfinv(2 * p - 1); + return mu + sigma * std::sqrt(2.0) * erfinv(2 * p - 1); } } // namespace math diff --git a/src/mlpack/tests/main_tests/emst_test.cpp b/src/mlpack/tests/main_tests/emst_test.cpp index 6240ad5f68..1a897fa902 100644 --- a/src/mlpack/tests/main_tests/emst_test.cpp +++ b/src/mlpack/tests/main_tests/emst_test.cpp @@ -104,10 +104,10 @@ TEST_CASE_METHOD(EMSTTestFixture, "EMSTFirstTwoOutputRowsIntegerTest", for (size_t i = 0; i < params.Get("output").n_cols; ++i) { REQUIRE(params.Get("output")(0, i) == - Approx((int)std::round(params.Get("output")(0, i))). + Approx(static_cast(std::round(params.Get("output")(0, i)))). epsilon(1e-7)); REQUIRE(params.Get("output")(1, i) == - Approx((int)std::round(params.Get("output")(1, i))). + Approx(static_cast(std::round(params.Get("output")(1, i)))). epsilon(1e-7)); } } From 6f5c8ec2d8f4590a09a6bd61388f16bf85edd9b3 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sat, 15 Jan 2022 21:46:07 +0530 Subject: [PATCH 07/17] trying to remove error --- src/mlpack/core/math/quantile.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index af563d5260..fabdca36be 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -23,7 +23,7 @@ namespace math /** Miscellaneous math routines. */ { * * @param x Input value. */ -double erfinv(double x) +double erfinverse(double x) { const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; @@ -78,7 +78,7 @@ double erfinv(double x) */ inline double quantile(double p, double mu = 0.0, double sigma = 1.0) { - return mu + sigma * std::sqrt(2.0) * erfinv(2 * p - 1); + return mu + sigma * std::sqrt(2.0) * erfinverse(2 * p - 1); } } // namespace math From 0cd2d635abd348fc470e30514ef08fb94c7ef5e8 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sat, 15 Jan 2022 21:53:20 +0530 Subject: [PATCH 08/17] removed inline from quantile function --- src/mlpack/core/math/quantile.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index fabdca36be..b6b7935c75 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -76,7 +76,7 @@ double erfinverse(double x) * @param mu Mean of the distribution. (Default 0) * @param sigma Standard deviation of the distribution. (Default 1) */ -inline double quantile(double p, double mu = 0.0, double sigma = 1.0) +double quantile(double p, double mu = 0.0, double sigma = 1.0) { return mu + sigma * std::sqrt(2.0) * erfinverse(2 * p - 1); } From d2e639fef213d879728a1d6e114d501500c0816b Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sat, 15 Jan 2022 22:22:24 +0530 Subject: [PATCH 09/17] conv fn to inline --- src/mlpack/core/math/quantile.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index b6b7935c75..eaf36e8e2e 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -23,7 +23,7 @@ namespace math /** Miscellaneous math routines. */ { * * @param x Input value. */ -double erfinverse(double x) +inline double erfinverse(double x) { const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; @@ -76,7 +76,7 @@ double erfinverse(double x) * @param mu Mean of the distribution. (Default 0) * @param sigma Standard deviation of the distribution. (Default 1) */ -double quantile(double p, double mu = 0.0, double sigma = 1.0) +inline double quantile(double p, double mu = 0.0, double sigma = 1.0) { return mu + sigma * std::sqrt(2.0) * erfinverse(2 * p - 1); } From 5e2961022508c6c5c8f8e58868c27c4796fb255c Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Fri, 21 Jan 2022 18:02:04 +0530 Subject: [PATCH 10/17] Revert "removed replace_all_copy" This reverts commit 6a7abfc5fddfb2b9806dc9c3beb862b8838a1656. --- src/mlpack/bindings/markdown/print_doc_functions.hpp | 5 ----- .../bindings/markdown/print_doc_functions_impl.hpp | 11 ----------- src/mlpack/bindings/markdown/print_docs.cpp | 12 +++++++----- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/mlpack/bindings/markdown/print_doc_functions.hpp b/src/mlpack/bindings/markdown/print_doc_functions.hpp index 41426aa9e1..6c5a7384a6 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions.hpp @@ -107,11 +107,6 @@ inline std::string ParamType(util::Params& p, util::ParamData& d); template inline bool IgnoreCheck(const std::string& bindingName, const T& t); -/** - * Replace string utility function. - */ -inline std::string ReplaceAll(std::string str, const std::string& from, const std::string& to); - } // namespace markdown } // namespace bindings } // namespace mlpack diff --git a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp index cbe45590b0..e5b6952ac2 100644 --- a/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/markdown/print_doc_functions_impl.hpp @@ -766,17 +766,6 @@ inline bool IgnoreCheck(const std::string& bindingName, const T& t) } } -inline std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { - size_t start_pos = 0; - if (from.size() == 0) - return str; - while((start_pos = str.find(from, start_pos)) != std::string::npos) { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); // Handles case where 'to' is a substring of 'from' - } - return str; -} - } // namespace markdown } // namespace bindings } // namespace mlpack diff --git a/src/mlpack/bindings/markdown/print_docs.cpp b/src/mlpack/bindings/markdown/print_docs.cpp index 3672b6dbec..19f45b5b4f 100644 --- a/src/mlpack/bindings/markdown/print_docs.cpp +++ b/src/mlpack/bindings/markdown/print_docs.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include "binding_info.hpp" #include "print_docs.hpp" #include "print_doc_functions.hpp" @@ -156,7 +158,7 @@ void PrintDocs(const std::string& bindingName, cout << ParamString(bindingName, it->second.name) << " | "; } cout << ParamType(params, it->second) << " | "; - string desc = ReplaceAll(it->second.desc, string("|"), string("\\|")); + string desc = boost::replace_all_copy(it->second.desc, "|", "\\|"); cout << desc; // just a string // Print whether or not it's a "special" language-only parameter. if (it->second.name == "copy_all_inputs" || it->second.name == "help" || @@ -238,16 +240,16 @@ void PrintDocs(const std::string& bindingName, cout << "{: #" << languages[i] << "_" << bindingName << "_detailed-documentation }" << endl; cout << endl; - string desc = ReplaceAll(doc.longDescription(), - string("|"), string("\\|")); + string desc = boost::replace_all_copy(doc.longDescription(), + "|", "\\|"); cout << desc << endl << endl; if (doc.example.size() > 0) cout << "### Example" << endl; for (size_t j = 0; j < doc.example.size(); ++j) { - string eg = ReplaceAll(doc.example[j](), - string("|"), string("\\|")); + string eg = boost::replace_all_copy(doc.example[j](), + "|", "\\|"); cout << eg << endl << endl; } cout << "### See also" << endl; From 78dc546e0c9199ed483d87a3beaea7323e848e2e Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Mon, 24 Jan 2022 23:10:46 +0530 Subject: [PATCH 11/17] replaced erfinv code due to license issue --- src/mlpack/core/math/quantile.hpp | 106 ++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 35 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index eaf36e8e2e..057e7a539a 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -20,53 +20,89 @@ namespace math /** Miscellaneous math routines. */ { /** * Computes the inverse erf function using the rational approximation from * Numerical Recipes. + * Code accompanying the article "Approximating the erfinv function" in + * GPU Computing Gems, Volume 2 * * @param x Input value. */ inline double erfinverse(double x) { - const double a[] = {0.886226899, -1.645349621, 0.914624893, -0.140543331}; - const double b[] = {1.0, -2.118377725, 1.442710462, -0.329097515, 0.012229801}; - const double c[] = {-1.970840454, -1.62490649, 3.429567803, 1.641345311}; - const double d[] = {1.0, 3.543889200, 1.637067800}; - double x2, r, y; - int sign_x; - if (x < -1 || x > 1) - return NAN; - - if (x == 0) - return 0; + double w, p; - if (x > 0) - { - sign_x = 1; - } - else - { - sign_x = -1; - x = -x; - } + w = -log((1.0 - x) * (1.0 + x)); - if (x <= 0.7) + if (w < 6.250000) { - x2 = x * x; - r = x * (((a[3] * x2 + a[2]) * x2 + a[1]) * x2 + a[0]); - r /= (((b[4] * x2 + b[3]) * x2 + b[2]) * x2 + b[1]) * x2 + b[0]; + w = w - 3.125000; + p = -3.6444120640178196996e-21; + p = -1.685059138182016589e-19 + p * w; + p = 1.2858480715256400167e-18 + p * w; + p = 1.115787767802518096e-17 + p * w; + p = -1.333171662854620906e-16 + p * w; + p = 2.0972767875968561637e-17 + p * w; + p = 6.6376381343583238325e-15 + p * w; + p = -4.0545662729752068639e-14 + p * w; + p = -8.1519341976054721522e-14 + p * w; + p = 2.6335093153082322977e-12 + p * w; + p = -1.2975133253453532498e-11 + p * w; + p = -5.4154120542946279317e-11 + p * w; + p = 1.051212273321532285e-09 + p * w; + p = -4.1126339803469836976e-09 + p * w; + p = -2.9070369957882005086e-08 + p * w; + p = 4.2347877827932403518e-07 + p * w; + p = -1.3654692000834678645e-06 + p * w; + p = -1.3882523362786468719e-05 + p * w; + p = 0.0001867342080340571352 + p * w; + p = -0.00074070253416626697512 + p * w; + p = -0.0060336708714301490533 + p * w; + p = 0.24015818242558961693 + p * w; + p = 1.6536545626831027356 + p * w; + } + else if (w < 16.000000) + { + w = sqrt(w) - 3.250000; + p = 2.2137376921775787049e-09; + p = 9.0756561938885390979e-08 + p * w; + p = -2.7517406297064545428e-07 + p * w; + p = 1.8239629214389227755e-08 + p * w; + p = 1.5027403968909827627e-06 + p * w; + p = -4.013867526981545969e-06 + p * w; + p = 2.9234449089955446044e-06 + p * w; + p = 1.2475304481671778723e-05 + p * w; + p = -4.7318229009055733981e-05 + p * w; + p = 6.8284851459573175448e-05 + p * w; + p = 2.4031110387097893999e-05 + p * w; + p = -0.0003550375203628474796 + p * w; + p = 0.00095328937973738049703 + p * w; + p = -0.0016882755560235047313 + p * w; + p = 0.0024914420961078508066 + p * w; + p = -0.0037512085075692412107 + p * w; + p = 0.005370914553590063617 + p * w; + p = 1.0052589676941592334 + p * w; + p = 3.0838856104922207635 + p * w; } else { - y = std::sqrt (-std::log ((1 - x) / 2)); - r = (((c[3] * y + c[2]) * y + c[1]) * y + c[0]); - r /= ((d[2] * y + d[1]) * y + d[0]); + w = sqrt(w) - 5.000000; + p = -2.7109920616438573243e-11; + p = -2.5556418169965252055e-10 + p * w; + p = 1.5076572693500548083e-09 + p * w; + p = -3.7894654401267369937e-09 + p * w; + p = 7.6157012080783393804e-09 + p * w; + p = -1.4960026627149240478e-08 + p * w; + p = 2.9147953450901080826e-08 + p * w; + p = -6.7711997758452339498e-08 + p * w; + p = 2.2900482228026654717e-07 + p * w; + p = -9.9298272942317002539e-07 + p * w; + p = 4.5260625972231537039e-06 + p * w; + p = -1.9681778105531670567e-05 + p * w; + p = 7.5995277030017761139e-05 + p * w; + p = -0.00021503011930044477347 + p * w; + p = -0.00013871931833623122026 + p * w; + p = 1.0103004648645343977 + p * w; + p = 4.8499064014085844221 + p * w; } - - r = r * sign_x; - x = x * sign_x; - - r -= (std::erf (r) - x) / (2 / std::sqrt (M_PI) * std::exp (-r * r)); - r -= (std::erf (r) - x) / (2 / std::sqrt (M_PI) * std::exp (-r * r)); - - return r; + return p * x; } /** From 031bb7ac3c5963a9fc0871720f26ff553144a7b5 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Mon, 24 Jan 2022 23:17:16 +0530 Subject: [PATCH 12/17] fix styling issues --- .../methods/preprocess/preprocess_describe_main.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp index 10c0f8f1f1..4031fdf9b0 100644 --- a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp @@ -93,11 +93,14 @@ PARAM_FLAG("row_major", "If specified, the program will calculate statistics " template std::string string_format(const std::string& format, Args ... args) { - size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' - if( size <= 0 ){ throw std::runtime_error( "Error during formatting." ); } - std::unique_ptr buf( new char[ size ] ); - snprintf( buf.get(), size, format.c_str(), args ... ); - return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside + size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; + if (size <= 0) + { + Log::Fatal << "Error during formatting." << std::endl; + } + std::unique_ptr buf(new char[ size ]); + snprintf(buf.get(), size, format.c_str(), args ...); + return std::string(buf.get(), buf.get() + size - 1); } /** From 810dcc935679a329e8d72f48681adbe33e4b3262 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sat, 29 Jan 2022 14:09:47 +0530 Subject: [PATCH 13/17] removed extra implementation and added iomanip --- .../preprocess/preprocess_describe_main.cpp | 51 ++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp index 4031fdf9b0..43014d2d1e 100644 --- a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp @@ -19,7 +19,7 @@ #include -#include +#include using namespace mlpack; using namespace mlpack::data; @@ -83,26 +83,6 @@ PARAM_FLAG("row_major", "If specified, the program will calculate statistics " "across rows, not across columns. (Remember that in mlpack, a column " "represents a point, so this option is generally not necessary.)", "r"); -/** - * Formats string similar to printf. - * - * @param format Format of string desired. - * @param args Args for inserting as parameter. - * @return Desired Formatted String. - */ -template -std::string string_format(const std::string& format, Args ... args) -{ - size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; - if (size <= 0) - { - Log::Fatal << "Error during formatting." << std::endl; - } - std::unique_ptr buf(new char[ size ]); - snprintf(buf.get(), size, format.c_str(), args ...); - return std::string(buf.get(), buf.get() + size - 1); -} - /** * Calculates the sum of deviations to the Nth Power. * @@ -221,8 +201,11 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) timers.Start("statistics"); // Print the headers. - Log::Info << string_format(stringFormat, "dim", "var", "mean", - "std", "median", "min", "max", "range", "skew", "kurt", "SE") << endl; + Log::Info << setw(width) << "dim" << setw(width) << "var" << setw(width) << + "mean" << setw(width) << "std" << setw(width) << setw(width) << setw(width) << + "median" << setw(width) << "min" << setw(width) << "max" << setw(width) << + "range" << setw(width) << "skew" << setw(width) << "kurt" << setw(width) << + "SE" << endl; // Lambda function to print out the results. auto PrintStatResults = [&](size_t dim, bool rowMajor) @@ -240,12 +223,22 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const double fStd = arma::stddev(feature, population); // Print statistics of the given dimension. - Log::Info << string_format(numberFormat, - dim, arma::var(feature, population), fMean, fStd, - arma::median(feature), fMin, fMax, (fMax - fMin), - Skewness(feature, fStd, fMean, population), - Kurtosis(feature, fStd, fMean, population), - StandardError(feature.n_elem, fStd)) << endl; + Log::Info << setw(width) << setprecision(precision) << numberFormat << + setw(width) << setprecision(precision) << dim << + setw(width) << setprecision(precision) << + arma::var(feature, population) << + setw(width) << setprecision(precision) << fMean << + setw(width) << setprecision(precision) << fStd << + setw(width) << setprecision(precision) << arma::median(feature) << + setw(width) << setprecision(precision) << fMin << + setw(width) << setprecision(precision) << fMax << + setw(width) << setprecision(precision) << (fMax - fMin) << + setw(width) << setprecision(precision) << + Skewness(feature, fStd, fMean, population) << + setw(width) << setprecision(precision) << + Kurtosis(feature, fStd, fMean, population) << + setw(width) << setprecision(precision) << + StandardError(feature.n_elem, fStd) << endl; }; // If the user specified dimension, describe statistics of the given From 074ab0f5c5b496a97bedbd5658625928bd8b91c8 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sun, 6 Mar 2022 19:45:34 +0530 Subject: [PATCH 14/17] changing fn name acc to convention --- src/mlpack/core/math/quantile.hpp | 6 +++--- src/mlpack/core/tree/cosine_tree/cosine_tree.cpp | 2 +- src/mlpack/methods/kde/kde_rules_impl.hpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index 057e7a539a..d1178dd213 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -25,7 +25,7 @@ namespace math /** Miscellaneous math routines. */ { * * @param x Input value. */ -inline double erfinverse(double x) +inline double ErfInverse(double x) { double w, p; @@ -112,9 +112,9 @@ inline double erfinverse(double x) * @param mu Mean of the distribution. (Default 0) * @param sigma Standard deviation of the distribution. (Default 1) */ -inline double quantile(double p, double mu = 0.0, double sigma = 1.0) +inline double Quantile(double p, double mu = 0.0, double sigma = 1.0) { - return mu + sigma * std::sqrt(2.0) * erfinverse(2 * p - 1); + return mu + sigma * std::sqrt(2.0) * ErfInverse(2 * p - 1); } } // namespace math diff --git a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp index c7e4c8d3cf..878d621b1d 100644 --- a/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp +++ b/src/mlpack/core/tree/cosine_tree/cosine_tree.cpp @@ -477,7 +477,7 @@ double CosineTree::MonteCarloError(CosineTree* node, // Fit a normal distribution using the calculated statistics, and calculate a // lower bound on the magnitudes for the passed 'delta' parameter. - double lowerBound = math::quantile(delta, mu, sigma); + double lowerBound = math::Quantile(delta, mu, sigma); // Upper bound on the subspace reconstruction error. node->L2Error(node->FrobNormSquared() - lowerBound); diff --git a/src/mlpack/methods/kde/kde_rules_impl.hpp b/src/mlpack/methods/kde/kde_rules_impl.hpp index cd8200445c..062e740efa 100644 --- a/src/mlpack/methods/kde/kde_rules_impl.hpp +++ b/src/mlpack/methods/kde/kde_rules_impl.hpp @@ -191,7 +191,7 @@ Score(const size_t queryIndex, TreeType& referenceNode) // Monte Carlo probabilistic estimation. // Calculate z using accumulated alpha if possible. const double alpha = depthAlpha + accumMCAlpha(queryIndex); - const double z = std::abs(math::quantile(alpha / 2.0)); + const double z = std::abs(math::Quantile(alpha / 2.0)); // Auxiliary variables. arma::vec sample; @@ -398,7 +398,7 @@ Score(TreeType& queryNode, TreeType& referenceNode) // Monte Carlo probabilistic estimation. // Calculate z using accumulated alpha if possible. const double alpha = depthAlpha + queryStat.AccumAlpha(); - const double z = std::abs(math::quantile(alpha / 2)); + const double z = std::abs(math::Quantile(alpha / 2)); // Auxiliary variables. arma::vec sample; From d805cadbe5f8c23397e6697308de182017dde229 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sun, 6 Mar 2022 19:52:09 +0530 Subject: [PATCH 15/17] removing extra calls of set precision, and removed unused code --- .../preprocess/preprocess_describe_main.cpp | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp index 43014d2d1e..b857674e31 100644 --- a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp @@ -223,22 +223,18 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const double fStd = arma::stddev(feature, population); // Print statistics of the given dimension. - Log::Info << setw(width) << setprecision(precision) << numberFormat << - setw(width) << setprecision(precision) << dim << - setw(width) << setprecision(precision) << - arma::var(feature, population) << - setw(width) << setprecision(precision) << fMean << - setw(width) << setprecision(precision) << fStd << - setw(width) << setprecision(precision) << arma::median(feature) << - setw(width) << setprecision(precision) << fMin << - setw(width) << setprecision(precision) << fMax << - setw(width) << setprecision(precision) << (fMax - fMin) << - setw(width) << setprecision(precision) << - Skewness(feature, fStd, fMean, population) << - setw(width) << setprecision(precision) << - Kurtosis(feature, fStd, fMean, population) << - setw(width) << setprecision(precision) << - StandardError(feature.n_elem, fStd) << endl; + Log::Info << setprecision(precision) << setw(width) << numberFormat << + setw(width) << dim << + setw(width) << arma::var(feature, population) << + setw(width) << fMean << + setw(width) << fStd << + setw(width) << arma::median(feature) << + setw(width) << fMin << + setw(width) << fMax << + setw(width) << (fMax - fMin) << + setw(width) << Skewness(feature, fStd, fMean, population) << + setw(width) << Kurtosis(feature, fStd, fMean, population) << + setw(width) << StandardError(feature.n_elem, fStd) << endl; }; // If the user specified dimension, describe statistics of the given From 58a7d5abd7f3cd0519f11487d828f23f2be4c768 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Sun, 6 Mar 2022 19:53:02 +0530 Subject: [PATCH 16/17] removing extra line --- .../preprocess/preprocess_describe_main.cpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp index b857674e31..2859190167 100644 --- a/src/mlpack/methods/preprocess/preprocess_describe_main.cpp +++ b/src/mlpack/methods/preprocess/preprocess_describe_main.cpp @@ -185,20 +185,6 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) // Load the data. arma::mat& data = params.Get("input"); - // Generate format recipe. - const string widthPrecision("%-" + to_string(width) + - to_string(precision)); - const string widthOnly("%-" + to_string(width) + "."); - string stringFormat = ""; - string numberFormat = ""; - - // We are going to print 11 different categories. - for (size_t i = 0; i < 11; ++i) - { - stringFormat += widthOnly + "s"; - numberFormat += widthPrecision + "f"; - } - timers.Start("statistics"); // Print the headers. Log::Info << setw(width) << "dim" << setw(width) << "var" << setw(width) << @@ -223,8 +209,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers) const double fStd = arma::stddev(feature, population); // Print statistics of the given dimension. - Log::Info << setprecision(precision) << setw(width) << numberFormat << - setw(width) << dim << + Log::Info << setprecision(precision) << setw(width) << dim << setw(width) << arma::var(feature, population) << setw(width) << fMean << setw(width) << fStd << From 7b12966f11018323412a45d886aa081b56faca62 Mon Sep 17 00:00:00 2001 From: shubham1206agra Date: Thu, 24 Mar 2022 10:30:53 +0530 Subject: [PATCH 17/17] Removed static_cast as approx uses double anyway. Fixing a typo in comments. --- src/mlpack/core/math/quantile.hpp | 2 +- src/mlpack/tests/main_tests/emst_test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/math/quantile.hpp b/src/mlpack/core/math/quantile.hpp index d1178dd213..fc469257ab 100644 --- a/src/mlpack/core/math/quantile.hpp +++ b/src/mlpack/core/math/quantile.hpp @@ -21,7 +21,7 @@ namespace math /** Miscellaneous math routines. */ { * Computes the inverse erf function using the rational approximation from * Numerical Recipes. * Code accompanying the article "Approximating the erfinv function" in - * GPU Computing Gems, Volume 2 + * GPU Computing Gems, Volume 2. * * @param x Input value. */ diff --git a/src/mlpack/tests/main_tests/emst_test.cpp b/src/mlpack/tests/main_tests/emst_test.cpp index 1a897fa902..afc6269210 100644 --- a/src/mlpack/tests/main_tests/emst_test.cpp +++ b/src/mlpack/tests/main_tests/emst_test.cpp @@ -104,10 +104,10 @@ TEST_CASE_METHOD(EMSTTestFixture, "EMSTFirstTwoOutputRowsIntegerTest", for (size_t i = 0; i < params.Get("output").n_cols; ++i) { REQUIRE(params.Get("output")(0, i) == - Approx(static_cast(std::round(params.Get("output")(0, i)))). + Approx(std::round(params.Get("output")(0, i))). epsilon(1e-7)); REQUIRE(params.Get("output")(1, i) == - Approx(static_cast(std::round(params.Get("output")(1, i)))). + Approx(std::round(params.Get("output")(1, i))). epsilon(1e-7)); } }