/*
 *              Copyright (C) 2021 by Robert E. Cousins
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions a
 * are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its 
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *	$Id: sls_test.c,v 1.5 2021/08/15 16:03:33 rec Exp $
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sls.h"

#define MAX_POINTS (1000)

double x[MAX_POINTS], y[MAX_POINTS];
int num_points;

void
test_common(char *msg)
{
  printf("\n\tTest: %s\n", msg);

  SLS s;
  sls_init(&s);
  for (int i = 0; i < num_points; i++)
    sls_sample(&s,x[i],y[i]);
  double b0,b1,r2;
  sls_model(&s,&b0,&b1,&r2);
  sls_dump(&s,stdout);
  printf("\n\t        Model: B0 = %g B1 = %g R^2 = %g\n", b0,b1,r2);
  printf("\t     Equation: y = %gX + %g\n", b1,b0);
  printf("\t    Mean of X: %g\n", sls_x_mean(&s));
  printf("\tVariance of X: %g\n", sls_x_variance(&s));
  printf("\t Std Dev of X: %g\n", sls_x_std_dev(&s));
  printf("\t    Mean of Y: %g\n", sls_y_mean(&s));
  printf("\tVariance of Y: %g\n", sls_y_variance(&s));
  printf("\t Std Dev of Y: %g\n", sls_y_std_dev(&s));  
  printf("\n");
  printf("+----+--------------+--------------+--------------+--------------+\n");
  printf("| ## |       X      |       Y      |     Y-hat    |     Error    |\n");
  printf("+----+--------------+--------------+--------------+--------------+\n");
  for (int i = 0; i < 10; i++) {
    double t = x[i] * b1 + b0;
    printf("| %2d | %12g | %12g | %12g | %12g |\n",
	   i+1,x[i],y[i],t,fabs(y[i]-t));
  }
  printf("+----+--------------+--------------+--------------+--------------+\n");
}

void
test0()
{
  num_points = MAX_POINTS;
  for (int i = 0; i < num_points; i++) {
    x[i] = i+1;
    y[i] = 2.0;
  }
  test_common("y = 2");
}

void
test1()
{
  num_points = MAX_POINTS;
  for (int i = 0; i < num_points; i++) {
    x[i] = i;
    y[i] = i+4;
  }
  test_common("y = x + 4");
}

void
test2()
{
  num_points = MAX_POINTS;
  for (int i = 0; i < num_points; i++) {
    x[i] = i;
    y[i] = 1000.0 + (double)arc4random() / (double) (1ll << 32);
  }
  test_common("y = random() + 1000");
}

void
test3()
{
  static double xi[] = {
    1.47, 1.50,	1.52, 1.55, 1.57, 
    1.60, 1.63, 1.65, 1.68, 1.70, 
    1.73, 1.75, 1.78, 1.80, 1.83
  };
  static double yi[] = {
    52.21, 53.12, 54.48, 55.84, 57.20,
    58.57, 59.93, 61.29, 63.11, 64.47,
    66.28, 68.10, 69.92, 72.19, 74.46
  };
  num_points = sizeof(xi)/sizeof(xi[0]);
  for (int i = 0; i < num_points; i++) {
    x[i] = xi[i];
    y[i] = yi[i];
  }
  test_common("Numeric example");
}
int
main(int argc, char *argv[])
{
  printf("Simple Least Squares Library test program\n");
  printf("Version: $Id: sls_test.c,v 1.5 2021/08/15 16:03:33 rec Exp $\n");
  printf("Copyright (C) 2021 by Robert Cousins\n");
  printf("All Rights Reserved\n");
  test0();
  test1();
  test2();
  test3();
  return 0;
}
