random_dists = [ 'Normal(1, 1)' , 'Lognormal(1, 1)' , 'Exp(1)' , 'Gumbel(6, 4)' ,
'Triangular(2, 9, 11)' ]
N = 500
norm = np . random . normal ( 1 , 1 , N )
logn = np . random . lognormal ( 1 , 1 , N )
expo = np . random . exponential ( 1 , N )
gumb = np . random . gumbel ( 6 , 4 , N )
tria = np . random . triangular ( 2 , 9 , 11 , N )
bootstrap_indices = np . random . randint ( 0 , N , N )
data = [
norm , norm [ bootstrap_indices ],
logn , logn [ bootstrap_indices ],
expo , expo [ bootstrap_indices ],
gumb , gumb [ bootstrap_indices ],
tria , tria [ bootstrap_indices ],
]
fig , ax1 = plt . subplots ( figsize = ( 10 , 6 ))
fig . canvas . manager . set_window_title ( 'A Boxplot Example' )
fig . subplots_adjust ( left = 0.075 , right = 0.95 , top = 0.9 , bottom = 0.25 )
bp = ax1 . boxplot ( data , notch = False , sym = '+' , orientation = 'vertical' , whis = 1.5 )
plt . setp ( bp [ 'boxes' ], color = 'black' )
plt . setp ( bp [ 'whiskers' ], color = 'black' )
plt . setp ( bp [ 'fliers' ], color = 'red' , marker = '+' )
ax1 . yaxis . grid ( True , linestyle = '-' , which = 'major' , color = 'lightgrey' ,
alpha = 0.5 )
ax1 . set (
axisbelow = True ,
title = 'Comparison of IID Bootstrap Resampling Across Five Distributions' ,
xlabel = 'Distribution' ,
ylabel = 'Value' ,
)
box_colors = [ 'darkkhaki' , 'royalblue' ]
num_boxes = len ( data )
medians = np . empty ( num_boxes )
for i in range ( num_boxes ):
box = bp [ 'boxes' ][ i ]
box_x = []
box_y = []
for j in range ( 5 ):
box_x . append ( box . get_xdata ()[ j ])
box_y . append ( box . get_ydata ()[ j ])
box_coords = np . column_stack ([ box_x , box_y ])
ax1 . add_patch ( Polygon ( box_coords , facecolor = box_colors [ i % 2 ]))
med = bp [ 'medians' ][ i ]
median_x = []
median_y = []
for j in range ( 2 ):
median_x . append ( med . get_xdata ()[ j ])
median_y . append ( med . get_ydata ()[ j ])
ax1 . plot ( median_x , median_y , 'k' )
medians [ i ] = median_y [ 0 ]
ax1 . plot ( np . average ( med . get_xdata ()), np . average ( data [ i ]),
color = 'w' , marker = '*' , markeredgecolor = 'k' )
ax1 . set_xlim ( 0.5 , num_boxes + 0.5 )
top = 40
bottom = - 5
ax1 . set_ylim ( bottom , top )
ax1 . set_xticklabels ( np . repeat ( random_dists , 2 ),
rotation = 45 , fontsize = 8 )
pos = np . arange ( num_boxes ) + 1
upper_labels = [ str ( round ( s , 2 )) for s in medians ]
weights = [ 'bold' , 'semibold' ]
for tick , label in zip ( range ( num_boxes ), ax1 . get_xticklabels ()):
k = tick % 2
ax1 . text ( pos [ tick ], .95 , upper_labels [ tick ],
transform = ax1 . get_xaxis_transform (),
horizontalalignment = 'center' , size = 'x-small' ,
weight = weights [ k ], color = box_colors [ k ])
fig . text ( 0.80 , 0.08 , f ' { N } Random Numbers' ,
backgroundcolor = box_colors [ 0 ], color = 'black' , weight = 'roman' ,
size = 'x-small' )
fig . text ( 0.80 , 0.045 , 'IID Bootstrap Resample' ,
backgroundcolor = box_colors [ 1 ],
color = 'white' , weight = 'roman' , size = 'x-small' )
fig . text ( 0.80 , 0.015 , '*' , color = 'white' , backgroundcolor = 'silver' ,
weight = 'roman' , size = 'medium' )
fig . text ( 0.815 , 0.013 , ' Average Value' , color = 'black' , weight = 'roman' ,
size = 'x-small' )
plt . show ()