OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-cogutil / tests / util / randomUTest.cxxtest

81 lines · cxxtest ref:
/** randomUTest.cxxtest ---
 *
 * Copyright (C) 2011 OpenCog Foundation
 *
 * Author: Nil Geisweiller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <opencog/util/random.h>
#include <opencog/util/mt19937ar.h>
#include <cmath>

using namespace std;
using namespace opencog;

class randomUTest : public CxxTest::TestSuite
{
public:
    // test that sampling a gaussian of unsigned doesn't raise an
    // exception when the sample would be negative
    void test_unsigned_gaussian_rand() {
        const unsigned m = 0, s = 10, size = 100;
        MT19937RandGen rng(1);
        for (unsigned i = 0; i < size; ++i)
            TS_ASSERT_THROWS_NOTHING(gaussian_rand(m, s, rng));
    }

    void test_gaussian_rand_convergence() {
        const double m = 1, s = 10, delta = 0.05;
        size_t size = 10000000;
        MT19937RandGen rng(1);

        // Calculate mean and variance manually
        double sum = 0.0;
        double sum_sq = 0.0;

        for (size_t i = 0; i < size; ++i) {
            double sample = gaussian_rand(m, s, rng);
            sum += sample;
            sum_sq += sample * sample;
        }

        double calculated_mean = sum / size;
        double variance = (sum_sq / size) - (calculated_mean * calculated_mean);
        double calculated_stddev = sqrt(variance);

        // check mean
        TS_ASSERT_DELTA(calculated_mean, m, delta);
        // check standard deviation
        TS_ASSERT_DELTA(calculated_stddev, s, delta);
    }

    void test_discrete() {
        std::vector<double> weights = { 1, 3, 4, 2 };
        size_t size = 10000000;
        int p[4] = {};

        MT19937RandGen rng(1);
        for (size_t i = 0; i < size; ++i)
            ++p[rng.rand_discrete(weights)];

        TS_ASSERT_LESS_THAN(p[1], p[2]);
        TS_ASSERT_LESS_THAN(p[3], p[1]);
        TS_ASSERT_LESS_THAN(p[0], p[3]);
    }
};