1use 5.006; 2use strict; 3use warnings; 4 5package Math::Random::OO::Bootstrap; 6# ABSTRACT: Generate random numbers with bootstrap resampling from a non-parametric distribution 7our $VERSION = '0.22'; # VERSION 8 9# Required modules 10use Carp; 11use Params::Validate 0.76 ':all'; 12 13# ISA 14use base qw( Class::Accessor::Fast ); 15 16 17{ 18 my $param_spec = { 19 data => { type => ARRAYREF }, 20 size => { type => SCALAR } 21 }; 22 23 __PACKAGE__->mk_accessors( keys %$param_spec ); 24 #__PACKAGE__->mk_ro_accessors( keys %$param_spec ); 25 26 sub new { 27 my $class = shift; 28 my $self = bless {}, ref($class) ? ref($class) : $class; 29 if ( @_ == 0 ) { 30 croak 'Math::Random::OO::Bootstrap->new() requires an argument'; 31 } 32 $self->data( ref $_[0] eq 'ARRAY' ? [ @{ $_[0] } ] : [@_] ); 33 $self->size( scalar @{ $self->data } ); 34 return $self; 35 } 36} 37 38 39sub seed { 40 my $self = shift; 41 srand( $_[0] ); 42} 43 44 45sub next { 46 my ($self) = @_; 47 my $rnd = int( rand( $self->size ) ); # index 0 to (size-1) 48 return $self->data->[$rnd]; 49} 50 511; 52 53__END__ 54 55=pod 56 57=encoding utf-8 58 59=head1 NAME 60 61Math::Random::OO::Bootstrap - Generate random numbers with bootstrap resampling from a non-parametric distribution 62 63=head1 VERSION 64 65version 0.22 66 67=head1 SYNOPSIS 68 69 use Math::Random::OO::Bootstrap; 70 @sample = qw( 2 3 3 4 4 5 5 6 6 7 ); 71 $prng = Math::Random::OO::Bootstrap->new(@sample); 72 $prng->seed(42); 73 $prng->next() # draws randomly from the sample 74 75=head1 DESCRIPTION 76 77This subclass of L<Math::Random::OO> generates random numbers with bootstrap 78resampling (i.e. resampling with replacement) from a given set of observations. 79Each item in the sample array is drawn with equal probability. 80 81=head1 METHODS 82 83=head2 C<new> 84 85 $prng = Math::Random::OO::Bootstrap->new(@sample); 86 $prng = Math::Random::OO::Bootstrap->new(\@sample); 87 88C<new> takes either a list or a reference to an array containing a 89set of observations and returns a new C<Math::Random::OO::Bootstrap> object. 90If a reference is provided, the object will make an internal copy 91of the array to avoid unexpected results if the source reference 92is modified. 93 94If the desired sample is an array of array references, the list 95must be enclosed in an anonymous array reference to avoid ambiguity. 96 97 @sample = ( [ 1, 2, 3], [2, 3, 4] ); 98 99 # Correct way 100 $prng = Math::Random::OO::Bootstrap->new( [ @sample ] ); 101 102 # Incorrect -- will only use [1, 2, 3] as the desired sample 103 $prng = Math::Random::OO::Bootstrap->new( @sample ); 104 105It is an error to call C<new> with no arguments. 106 107=head2 C<seed> 108 109 $rv = $prng->seed( @seeds ); 110 111This method seeds the random number generator. At the moment, only the 112first seed value matters. It should be a positive integer. 113 114=head2 C<next> 115 116 $rnd = $prng->next(); 117 118This method returns the next random number from the random number generator 119by resampling with replacement from the provided data. It does not take any 120parameters. 121 122=head1 AUTHOR 123 124David Golden <dagolden@cpan.org> 125 126=head1 COPYRIGHT AND LICENSE 127 128This software is Copyright (c) 2013 by David Golden. 129 130This is free software, licensed under: 131 132 The Apache License, Version 2.0, January 2004 133 134=cut 135