/*
 *                            COPYRIGHT
 *
 *  att1class2.c - single attribute data generator
 *  Copyright (C) 2012 Exstrom Laboratories LLC
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  A copy of the GNU General Public License is available on the internet at:
 *  http://www.gnu.org/copyleft/gpl.html
 *
 *  or you can write to:
 *
 *  The Free Software Foundation, Inc.
 *  675 Mass Ave
 *  Cambridge, MA 02139, USA
 *
 *  Exstrom Laboratories LLC contact:
 *  stefan(AT)exstrom.com
 *
 *  Exstrom Laboratories LLC
 *  Longmont, CO 80503, USA
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

/* Compile: gcc -lm -lgsl -lgslcblas -o att1class2 att1class2.c */

/*
This program generates single attribute data that can be used to test
a binary classifier. The attribute values for the two classes are
drawn from two Normal distributions. The mean and standard deviation
for the two distributions are specified on the command line along with
the probability of class 1. The output lines have the class followed by
the attribute value. If C(i) is the ith class and A(i) the ith attribute
value then the output will look like:

C(1) A(1)
C(2) A(2)
.
.
.
C(nr) A(nr)
*/

int main( int argc, char *argv[] )
{
  if(argc < 8){
    printf("usage: att1class2 nr seed p m1 s1 m2 s2\n");
    printf("  nr = number of records to generate.\n");
    printf("  seed = random number seed.\n");
    printf("  p = probability of class 1\n");
    printf("  m1 = mean of attribute conditioned on class 1.\n");
    printf("  s1 = standard deviation of attribute conditioned on class 1.\n");
    printf("  m0 = mean of attribute conditioned on class 0.\n");
    printf("  s0 = standard deviation of attribute conditioned on class 0.\n");
    printf("  Example: ./att1class2 2000 17 0.54 63.8 4.18 69.4 4.69\n");
    return(-1);}

  int i, j;
  int nr = atoi(argv[1]);
  unsigned long int seed = atoi(argv[2]);
  double p = atof(argv[3]);
  double m1 = atof(argv[4]);
  double s1 = atof(argv[5]);
  double m0 = atof(argv[6]);
  double s0 = atof(argv[7]);

  gsl_rng *rng = gsl_rng_alloc(gsl_rng_mt19937);
  gsl_rng_set(rng, seed);

  unsigned int class;
  double x;

  for(i = 0; i < nr; ++i){
    class = gsl_ran_bernoulli(rng, p); // P(0)=1-p, P(1)=p
    if(class == 1)
      x = m1 + gsl_ran_gaussian(rng, s1);
    else
      x = m0 + gsl_ran_gaussian(rng, s0);
    printf("%d %lf\n", class, x);}

  gsl_rng_free(rng);
  return(0);
}
