Changeset 3075 in Sophya for trunk/SophyaLib/NTools/perandom.cc
- Timestamp:
- Sep 14, 2006, 11:43:30 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/perandom.cc
r2870 r3075 6 6 #include <iostream> 7 7 8 8 9 //////////////////////////////////////////////////////////////////////////// 9 //++ 10 // Class FunRan 11 // Lib Outils++ 12 // include perandom.h 13 // 14 // Tirage aleatoire sur un histogramme 1D. 15 //-- 16 17 //++ 10 /*! 11 \class SOPHYA::FunRan 12 \ingroup NTools 13 Classe for generating random variables from 1D function 14 */ 15 16 17 /********* Methode *********/ 18 /*! Createur. f is a probability density function (PDF). 19 Le tirage aleatoire est fait sur un histogramme 20 Histo(xMin,xMax,nBin) (voir convention dans Histo). 21 Chaque bin de l'histogramme contient la valeur de la PDF 22 au centre du bin. 23 Les valeurs retournees sont les valeurs du centre des bins. 24 Si binw est la largeur du bin, les valeurs retournees 25 vont de xmin+binw/2 a xmax-binw/2. 26 */ 18 27 FunRan::FunRan(FunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin) 19 //20 // Createur. f is a probability density function (PDF).21 // Le tirage aleatoire est fait sur un histogramme22 // Histo(xMin,xMax,nBin) (voir convention dans Histo).23 // Chaque bin de l'histogramme contient la valeur de la PDF24 // au centre du bin.25 // Les valeurs retournees sont les valeurs du centre des bins.26 // Si binw est la largeur du bin, les valeurs retournees27 // vont de xmin+binw/2 a xmax-binw/2.28 //--29 28 : Histo(xMin,xMax,nBin) 30 29 { … … 42 41 } 43 42 44 //++ 43 /********* Methode *********/ 44 /*! Createur. tab is a probability density function. 45 The return random values will be between 0 and nBin-1. 46 See FunRan::FunRan(FunRan::Func...) for further comments. 47 */ 45 48 FunRan::FunRan(r_8 *tab, int_4 nBin) 46 //47 // Createur. tab is a probability density function48 // The return random values will be between 0 and nBin-149 // See FunRan::FunRan(FunRan::Func...) for further comments.50 //--51 49 : Histo(-0.5,nBin-0.5,nBin) 52 50 { … … 63 61 } 64 62 65 //++ 63 /********* Methode *********/ 64 /*! Createur. tab is a probability density function 65 The content of tab is identified has the content of 66 an Histogram define by Histo(xMin,xMax,nBin). 67 See FunRan::FunRan(FunRan::Func...) for further comments. 68 */ 66 69 FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax) 67 //68 // Createur. tab is a probability density function69 // The content of tab is identified has the content of70 // an Histogram define by Histo(xMin,xMax,nBin).71 // See FunRan::FunRan(FunRan::Func...) for further comments.72 //--73 70 : Histo(xMin,xMax,nBin) 74 71 { … … 85 82 } 86 83 87 / /++88 / /Createur.89 //If pdf=true, h is a probability density fonction.90 //If pdf=false, h is a distribution function (not necessarly normalized to 1).91 //See FunRan::FunRan(FunRan::Func...) for further comments.92 //-- 84 /********* Methode *********/ 85 /*! Createur. 86 If pdf=true, h is a probability density fonction. 87 If pdf=false, h is a distribution function (not necessarly normalized to 1). 88 See FunRan::FunRan(FunRan::Func...) for further comments. 89 */ 93 90 FunRan::FunRan(Histo &h, bool pdf) 94 91 : Histo(h) … … 107 104 } 108 105 109 //++ 106 /********* Methode *********/ 107 /*! Tirage avec retour du numero de bin entre 0 et mBins-1. 108 It returns the first bin whose content is greater or equal 109 to the random uniform number (in [0,1]) 110 */ 110 111 int_4 FunRan::BinRandom() 111 //112 // Tirage avec retour du numero de bin entre 0 et mBins-1.113 // It returns the first bin whose content is greater or equal114 // to the random uniform number (in [0,1])115 //--116 112 { 117 113 // recherche du premier bin plus grand ou egal a z … … 121 117 } 122 118 123 //++ 119 /********* Methode *********/ 120 /*! Tirage avec retour abscisse du bin non interpole. */ 124 121 r_8 FunRan::Random() 125 //126 // Tirage avec retour abscisse du bin non interpole.127 //--128 122 { 129 123 r_8 z=drand01(); … … 138 132 139 133 //////////////////////////////////////////////////////////////////////////// 140 //++ 141 // Class FunRan2D 142 // Lib Outils++ 143 // include perandom.h 144 // 145 // Tirage aleatoire sur un histogramme 2D. 146 //-- 147 148 //++ 134 /*! 135 \class SOPHYA::FunRan2D 136 \ingroup NTools 137 Classe for generating random variables from 2D function 138 */ 139 140 /********* Methode *********/ 141 /*! Creator for random from a table */ 149 142 FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY) 150 //151 // Createur.152 //--153 143 { 154 144 // Tirage en X, somme sur les Y. … … 171 161 } 172 162 173 //++ 163 /********* Methode *********/ 164 /*! Creator for random from a table */ 174 165 FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY) 175 //176 // Createur.177 //--178 166 { 179 167 // Tirage en X, somme sur les Y. … … 198 186 } 199 187 188 /********* Methode *********/ 189 /*! Destructor */ 200 190 FunRan2D::~FunRan2D() 201 191 { … … 208 198 } 209 199 210 //++ 200 /********* Methode *********/ 201 /*! Tirage avec retour du numeros de bin. */ 211 202 void FunRan2D::BinRandom(int_4& x, int_4& y) 212 //213 // Tirage avec retour du numeros de bin.214 //--215 203 { 216 204 x = ranX->BinRandom(); … … 218 206 } 219 207 220 //++ 208 /********* Methode *********/ 209 /*! Tirage avec retour abscisse et ordonnee du bin interpole. */ 221 210 void FunRan2D::Random(r_8& x, r_8& y) 222 //223 // Tirage avec retour abscisse et ordonnee224 // du bin interpole.225 //--226 211 { 227 212 x = ranX->Random(); … … 229 214 y = ranY[i]->Random(); 230 215 } 216 217 218 219 ///////////////////////////////////////////////////////////////// 220 /* 221 --- Remarque sur complex< r_8 > ComplexGaussRan(double sig) 222 x = r cos(t) tire gaussien: pdf f(x) = 1/(sqrt(2Pi) Sx) exp(-(x-Mx)^2/(2 Sx^2)) 223 y = r sin(t) tire gaussien: pdf f(y) = 1/(sqrt(2Pi) Sy) exp(-(y-My)^2/(2 Sy^2)) 224 x,y independants --> pdf f(x,y) = f(x) f(y) 225 - On cherche la pdf g(r,t) du module et de la phase 226 (r=sqrt(x^2+y^2,t=atan2(y,x)) --> (x,y): le Jacobien = r 227 g(r,t) = r f(x,y) = r f(x) f(y) 228 = r/(2Pi Sx Sy) exp(-(x-Mx)^2/(2 Sx^2)) exp(-(y-My)^2/(2 Sy^2)) 229 - Le cas general est complique 230 (cf D.Pelat cours DEA "bruits et signaux" section 4.5) 231 - Cas ou "Mx = My = 0" et "Sx = Sy = S" 232 g(r,t) = r/(2Pi S^2) exp(-r^2/(2 S^2)) 233 La distribution de "r" est donc: 234 g(r) = Integrate[g(r,t),{t,0,2Pi}] 235 = r/S^2 exp(-r^2/(2 S^2)) 236 La distribution de "t" est donc: 237 g(t) = Integrate[g(r,t),{r,0,Infinity}] 238 = 1 / 2Pi (distribution uniforme sur [0,2Pi[) 239 Les variables aleatoires r,t sont independantes: 240 g(r,t) = g(r) g(t) 241 */
Note:
See TracChangeset
for help on using the changeset viewer.