Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
STA 6106 – Fall 2009 Simulation of Two Independent Random Samples from a Standard Normal Distribution Using the Box-Muller Transformation The following program simulates two independent random samples of an arbitrary size from a standard normal distribution, through use of the Box-Muller transformation. The input values are the sample size and the initial seed. 5 15 PROGRAM BOXMULLER INTEGER K DOUBLE PRECISION X(20000), Y(20000), U1, U2, RK, SUMX, SUMX2, THETA, PI, R2 DOUBLE PRECISION MEANX, VARX, SDX, MEANY, VARY, SDY, RX, SEED DOUBLE PRECISION PERCVARX, PERCSDX, PERCVARY, PERCSDY, SUMY, SUMY2 PRINT*, 'Input sample size: ' READ*, K PRINT*, 'Input initial seed: ' READ*, SEED PI = 3.14159265358979323846 RK = K SUMX = 0 SUMX2 = 0 SUMY = 0 SUMY2 = 0 DO 15 I = 1, K CALL RANDOM1(U1, RX, SEED) SEED = RX CALL RANDOM1(U2, RX, SEED) SEED = RX R2 = -2*LOG(U1) THETA = 2*PI*U2 X(I) = SQRT(R2)*COS(THETA) Y(I) = SQRT(R2)*SIN(THETA) SUMX = SUMX + X(I) SUMY = SUMY + Y(I) SUMX2 = SUMX2 + (X(I)*X(I)) SUMY2 = SUMY2 + (Y(I)*Y(I)) CONTINUE MEANX = SUMX/RK MEANY = SUMY/RK VARX = (SUMX2 - (MEANX*SUMX))/(RK-1.) VARY = (SUMY2 - (MEANY*SUMY))/(RK-1.) PERCVARX = 100.*(VARX - 1.) PERCVARY = 100.*(VARY - 1.) SDX = SQRT(VARX) SDY = SQRT(VARY) PERCSDX = 100.*(SDX - 1.) PERCSDY = 100.*(SDY - 1.) PRINT*, 'The sample mean for X is: ', MEANX PRINT*, 'The sample mean for Y is: ', MEANY PRINT*, 'The sample variance for X is: ', VARX PRINT*, ' Which differs from the distribution variance by: ', PERCVARX, '%.' PRINT*, 'The sample variance for Y is: ', VARY PRINT*, ' Which differs from the distribution variance by: ', PERCVARY, '%.' PRINT*, 'The sample std. deviation for X is: ', SDX PRINT*, ' Which differs from the distribution std. dev. by: ', PERCSDX, '%.' PRINT*, 'The sample std. deviation for Y is: ', SDY PRINT*, ' Which differs from the distribution std. dev. by: ', PERCSDY, '%.' END SUBROUTINE RANDOM1(U, RX, IDUM) DOUBLE PRECISION RX, U, IDUM A = 16807 B = 2147483647 X = MOD(A*IDUM, B) RX = X U = RX/B RETURN END With SEED = 5419 and sample size K = 20,000, we obtain: Sample Mean for X = -0.00180550547. Sample Mean for Y = -0.00462067158. Sample Variance for X = 1.0022515, which differs from the distribution variance by 0.2252%. Sample Variance for Y = 0.999142334, which differs from the distribution variance by -0.0858%. Sample Standard Deviation for X = 1.00112512, which differs from the distribution standard deviation by 0.1125%. Sample Standard Deviation for Y = 0.999571075, which differs from the distribution standard deviation by -0.0429%.