Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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,v 1.7 2004/04/12 00:36:28 yoshua Exp $ 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 00061 real incomplete_beta(real z, real x, real y); 00063 //double incbet(double x, double y, double z); 00064 00066 real student_t_cdf(real t, int nb_degrees_of_freedom); 00067 00074 void seed(); 00076 void manual_seed(long x); 00078 long get_seed(); 00079 00081 real uniform_sample(); 00083 real bounded_uniform(real a,real b); 00084 00086 real expdev(); 00088 real gaussian_01(); 00089 inline real normal_sample() { return gaussian_01(); } 00090 00092 real gaussian_mu_sigma(real mu, real sigma); 00093 00097 real gaussian_mixture_mu_sigma(Vec& w, const Vec& mu, const Vec& sigma); 00098 00100 real gamdev(int ia); 00102 real poidev(real xm); 00104 real bnldev(real pp, int n=1); 00106 inline real binomial_sample(real prob1) { return bnldev(prob1); } 00107 00109 int multinomial_sample(const Vec& distribution); 00110 00112 int uniform_multinomial_sample(int N); 00113 00115 template <class T> 00116 void bootstrap_rows(const TMat<T>& source, TMat<T> destination) 00117 { 00118 int N=source.length(); 00119 destination.resize(N,source.width()); 00120 for (int i=0;i<N;i++) 00121 { 00122 int j = uniform_multinomial_sample(N); 00123 destination(i) << source(j); 00124 } 00125 } 00126 00128 void fill_random_uniform(const Vec& dest, real minval=0, real maxval=1); 00129 00131 void fill_random_discrete(const Vec& dest, const Vec& set); 00132 00134 void fill_random_normal(const Vec& dest, real mean=0, real stdev=1); 00135 00137 void fill_random_normal(const Vec& dest, const Vec& mean, const Vec& stdev); 00138 00139 void fill_random_uniform(const Mat& dest, real minval=0, real maxval=1); 00140 void fill_random_normal(const Mat& dest, real mean=0, real sdev=1); 00141 00142 00144 template<class T> 00145 void shuffleElements(const TVec<T>& vec) 00146 { 00147 T* v = vec.data(); 00148 for(int i=0; i<vec.length(); i++) 00149 { 00150 int j = i+(int)(uniform_sample()*(vec.length()-i)); 00151 // int j=(int)floor(i+uniform_sample()*(length()-i-1e-5)); 00152 if(j!=i) 00153 { 00154 T tmp = v[i]; 00155 v[i] = v[j]; 00156 v[j] = tmp; 00157 } 00158 } 00159 } 00160 00161 00162 // Performs a random permutation of all the rows of this Mat 00163 template<class T> 00164 void shuffleRows(const TMat<T>& mat) 00165 { 00166 for(int i=0; i<mat.length(); i++) 00167 { 00168 int j = i+int(uniform_sample()*(mat.length()-i)); 00169 mat.swapRows(i,j); 00170 } 00171 } 00172 00176 template<class T> 00177 void computeRanks(const TMat<T>& mat, TMat<T>& ranks) 00178 { 00179 int width=mat.width(); 00180 int n=mat.length(); 00181 ranks.resize(n,width); 00182 TVec<Mat> sorted(width); 00183 // sort all the y's 00184 for (int j=0;j<width;j++) 00185 sorted[j].resize(n,2); 00186 for (int i=0;i<n;i++) 00187 { 00188 for (int j=0;j<width;j++) 00189 { 00190 sorted[j](i,0)=mat(i,j); 00191 sorted[j](i,1)=i; 00192 } 00193 } 00194 for (int j=0;j<width;j++) 00195 { 00196 shuffleRows(sorted[j]); // to randomly permute the order of elements which have the same value, i.e. their rank within their category 00197 sortRows(sorted[j]); 00198 } 00199 // compute the ranks 00200 for (int i=0;i<n;i++) 00201 for (int j=0;j<width;j++) 00202 ranks(int(sorted[j](i,1)),j) = i; 00203 } 00204 00205 } // end of namespace PLearn 00206 00207 #endif 00208 00209 00210 00211 00212 00213 00214

Generated on Tue Aug 17 16:03:21 2004 for PLearn by doxygen 1.3.7