
/*
 * Copyright (c) 2001 BBNT Solutions LLC
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice and this permission
 * appear in all copies and in supporting documentation, and that the
 * name of BBN Technologies not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  BBN makes no representations about the
 * suitability of this software for any purposes.  It is provided "AS
 * IS" without express or implied warranties.
 */

#include <stdio.h>
#include <stdlib.h>
#include "anova.h"

double data [MAX_SETS] [MAX_ELEMENTS];
double sum [MAX_SETS], avg [MAX_SETS], col_effect [MAX_SETS];
double grand_sum = 0, grand_mean;
double ssy = 0, ss0 = 0, ssa = 0, sse = 0, sst = 0;
double msa, mse, f_computed;
unsigned long data_items [MAX_SETS];
unsigned long grand_items = 0;
unsigned short sets = 0;


void usage (progname)
char *progname;
{
    fprintf (stderr,"usage: %s dataset1 dataset2 ... datasetN\n", progname);
    exit (1);
}


void read_set (filename,set)
char *filename;
{
    char buffer [BUFFER_SIZE];
    FILE *fp;

    fprintf (stdout,"slurping %s...\n", filename);
    if ((fp = fopen (filename,"r")) == NULL)
    {
	fprintf (stderr,"can't open dataset: %s\n", filename);
	exit (1);
    }
    data_items [sets] = 0;
    while (!feof (fp))
    {
	if (fgets (buffer,BUFFER_SIZE,fp) != NULL)
	{
	    data [sets] [data_items [sets]] = atof (buffer);
	    data_items [sets]++;
	}
    }
    fclose (fp);
}


void crunch_data_matrix ()
{
    double err, fac_contrib;
    int i, j;

    for (i = 0; i < sets; i++)
    {
	sum [i] = 0;
	for (j = 0; j < data_items [i]; j++)
	{
	    sum [i] += data [i] [j];
	    ssy += (data [i] [j] * data [i] [j]);
	    grand_sum += data [i] [j];
	    grand_items++;
	}
	avg [i] = sum [i] / data_items [i];
    }
    grand_mean = grand_sum / grand_items;
    ss0 = grand_items * (grand_mean * grand_mean);
    sst = ssy - ss0;
    for (i = 0; i < sets; i++)
    {
	col_effect [i] = avg [i] - grand_mean;
	ssa += (data_items [i] * (col_effect [i] * col_effect [i]));
    }
    for (i = 0; i < sets; i++)
	for (j = 0; j < data_items [i]; j++)
	{
	    err = data [i] [j] - grand_mean - col_effect [i];
	    sse += (err * err);
	}
    fprintf (stdout,"grand_sum = %f\n", grand_sum);
    fprintf (stdout,"grand_items = %ld\n", grand_items);
    fprintf (stdout,"grand_mean = %f\n", grand_mean);
    fprintf (stdout,"ssy = %f\nss0 = %f\nsst = %f\nssa = %f\nsse = %f\n",
	     ssy, ss0, sst, ssa, sse);
    fac_contrib = 100.0 * (ssa / sst);
    fprintf (stdout,"factor contribution: %f\n", fac_contrib);
    fprintf (stdout,"error contribution:  %f\n", 100.0 - fac_contrib);
    msa = ssa / (double)(sets - 1);
    mse = sse / (double)(grand_items - sets);
    fprintf (stdout,"msa = %f\n", msa);
    fprintf (stdout,"mse = %f\n", mse);
    f_computed = msa / mse;
    fprintf (stdout,"f computed = %f\n", f_computed);
}


int main (argc,argv)
int argc;
char *argv [];
{
    int i;

    for (i = 1; (i < argc) && (sets < MAX_SETS); i++, sets++)
	read_set (argv [i]);
    if (!sets)
	usage (argv [0]);
    crunch_data_matrix ();
    exit (0);
}
