discreteDST.cc

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00032 #include "fastlib/fastlib.h"
00033 #include "discreteDST.h"
00034 #include "support.h"
00035 
00036 void DiscreteDST::Init(int N) {
00037   p.Init(N);
00038   ACC_p.Init(N);
00039   double s = 1;
00040   for (int i = 0; i < N-1; i++) {
00041     p[i] = RAND_UNIFORM(s*0.2,s*0.8);
00042     s -= p[i];
00043   }
00044   p[N-1] = s;
00045 }
00046 
00047 void DiscreteDST::generate(int* v) {
00048   int N = p.length();
00049   double r = RAND_UNIFORM_01;
00050   double s = 0;
00051   for (int i = 0; i < N; i++) {
00052     s += p[i];
00053     if (s >= r) {
00054       *v = i;
00055       return;
00056     }
00057   }
00058   *v = N-1;
00059 }
00060 
00061 void DiscreteDST::end_accumulate() {
00062   int N = p.length();
00063   double s = 0;
00064   for (int i = 0; i < N; i++) s += ACC_p[i];
00065   if (s == 0) s = -INFINITY;
00066   for (int i = 0; i < N; i++) p[i] = ACC_p[i]/s;
00067 }
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3