/*
  File name: lind.c
  Created by: Ljubomir Buturovic
  Created: 12/05/2002
  Purpose: linear discriminant learning and prediction.
*/

/*
  Copyright 2004 Ljubomir J. Buturovic

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

static char rcsid[] = "$Id: lind.c,v 1.28 2004/10/21 01:53:44 ljubomir Exp $";

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "lind.h"
#include "lau.h"
#include "lerr.h"
#include "lmat.h"
#include "pau.h"
#include "bagging.h"

/*
  Debugging function: multiply 'a' and 'b' and check the product is
  identity matrix.
*/
static void inverse_check(float **a, float **b, int d, FILE *fdbg)
{
  int   i;
  int   j;
  char  *cTmp;
  float **product;

  cTmp = rcsid; /* kill compiler warning */
  /*
    The code is disabled for now because it produces enormous debug
    file in cross-validation. Enable as needed.
   */
  if (a && b && fdbg && 0)
    {
      product = fmx_mult(a, d, d, b, d, 0);
      if (product)
	{
	  fprintf(fdbg, "inverse_check():\n");
	  for (i = 0; i < d; i++)
	    {
	      for (j = 0; j < d; j++)
		fprintf(fdbg, "%12.5g\t", product[i][j]);
	      fprintf(fdbg, "\n");
	    }
	  mx_free((void **) product, d);
	}
    }
}

/*
  Multiply transpose of input data matrix 'x' with itself. The result
  is a d by d matrix.  This is the first term of the pseudo-inverse
  matrix for a linear discriminant classifer.
*/
static float **t_mult(float **x, int nv, int d)
{
  int    i;
  int    j;
  int    k;
  double s;
  float  **mx;
  
  mx = fmx_alloc(d, d);
  if (mx)
    {
      for (i = 0; i < d; i++)
	for (j = 0; j < d; j++)
	  {
	    s = 0.0;
	    for (k = 0; k < nv; k++)
	      s += x[k][i]*x[k][j];
	    mx[i][j] = s;
	  }
    }
  return mx;
}

/*
  Calculate linear discriminant classifier using pseudo-inverse
  solution. The returned value is c by d+1 matrix (number of classes
  by number of features plus 1). The last column contains biases.

  The computations follow Christopher M. Bishop, Neural Networks for
  Pattern Recognition, Section 3.4.3, Pseudo-inverse solution. Oxford
  University Press, Oxford, 1995.

  In case of error, return NULL and set 'errc'. The errors are EINVAL
  if 'dset' is NULL, memory allocation errors, and singular matrix
  (LERR_SINGULAR).
*/
float **lind_learn(struct dataset *dset, int *errc, FILE *fdbg)
{
  int   i;
  int   k;
  int   d;
  int   iclass;
  int   icum;
  int   status;
  float tk;
  float *xmean;
  float **lin = (float **) 0;
  float **mult;
  float **inverse;
  float **pseudo_inverse;
  float **target;

  if (dset)
    {
      d = dset->d;
      target = fmx_alloc(dset->nv, dset->c);
      if (target)
	{
	  fmx_set(target, dset->nv, dset->c, 0.0);
	  iclass = 0;
	  icum = dset->nd[0];
	  for (i = 0; i < dset->nv; i++)
	    {
	      target[i][iclass] = 1.0;
	      if ((i == icum-1) && (iclass < dset->c-1))
		{
		  iclass++;
		  icum += dset->nd[iclass];
		}
	    }
	  mult = t_mult(dset->x, dset->nv, d);
	  if (mult)
	    {
	      inverse = fmx_inv(mult, d, (float *) 0, (float *) 0, errc);
	      if (inverse)
		{
		  if (fdbg)
		    inverse_check(mult, inverse, d, fdbg);
		  pseudo_inverse = fmx_mult(inverse, d, d, dset->x, dset->nv, 1);
		  if (pseudo_inverse)
		    {
		      lin = fmx_alloc(dset->c, d+1);
		      if (lin)
			{
			  status = fmx_tmulta(lin, pseudo_inverse, d, dset->nv, target, 
					      dset->c, 0);
			  if (!status)
			    {
			      /*
				Compute bias terms now using equations
				(3.48) and (3.49) from Bishop.
			      */
			      xmean = fmx_mean(dset->x, dset->nv, dset->d);
			      if (xmean)
				{
				  for (k = 0; k < dset->c; k++)
				    {
				      tk = fmx_col_mean(target, dset->nv, k);
				      lin[k][d] = tk-fvec_dot(lin[k], xmean, d, (int *) 0);
				    }
				  vx_free(xmean);
				}
			    }
			  else
			    *errc = LERR_INTERNAL;
			}
		      mx_free((void **) pseudo_inverse, d);
		    }
		  mx_free((void **) inverse, d);
		}
	      mx_free((void **) mult, d);
	    }
	  mx_free((void **) target, dset->nv);
	}
    }
  else if (errc)
    *errc = EINVAL;
  return lin;
}



syntax highlighted by Code2HTML, v. 0.9.1