/* program to calculate the sum of errors squared */

/* y-hat models are: */
char* Labels[] = {"x",
                  "min(1,max(-1,x))",
                  "(2/pi)*atan(1.80*x + 4.55*x^3)",
                  "sign(x)*exp(|x|)",
                  "sign(x)",
                  "0.435*sign(x)*exp(0.694*|x|)"};

/* where x=JWB(a)-JWB(b)  and  x=(Wilson(a)-Wilson(b))/100  */

#include <stdio.h>
#include <string.h>
#include <math.h>

#define NUMYHAT 6

double SumErrSq[2][NUMYHAT], SumSq = 0;

double abs(x) double x; { return x >= 0.0 ? x : -x; }
double sign(x) double x; { return x > 0.0 ? 1.0 : x == 0.0 ? 0.0 : -1.0; }
double max(x,y) double x,y; { return x > y ? x : y; }
double min(x,y) double x,y; { return x < y ? x : y; }

main() {
    FILE *fpIn, *fpOut1, *fpOut2;
    char String[256];
    char *Date, *Team, *Opponent, *pCh;
    char Ch;
    int  Score1, Score2, Margin, Result, Wilson, Marsee;
    double BOMB, JWB, Lightner;
    double x, y, yHat, TwoDivPi;
    int Ix,Which;

    for(Ix=0; Ix<NUMYHAT; Ix++) {
        SumErrSq[0][Ix] = 0;
        SumErrSq[1][Ix] = 0;
    }
    TwoDivPi = 0.5/atan(1.0);
    fpIn = fopen("compare.txt","r");
    if(!fpIn) {printf("Can't open compare.txt\n"); exit(1);}
    fpOut1 = fopen("compare2.txt","w");
    if(!fpOut1) {printf("Can't open compare2.txt\n"); exit(1);}
    fpOut2 = fopen("JWB.dat","w");
    if(!fpOut2) {printf("Can't open JWB.dat\n"); exit(1);}

    fgets(String,sizeof String,fpIn);
    fputs(String,fpOut1);
    fgets(String,sizeof String,fpIn);
    fputs("date\tTeam\tScore\tOpponent\tScore\tMargin\tResult\tBOMB\tJWB\t\
Wilson\tMarsee\tLightner\n",fpOut1);
printf("Hi\n");
    fgets(String,sizeof String,fpIn);
    while(!feof(fpIn)) {
        Date = pCh = String;
        while((Ch = *pCh++) && (Ch != '\t'));
        if(!Ch) exit(1);
        *(pCh-1) = 0;
        Team = pCh;
        while((Ch = *pCh++) && (Ch != '\t'));
        if(!Ch) exit(1);
        *(pCh-1) = 0;
        Score1 = atoi(pCh);
        while((Ch = *pCh++) && (Ch != '\t'));
        if(!Ch) exit(1);
        Opponent = pCh;
        while((Ch = *pCh++) && (Ch != '\t'));
        if(!Ch) exit(1);
        *(pCh-1) = 0;
        if(sscanf(pCh,"%d\t%d\t%d\t%lf\t%lf\t%d\t%d\t%lf\n",
         &Score2,&Margin,&Result,&BOMB,&JWB,&Wilson,&Marsee,&Lightner) != 8)
          exit(1);
        fprintf(fpOut1,"%s\t%s\t%d\t%s\t%d\t%d\t%d\t%.6f\t%.6f\t%d\t%d\t%.2f\n",
         Date,Team,Score1,Opponent,Score2,Margin,Result,BOMB,
         JWB,Wilson,Marsee,Lightner);
        fprintf(fpOut1,"%s\t%s\t%d\t%s\t%d\t%d\t%d\t%.6f\t%.6f\t%d\t%d\t%.2f\n",
         Date,Opponent,Score2,Team,Score1,-Margin,-Result,-BOMB,
         -JWB,-Wilson,-Marsee,-Lightner);
        fgets(String,sizeof String,fpIn);
        fprintf(fpOut2,"%11.6f%3d\n",JWB,Result);
        x = JWB;
        y = Result;
        for(Which=0; Which<=1; Which++) {
            for(Ix=0; Ix<NUMYHAT; Ix++) {
                switch(Ix) {
                  case 0: yHat = x; break;
                  case 1: yHat = min(1.0,max(-1.0,x)); break;
                  case 2: yHat = TwoDivPi*atan((1.80+4.55*x*x)*x); break;
                  case 3: yHat = sign(x)*exp(abs(x)); break;
                  case 4: yHat = sign(x); break;
                  case 5: yHat = 0.435*sign(x)*exp(0.694*abs(x)); break;
                }
                SumErrSq[Which][Ix] += (y-yHat)*(y-yHat);
            }
            x = Wilson*0.01;
        }
        SumSq += y*y;
    }
    for(Ix=0; Ix<NUMYHAT; Ix++) {
        printf("%30.30s%7.0f %7.0f  %7.0f %7.0f\n",Labels[Ix],
         SumErrSq[0][Ix],SumErrSq[1][Ix],
         100.0*(1.0-SumErrSq[0][Ix]/SumSq),100.0*(1.0-SumErrSq[1][Ix]/SumSq));
    }
}     
