Added statistisc tests to the test class from main.

This commit is contained in:
Relintai 2023-01-25 02:22:04 +01:00
parent 889515be41
commit 0ebe6f13e3
4 changed files with 163 additions and 12 deletions

View File

@ -8,7 +8,7 @@ def get_doc_classes():
return [
"MLPPTest",
"MLPPTests",
]
def get_doc_path():

View File

@ -27,7 +27,7 @@ SOFTWARE.
void register_pmlpp_types(ModuleRegistrationLevel p_level) {
if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) {
ClassDB::register_class<MLPPTest>();
ClassDB::register_class<MLPPTests>();
}
}

View File

@ -1,12 +1,154 @@
#include "mlpp_tests.h"
MLPPTest::MLPPTest() {
#include "core/math/math_funcs.h"
//TODO remove
#include <cmath>
#include <ctime>
#include <iostream>
#include <vector>
#include "../mlpp/activation/activation.h"
#include "../mlpp/ann/ann.h"
#include "../mlpp/auto_encoder/auto_encoder.h"
#include "../mlpp/bernoulli_nb/bernoulli_nb.h"
#include "../mlpp/c_log_log_reg/c_log_log_reg.h"
#include "../mlpp/convolutions/convolutions.h"
#include "../mlpp/cost/cost.h"
#include "../mlpp/data/data.h"
#include "../mlpp/dual_svc/dual_svc.h"
#include "../mlpp/exp_reg/exp_reg.h"
#include "../mlpp/gan/gan.h"
#include "../mlpp/gaussian_nb/gaussian_nb.h"
#include "../mlpp/kmeans/kmeans.h"
#include "../mlpp/knn/knn.h"
#include "../mlpp/lin_alg/lin_alg.h"
#include "../mlpp/lin_reg/lin_reg.h"
#include "../mlpp/log_reg/log_reg.h"
#include "../mlpp/mann/mann.h"
#include "../mlpp/mlp/mlp.h"
#include "../mlpp/multinomial_nb/multinomial_nb.h"
#include "../mlpp/numerical_analysis/numerical_analysis.h"
#include "../mlpp/outlier_finder/outlier_finder.h"
#include "../mlpp/pca/pca.h"
#include "../mlpp/probit_reg/probit_reg.h"
#include "../mlpp/softmax_net/softmax_net.h"
#include "../mlpp/softmax_reg/softmax_reg.h"
#include "../mlpp/stat/stat.h"
#include "../mlpp/svc/svc.h"
#include "../mlpp/tanh_reg/tanh_reg.h"
#include "../mlpp/transforms/transforms.h"
#include "../mlpp/uni_lin_reg/uni_lin_reg.h"
#include "../mlpp/wgan/wgan.h"
Vector<double> dstd_vec_to_vec(const std::vector<double> &in) {
Vector<double> r;
r.resize(static_cast<int>(in.size()));
double *darr = r.ptrw();
for (uint32_t i = 0; i < in.size(); ++i) {
darr[i] = in[i];
}
return r;
}
MLPPTest::~MLPPTest() {
void MLPPTests::test_statistics() {
ERR_PRINT("MLPPTests::test_statistics() Started!");
MLPPStat stat;
MLPPConvolutions conv;
// STATISTICS
std::vector<double> x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<double> y = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
std::vector<double> w = { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
is_approx_equalsd(stat.mean(x), 5.5, "Arithmetic Mean");
is_approx_equalsd(stat.mean(x), 5.5, "Median");
is_approx_equals_dvec(dstd_vec_to_vec(x), dstd_vec_to_vec(stat.mode(x)), "stat.mode(x)");
is_approx_equalsd(stat.range(x), 9, "Range");
is_approx_equalsd(stat.midrange(x), 4.5, "Midrange");
is_approx_equalsd(stat.absAvgDeviation(x), 2.5, "Absolute Average Deviation");
is_approx_equalsd(stat.standardDeviation(x), 3.02765, "Standard Deviation");
is_approx_equalsd(stat.variance(x), 9.16667, "Variance");
is_approx_equalsd(stat.covariance(x, y), -9.16667, "Covariance");
is_approx_equalsd(stat.correlation(x, y), -1, "Correlation");
is_approx_equalsd(stat.R2(x, y), 1, "R^2");
// Returns 1 - (1/k^2)
is_approx_equalsd(stat.chebyshevIneq(2), 0.75, "Chebyshev Inequality");
is_approx_equalsd(stat.weightedMean(x, w), 5.5, "Weighted Mean");
is_approx_equalsd(stat.geometricMean(x), 4.52873, "Geometric Mean");
is_approx_equalsd(stat.harmonicMean(x), 3.41417, "Harmonic Mean");
is_approx_equalsd(stat.RMS(x), 6.20484, "Root Mean Square (Quadratic mean)");
is_approx_equalsd(stat.powerMean(x, 5), 7.39281, "Power Mean (p = 5)");
is_approx_equalsd(stat.lehmerMean(x, 5), 8.71689, "Lehmer Mean (p = 5)");
is_approx_equalsd(stat.weightedLehmerMean(x, w, 5), 8.71689, "Weighted Lehmer Mean (p = 5)");
is_approx_equalsd(stat.contraHarmonicMean(x), 7, "Contraharmonic Mean");
is_approx_equalsd(stat.heronianMean(1, 10), 4.72076, "Hernonian Mean");
is_approx_equalsd(stat.heinzMean(1, 10, 1), 5.5, "Heinz Mean (x = 1)");
is_approx_equalsd(stat.neumanSandorMean(1, 10), 3.36061, "Neuman-Sandor Mean");
is_approx_equalsd(stat.stolarskyMean(1, 10, 5), 6.86587, "Stolarsky Mean (p = 5)");
is_approx_equalsd(stat.identricMean(1, 10), 4.75135, "Identric Mean");
is_approx_equalsd(stat.logMean(1, 10), 3.90865, "Logarithmic Mean");
is_approx_equalsd(stat.absAvgDeviation(x), 2.5, "Absolute Average Deviation");
ERR_PRINT("MLPPTests::test_statistics() Finished!");
}
void MLPPTest::_bind_methods() {
//ClassDB::bind_method(D_METHOD(""), &MLPPTest::);
void MLPPTests::is_approx_equalsd(double a, double b, const String &str) {
if (!Math::is_equal_approx(a, b)) {
ERR_PRINT("TEST FAILED: " + str + " Got: " + String::num(a) + " Should be: " + String::num(b));
}
}
void MLPPTests::is_approx_equals_dvec(const Vector<double> &a, const Vector<double> &b, const String &str) {
if (a.size() != b.size()) {
goto IAEDVEC_FAILED;
}
for (int i = 0; i < a.size(); ++i) {
if (!Math::is_equal_approx(a[i], b[i])) {
goto IAEDVEC_FAILED;
}
}
return;
IAEDVEC_FAILED:
String fail_str = "TEST FAILED: ";
fail_str += str;
fail_str += " Got: [ ";
for (int i = 0; i < a.size(); ++i) {
fail_str += String::num(a[i]);
fail_str += " ";
}
fail_str += "] Should be: [ ";
for (int i = 0; i < b.size(); ++i) {
fail_str += String::num(b[i]);
fail_str += " ";
}
fail_str += "].";
ERR_PRINT(fail_str);
}
MLPPTests::MLPPTests() {
}
MLPPTests::~MLPPTests() {
}
void MLPPTests::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_statistics"), &MLPPTests::test_statistics);
}

View File

@ -1,17 +1,26 @@
#ifndef MLPP_TEST_H
#define MLPP_TEST_H
#ifndef MLPP_TESTS_H
#define MLPP_TESTS_H
// TODO port this class to use the test module once it's working
// Also don't forget to remove it's bindings
#include "core/containers/vector.h"
#include "core/object/reference.h"
class MLPPTest : public Reference {
GDCLASS(MLPPTest, Reference);
#include "core/string/ustring.h"
class MLPPTests : public Reference {
GDCLASS(MLPPTests, Reference);
public:
MLPPTest();
~MLPPTest();
void test_statistics();
void is_approx_equalsd(double a, double b, const String &str);
void is_approx_equals_dvec(const Vector<double> &a, const Vector<double> &b, const String &str);
MLPPTests();
~MLPPTests();
protected:
static void _bind_methods();