PLearn 0.1
random.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal, all rights reserved
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: random.h 8210 2007-10-24 19:12:31Z nouiz $
00038  ******************************************************* */
00039 
00040 #ifndef RANDOM_H
00041 #define RANDOM_H
00042 
00043 #include "TMat.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 /*
00049 
00050 Special functions.
00051 -----------------
00052 */
00053 
00055 real  log_gamma(real x);
00056 
00058 real log_beta(real x, real y);
00059 
00062 real incomplete_beta(real z, real x, real y);
00064 //double incbet(double x, double y, double z);
00065 
00067 real student_t_cdf(real t, int nb_degrees_of_freedom);
00068 
00075 void  seed();
00077 void  manual_seed(int32_t x);
00079 int32_t  get_seed();
00080 
00090 real  uniform_sample();
00092 real  bounded_uniform(real a,real b);
00093 
00095 real  expdev();
00097 real  gaussian_01();
00098 inline real normal_sample() { return gaussian_01(); }
00099 
00101 real  gaussian_mu_sigma(real mu, real sigma);
00102 
00106 real  gaussian_mixture_mu_sigma(Vec& w, const Vec& mu, const Vec& sigma);
00107 
00109 real  gamdev(int ia);
00112 real  poidev(real xm);
00114 real  bnldev(real pp, int n=1);
00116 inline real binomial_sample(real prob1) { return bnldev(prob1); }
00117 
00119 int  multinomial_sample(const Vec& distribution);
00120 
00122 int uniform_multinomial_sample(int N);
00123 
00125 template <class T>
00126 void bootstrap_rows(const TMat<T>& source, TMat<T> destination)
00127 {
00128     int N=source.length();
00129     destination.resize(N,source.width());
00130     for (int i=0;i<N;i++)
00131     {
00132         int j = uniform_multinomial_sample(N);
00133         destination(i) << source(j);
00134     }
00135 }
00136 
00138 void fill_random_uniform(const Vec& dest, real minval=0, real maxval=1);
00139 
00141 void fill_random_discrete(const Vec& dest, const Vec& set);
00142 
00144 void fill_random_normal(const Vec& dest, real mean=0, real stdev=1);
00145 
00147 void fill_random_normal(const Vec& dest, const Vec& mean, const Vec& stdev);
00148 
00149 void fill_random_uniform(const Mat& dest, real minval=0, real maxval=1);
00150 void fill_random_normal(const Mat& dest, real mean=0, real sdev=1);
00151 
00155 void random_subset_indices(const TVec<int>& dest, int n);
00156 
00158 template<class T>
00159 void shuffleElements(const TVec<T>& vec)
00160 {
00161     if (vec.isEmpty())
00162         return; // Do not try to shuffle an empty vec.
00163     
00164     T* v = vec.data();
00165     for(int i=0; i<vec.length(); i++)
00166     {
00167         int j = i+(int)(uniform_sample()*(vec.length()-i));
00168         // int j=(int)floor(i+uniform_sample()*(length()-i-1e-5));
00169         if(j!=i)
00170         {
00171             T tmp = v[i];
00172             v[i] = v[j];
00173             v[j] = tmp;
00174         }
00175     }
00176 }
00177 
00178 
00179 // Performs a random permutation of all the rows of this Mat
00180 template<class T>
00181 void shuffleRows(const TMat<T>& mat)
00182 {
00183     for(int i=0; i<mat.length(); i++)
00184     {
00185         int j = i+int(uniform_sample()*(mat.length()-i));
00186         mat.swapRows(i,j);
00187     }
00188 }
00189 
00198 template<class T>
00199 TVec<int> computeRanks(const TMat<T>& mat, TMat<T>& ranks, bool ignore_missing = false)
00200 {
00201     TVec<int> result;
00202     int width=mat.width();
00203     int n=mat.length();
00204     ranks.resize(n,width);
00205     TVec<Mat> sorted(width); 
00206     // Sort all the y's.
00207     for (int j=0;j<width;j++)
00208         sorted[j].resize(n,2);
00209     if (ignore_missing) {
00210         // We do not know in advance how many non-missing values there are.
00211         for (int j = 0; j < width; j++)
00212             sorted[j].resize(0,2);
00213         Vec val(2);
00214         for (int i = 0; i < n; i++)
00215             for (int j = 0; j < width; j++) {
00216                 val[0] = mat(i,j);
00217                 if (!is_missing(val[0])) {
00218                     val[1] = i;
00219                     sorted[j].appendRow(val);
00220                 }
00221             }
00222         result.resize(width);
00223         for (int j = 0; j < width; j++)
00224             result[j] = sorted[j].length();
00225     } else {
00226         for (int i=0;i<n;i++)
00227         {
00228             for (int j=0;j<width;j++)
00229             {
00230                 sorted[j](i,0)=mat(i,j);
00231 #ifdef BOUNDCHECK
00232                 if (is_missing(sorted[j](i,0)))
00233                     PLERROR("In computeRanks - Found a missing value, but 'ignore_missing' is false");
00234 #endif
00235                 sorted[j](i,1)=i;
00236             }
00237         }
00238     }
00239     for (int j=0;j<width;j++)
00240     {
00241         shuffleRows(sorted[j]); // To randomly permute the order of elements which have the same value, i.e. their rank within their category
00242         sortRows(sorted[j]);
00243     }
00244     // Compute the ranks.
00245     if (ignore_missing)
00246         ranks.fill(-1);
00247     for (int j=0;j<width;j++)
00248         for (int i=0;i<sorted[j].length();i++)
00249             ranks(int(sorted[j](i,1)),j) = i;
00250     return result;
00251 }
00252 
00253 } // end of namespace PLearn
00254 
00255 #endif
00256 
00257 
00258 /*
00259   Local Variables:
00260   mode:c++
00261   c-basic-offset:4
00262   c-file-style:"stroustrup"
00263   c-file-offsets:((innamespace . 0)(inline-open . 0))
00264   indent-tabs-mode:nil
00265   fill-column:79
00266   End:
00267 */
00268 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines