2003년 11월 17일 월요일

표준편차(standard deviation)

#include <iostream>
#include <math.h>

using namespace std;

double get_div(const int n, double a[])
{
    double sum = 0;
    if (n > 0)
    {
        for (int i = 0; i < n ; i++)
        {
            sum += a[i];
        }
        return sum / n;
    }

    return 0;
}


double standard_deviation1(const int n, double a[])
{
    double sum = 0;
    double div = 0;
    double distribution_n = 0;
    double distribution = 0;

    if (n > 0)
    {
        div = get_div(n, a);

        for (int i = 0; i < n ; i++)
        {
            distribution_n = distribution_n + pow((a[i] - div), 2);
        }
        distribution = distribution_n / n;

        return sqrt(distribution);
    }

    return 0;
}


double standard_deviation2(const int n, const double m, const double d, const double new_val)
{
    double expect_square_val = 0;
    double expect_val = 0;

    if (n > 0)
    {
        expect_square_val = (n * (pow(d, 2) + pow(m, 2)) + pow(new_val, 2)) / (n + 1);
        expect_val = (n * m + new_val) / (n + 1);

        return sqrt(expect_square_val - pow(expect_val, 2));
    }

    return 0;
}

double standard_deviation_sum(const int n_a, const double m_a, const double d_a, const int n_b, const double m_b, const double d_b)
{
    double expect_square_val = 0;
    double expect_val = 0;

    if (n_a + n_b > 0)
    {
        expect_square_val = (n_a * (pow(d_a, 2) + pow(m_a, 2)) + n_b * (pow(d_b, 2) + pow(m_b, 2))) / (n_a + n_b);
        expect_val = (n_a * m_a + n_b * m_b) / (n_a + n_b);

        return sqrt(expect_square_val - pow(expect_val, 2));
    }

    return 0;
}

댓글 없음:

댓글 쓰기