Μια μικρή συλλογή στατιστικών συναρτήσεων στη C++

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

#define NMAX 100000

using namespace std;

double sum      (double *x, unsigned int n);
double sum2     (double *x, unsigned int n);
double average  (double *x, unsigned int n);
double variance (double *x, unsigned int n);
double stdev    (double *x, unsigned int n);
double sumxy    (double *x, double *y, unsigned int n);
double covar    (double *x, double *y, unsigned int n);
double correl   (double *x, double *y, unsigned int n);
double pearson  (double *x, double *y, unsigned int n);

int main()
{
    
    double x[NMAX], y[NMAX];
    double z;
    unsigned int i=0;
    unsigned int n;
    
    cout << "x,y : ";
    while (cin>>z)
    {
        x[i] = z;
        cin >> z;
        y[i] = z;
        ++i;
        cout << "x,y : ";
    }
    n=i;

    cout << endl;
    cout << "Results : " << endl;
    
    cout << "ave x   : " << average(x,n) << endl;    
    cout << "var x   : " << variance(x,n) << endl;    
    cout << "std x   : " << stdev(x,n) << endl; 
    
    cout << "ave y   : " << average(y,n) << endl; 
    cout << "var y   : " << variance(y,n) << endl; 
    cout << "std y   : " << stdev(y,n) << endl;
     
    cout << "covar   : " << covar(x,y,n) << endl; 
    cout << "correl  : " << correl(x,y,n) << endl; 
    cout << "pearson : " << pearson(x,y,n) << endl; 
        
    return 0;
}

double sum(double *x, unsigned int n)
{
    double sum = 0.0;   
    for (int i=0; i < n; i++)
    {
        sum += x[i];
    }
    return sum; 
}

double sum2(double *x, unsigned int n)
{
    double sum2 = 0.0;   
    for (int i=0; i < n; i++)
    {
        sum2 += pow(x[i], 2.0);
    }
    return sum2; 
}

double average(double *x, unsigned int n)
{
    double ave = sum(x,n) / n;   
    return ave; 
}

double variance(double *x, unsigned int n)
{
    double s  = sum(x, n);
    double s2 = sum2(x, n);   
    double avex = average(x,n);
    double var = 0.0;
    for (int i=0; i < n; i++)
    {
        var += pow( (x[i] - avex), 2.0 );
    }
    var /= (n-1);    
    return var; 
}

double stdev(double *x, unsigned int n)
{
    double var = variance(x,n);
    double std = sqrt(var);
    return std; 
}


double sumxy(double *x, double *y, unsigned int n)
{
    double sumxy = 0.0;   
    for (int i=0; i < n; i++)
    {
        sumxy += x[i] * y[i];
    }
    return sumxy; 
}

double covar(double *x, double *y, unsigned int n)
{
    double avex = average(x,n);
    double avey = average(y,n);
    double covar = 0.0;
    for (int i=0; i < n; i++)
    {
        covar += (x[i] - avex) * (y[i] - avey);
    }
    covar /= n;
    return covar;   
}


double correl(double *x, double *y, unsigned int n)
{
    double cov = covar (x,y,n);
    double stdx = stdev(x,n);
    double stdy = stdev(y,n);
    double correl = cov / (stdx*stdy);
    return correl;
}

double pearson(double *x, double *y, unsigned int n)
{
    double sx = sum (x,n);
    double sy = sum (y,n);
    double s2x = sum2 (x,n);
    double s2y = sum2 (y,n);
    double sxy = sumxy (x,y,n);   
    double pearson = (n*sxy - sx*sy) / 
        sqrt( (n*s2x -pow(sx,2.0) ) * (n*s2y - pow(sy,2.0)) );
    return pearson;
}

Συνδεθείτε για περισσότερες δυνατότητες αλληλεπίδρασης,
σχολιασμοί, εξωτερικοί σύνδεσμοι, βοήθεια, ψηφοφορίες, αρχεία, κτλ.

Creative Commons License
Εκπαιδευτικό υλικό από τον Αθανάσιο Σταυρακούδη σας παρέχετε κάτω από την άδεια Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.
Σας παρακαλώ να ενημερωθείτε για κάποιους επιπλέον περιορισμούς
http://stavrakoudis.econ.uoi.gr/stavrakoudis/?iid=401.