Advertisement
wachiorsino

Nasa.gov

Mar 1st, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. Nasa.gov
  2.  
  3. FUNCTION BBOOTSTRAP,X,NSAMP, FUNCT=FUNCT, SLOW=METHOD
  4. ;
  5. ;+
  6. ; NAME:
  7. ; BBOOTSTRAP
  8. ;
  9. ; PURPOSE:
  10. ; 1-parameter bootstrap calculation of function FUNCT
  11. ;
  12. ;CALLING SEQUENCE:
  13. ; biwt_means = bbootstrap( data, Num_Samp,[FUNCT='biweight_mean', /SLOW] )
  14. ; OR
  15. ; means = bbootstrap( data, Num_Samp )
  16. ;
  17. ; INPUT:
  18. ; DATA = input vector
  19. ; Num_Samp = the number of bootstrap samples to be taken. The more the
  20. ; better. Should be at least 30 if a standard deviation is to
  21. ; be calculated. At least 200 if 95% confidence limits are to
  22. ; be calculated.
  23. ;KEYWORD:
  24. ; FUNCT = the name of the function to be applied. If missing, AVERAGE is
  25. ; assumed.
  26. ; SLOW If present, the BALANCED bootstrap is performed. This is more
  27. ; accurate, especially for long-tailed distributions, but is also
  28. ; much slower and requires more memory.
  29. ;
  30. ; RETURNS:
  31. ; vector of NSAMP bootstrap answers. The mean, standard deviation
  32. ; and confidence intervals can be calculated from this
  33. ;
  34. ;SUBROUTINES CALLED:
  35. ; 'FUNCT'
  36. ; PERMUTE
  37. ;
  38. ;NOTES:
  39. ; This program randomly selects values to fill sets of the same size as Y,
  40. ; Then calculates the FUNCT of each. It does this NSAMP times. If the SLOW
  41. ; mode is requested, the balanced bootstrap method is used to obtain the
  42. ; samples, hence the name BBOOTSTRAP. This method is preferable to that
  43. ; used in BOOT_MEAN, but does require more virtual memory, and patience.
  44. ;
  45. ; The user should choose NSAMP large enough to get a good distribution.
  46. ; The sigma of that distribution is then the standard deviation of the
  47. ; mean of ANSER.
  48. ; For example, if input X is normally distributed with a standard
  49. ; deviation of 1.0, the standard deviation of the vector MEAN will be
  50. ; ~1.0/SQRT(N-1), where N is the number of values in X.
  51. ;
  52. ; WARNING:
  53. ; At least 5 points must be input. The more, the better.
  54. ;
  55. ; REVISION HISTORY:
  56. ; H.T. Freudenreich, HSTX, 2/95
  57. ;-
  58.  
  59. on_error,2
  60.  
  61. IF N_ELEMENTS(FUNCT) EQ 0 THEN USE_DEFAULT=1 ELSE USE_DEFAULT=0
  62. ANSER=FLTARR(NSAMP)
  63. N=LONG(N_ELEMENTS(X))
  64.  
  65. IF KEYWORD_SET(SLOW) THEN BEGIN
  66.  
  67. ; Concatenate everything into one long vector:
  68. M=LONG(NSAMP)*N
  69. BIGGY=FLTARR(M)
  70. K=N-1L
  71. I1=0L
  72. FOR I=0, NSAMP-1 DO BEGIN
  73. BIGGY(I1:I1+K)=X
  74. I1=I1+N
  75. ENDFOR
  76.  
  77. ; Now scramble it!
  78. ; Select M numbers at random, repeating none.
  79. BIGGY=BIGGY(PERMUTE(M))
  80.  
  81. ; Now divide it into NSAMP units and perform the WHATEVER on each.
  82. ANSER=FLTARR(NSAMP)
  83. I1=0L
  84. IF USE_DEFAULT EQ 1 THEN BEGIN
  85. FOR I=0, NSAMP-1 DO BEGIN
  86. ANSER(I)=TOTAL( BIGGY(I1:I1+K) )
  87. I1=I1+N
  88. ENDFOR
  89. ANSER=ANSER/N
  90. ENDIF ELSE BEGIN
  91. FOR I=0, NSAMP-1 DO BEGIN
  92. ANSER(I)=CALL_FUNCTION( FUNCT, BIGGY(I1:I1+K) )
  93. I1=I1+N
  94. ENDFOR
  95. ENDELSE
  96.  
  97. ENDIF ELSE BEGIN
  98.  
  99. R0=SYSTIME(1)*2.+1.
  100. SEEDS=RANDOMU(R0,NSAMP)*1.0E6+1.
  101. FOR I=0,NSAMP-1 DO BEGIN
  102. ; Update the random number seed.
  103. R0=SEEDS(I)
  104. ; Uniform random numbers between 0 and N-1:
  105. PICK=RANDOMU(R0,N)
  106. R=LONG(PICK*N)
  107. V=X(R)
  108. IF USE_DEFAULT EQ 1 THEN ANSER(I)=TOTAL(V)/N ELSE $
  109. ANSER(I)=CALL_FUNCTION( FUNCT, V )
  110. ENDFOR
  111. ENDELSE
  112.  
  113. RETURN,ANSER
  114. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement