Bio/Tools/RandomDistFunctions.pm

filter: Statementstmt 85.1% 40 / 47 Branchbran 56.2% 9 / 16 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 8 / 8 Podpod 75.0% 3 / 4 totaltotal 69.4% 66 / 95 CC 13 SCAR 15.1
CC
13
Cov
72%
rand_geometric_distribution
6.6
rand_birth_distribution
5.6
rand_exponentional_distribution
5.6
CRAP 4.5
« Bio/Ontology/Relationship.pm ↑ Summary Bio/Search/HSP/WABAHSP.pm »
1 #
2 # BioPerl module for Bio::Tools::RandomDistFunctions
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason-at-bioperl.org>
7 #
8 # Copyright Jason Stajich
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::Tools::RandomDistFunctions - A set of routines useful for
17 generating random data in different distributions
18  
19 =head1 SYNOPSIS
20  
21 use Bio::Tools::RandomDistFunctions;
22 my $dist = Bio::Tools::RandomDistFunctions->new();
23 for my $v ( 1..1000 ) {
24 my $birth_dist = $dist->rand_birth_distribution($lambda);
25 # ... do something with the variable
26 }
27  
28 =head1 DESCRIPTION
29  
30 Most of the code is based on the C implementation of these routines in
31 Mike Sanderson's r8s's package. See http://loco.biosci.arizona.edu/r8s/ for
32 information on his software.
33  
34 =for comment
35 This code tries to be fast and use available faster BigInt and GMP
36 library methods when those modules are available.
37  
38 =head1 FEEDBACK
39  
40 =head2 Mailing Lists
41  
42 User feedback is an integral part of the evolution of this and other
43 Bioperl modules. Send your comments and suggestions preferably to
44 the Bioperl mailing list. Your participation is much appreciated.
45  
46 bioperl-l@bioperl.org - General discussion
47 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
48  
49 =head2 Support
50  
51 Please direct usage questions or support issues to the mailing list:
52  
53 I<bioperl-l@bioperl.org>
54  
55 rather than to the module maintainer directly. Many experienced and
56 reponsive experts will be able look at the problem and quickly
57 address it. Please include a thorough description of the problem
58 with code and data examples if at all possible.
59  
60 =head2 Reporting Bugs
61  
62 Report bugs to the Bioperl bug tracking system to help us keep track
63 of the bugs and their resolution. Bug reports can be submitted via the
64 web:
65  
66 https://github.com/bioperl/bioperl-live/issues
67  
68 =head1 AUTHOR - Jason Stajich
69  
70 Email jason-at-bioperl.org
71  
72 =head1 CONTRIBUTORS
73  
74 Thanks to Mike Sanderson for assistance in the getting this
75 implementation together.
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object methods.
80 Internal methods are usually preceded with a _
81  
82 =cut
83  
84  
85 # Let the code begin...
86  
87  
88 package Bio::Tools::RandomDistFunctions;
89 4 $Bio::Tools::RandomDistFunctions::VERSION = '1.7.8';
90 4 require Exporter;
91 4 ▶use vars qw(%LOADED @EXPORT_OK); use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 #use Math::BigFloat lib => 'GMP,Bit::Vector';
94 #use Math::BigInt lib => 'GMP,Bit::Vector';
95 4 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 4 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 =head2 birth_distribution
100  
101 Title : rand_birth_distribution
102 Usage : my $randvar = $dist->
103 rand_birth_distribution($lambda);
104 Function: Returns a random number from a birth process waiting
105 time with a fixed interval
106 1.0. Times are measured from 0=present,1=root;
107 Returns : floating point number
108 Args : $lambda ( > 0 )
109 References : This is based on code by Mike Sanders in r8s.
110 Ross, Stochastic Processes, p. 145 for the density
111  
112 =cut
113  
114 4 ▶sub rand_birth_distribution{
Subroutine rand_birth_distribution: called CC=4 SCAR=17.2
Pod undocumented
115 4 my ($self,$lambda) = @_;
116 4 ▶ if( ! ref($self) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self or $self =~ /RandomDistFunctions/) 0 4 4
Condition33%1/3
ref $self or $self =~ /RandomDistFunctions/
l!l&&r!l&&!r
400
MC/DC0%0/2
ref $self or $self =~ /RandomDistFunctions/
ref $self $self =~ /RandomDistFunctions/
Truth table33%1/3
A or B
A= ref $selfB= $self =~ /RandomDistFunctions/
ABresult
1X1
011
000
117 $self !~ /RandomDistFunctions/ ) {
118 0 $lambda = $self;
119 }
120 4 ▶ unless( $lambda ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lambda) 0 4 4
121 0 $self->throw("Cannot call birth_distribution without a valid lambda value (>0)");
122 }
123 4 return 1 - (log(rand(1) * (exp($lambda) - 1)+1)/ $lambda);
124 }
125  
126  
127 =head2 rand_geometric_distribution
128  
129 Title : rand_geometric_distribution
130 Usage : my $randvar = $dist->rand_geometric_distribution($param);
131 Function: Returns a random geometric variate distributed with
132 parameter $param, according to
133 c.d.f. 1 - ( 1- param) ^ n
134 Returns : integer
135 Args : $param ( 0 > $param < 1 )
136  
137  
138 =cut
139  
140 1 ▶sub rand_geometric_distribution{
Subroutine rand_geometric_distribution: called CC=5 SCAR=18.9
Pod documented
141 1 my ($self,$param) = @_;
142 1 ▶ if( ! ref($self) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self or $self =~ /RandomDistFunctions/) 0 1 1
Condition33%1/3
ref $self or $self =~ /RandomDistFunctions/
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self or $self =~ /RandomDistFunctions/
ref $self $self =~ /RandomDistFunctions/
Truth table33%1/3
A or B
A= ref $selfB= $self =~ /RandomDistFunctions/
ABresult
1X1
011
000
143 $self !~ /RandomDistFunctions/ ) {
144 0 $param = $self;
145 }
146 1 ▶ unless( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param) 0 1 1
147 0 $self->throw("Cannot call rand_geometric_distribution without a valid param value (>0)");
148 }
149  
150 1 my $den;
151 1 ▶ if( $param < 1e-8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param < 1e-08) { } 0 1 1
152 0 $den = (-1 * $param) - ( $param * $param ) / 2;
153 } else {
154 1 $den = log(1 - $param);
155 }
156 1 my $z = log(1 - rand(1)) / $den;
157 1 return POSIX::floor($z) + 1;
158 # MSanderson comments from r8s code
159 # Is this the right truncation of the real-valued expression above?
160 # YES
161 # Checked by reference to the expected mean of the distribution in
162 # 100,000 replicates
163 # EX = 1/param Var = (1-param)/param^2 See Olkin, Gleser, and
164 # Derman, p. 193ff. Probability Models and Applications, 1980.
165 }
166  
167 =head2 rand_exponentional_distribution
168  
169 Title : rand_exponentional_distribution
170 Usage : my $var = $dist->rand_exponentional_distribution($param);
171 Function: Returns a random exponential variate distributed with parameter
172 $param, according to c.d.f 1 - e^(-param * x)
173 Returns : floating point number
174 Args : $param ( > 0 )
175  
176  
177 =cut
178  
179 1 ▶sub rand_exponentional_distribution {
Subroutine rand_exponentional_distribution: called CC=4 SCAR=17.2
Pod documented
180 1 my ($self,$param) = @_;
181 1 ▶ if( ! ref($self) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self or $self =~ /RandomDistFunctions/) 0 1 1
Condition33%1/3
ref $self or $self =~ /RandomDistFunctions/
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self or $self =~ /RandomDistFunctions/
ref $self $self =~ /RandomDistFunctions/
Truth table33%1/3
A or B
A= ref $selfB= $self =~ /RandomDistFunctions/
ABresult
1X1
011
000
182 $self !~ /RandomDistFunctions/ ) {
183 0 $param = $self;
184 }
185 1 ▶ unless( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param) 0 1 1
186 0 $self->throw("Cannot call rand_exponentional_distribution without a valid param value (>0)");
187 }
188 1 return log( 1- rand(1)) / $param;
189 }
190  
191 =head2 rand_normal_distribution
192  
193 Title : rand_normal_distribution
194 Usage : my $var = $dist->rand_normal_distribution()
195 Function: Returns a random normal (gaussian) variate distributed
196 Returns : floating point number
197 Args : none
198  
199  
200 =cut
201  
202 1 ▶sub rand_normal_distribution{
Subroutine rand_normal_distribution: called CC=3 SCAR=11.0
Pod documented
203 1 my $gset;
204 1 my ($rsq,$v1,$v2) = ( 0,0,0);
205 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$rsq >= 1 || $rsq == 0 and do { do { $v1 = 2 * rand(1) - 1; $v2 = 2 * rand(1) - 1; $rsq = $v1 ** 2 + $v2 ** 2 } } 1 1 2
Condition66%2/3
$rsq >= 1 or $rsq == 0
l!l&&r!l&&!r
101
MC/DC50%1/2
$rsq >= 1 or $rsq == 0
$rsq >= 1 $rsq == 0
Truth table66%2/3
A or B
A= $rsq >= 1B= $rsq == 0
ABresult
1X1
011
000
206 2 $v1 = 2 * rand(1) - 1;
207 2 $v2 = 2 * rand(1) - 1;
208 2 $rsq= $v1**2 + $v2 ** 2;
209 } while( $rsq >= 1 || $rsq == 0);
210 1 my $fac = sqrt(-2.0 * log($rsq) / $rsq );
211 1 $gset = $v1 * $fac;
212 1 return $v2 * $fac;
213 }
214  
215 4 1;
« Bio/Ontology/Relationship.pm ↑ Summary Bio/Search/HSP/WABAHSP.pm »