| 1 | package Statistics::Sequences::Vnomes; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Algorithm::Combinatorics qw(variations_with_repetition); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Math::Cephes 0.43; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Statistics::Sequences 0.13; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Statistics::Lite qw(sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use base qw(Statistics::Sequences); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | $Statistics::Sequences::Vnomes::VERSION = '0.20'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =pod | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Statistics::Sequences::Vnomes - Serial Test psi-square for equiprobability of v-nomes (or Ngrams) (Good's and Kendall-Babington-Smith's tests) | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Statistics::Sequences::Vnomes 0.20; | ||||||||||||||||||||||||||||
| 21 | my $vnomes = Statistics::Sequences::Vnomes->new(); | ||||||||||||||||||||||||||||
| 22 | $vnomes->load(qw/1 0 0 0 1 1 0 1 1 0 0 1 0 0 1 1 1 1 0 1/); # ordered data, numerical or other, binary or what | ||||||||||||||||||||||||||||
| 23 | my $freq_href = $vnomes->observed(length => 3); # returns hashref of frequencies for trinomes in the sequence | ||||||||||||||||||||||||||||
| 24 | my @freq = $vnomes->observed(length => 3); # returns only observed trinome frequencies (not keyed by trinomes themselves) | ||||||||||||||||||||||||||||
| 25 | $val = $vnomes->expected(length => 3); # mean chance expectation for the frequencies (2.5) | ||||||||||||||||||||||||||||
| 26 | $val = $vnomes->psisq(length => 3); # Good's "second backward differences" psi-square (3.161); see option 'delta' | ||||||||||||||||||||||||||||
| 27 | $val = $vnomes->p_value(length => 3); # for psi-square (0.206) | ||||||||||||||||||||||||||||
| 28 | my $href = $vnomes->stats_hash(length => 3, values => {psisq => 1, p_value => 1}); # include any method (& their options) | ||||||||||||||||||||||||||||
| 29 | $vnomes->dump(length => 3, | ||||||||||||||||||||||||||||
| 30 | values => {observed => 1, expected => 1, psisq => 1, p_value => 1}, # what stats to show (or not if => 0) | ||||||||||||||||||||||||||||
| 31 | format => 'table', flag => 1, precision_s => 3, precision_p => 7, verbose => 1); | ||||||||||||||||||||||||||||
| 32 | # prints: | ||||||||||||||||||||||||||||
| 33 | # Vnomes (3) statistics | ||||||||||||||||||||||||||||
| 34 | #.-----------+-----------+-----------+-----------. | ||||||||||||||||||||||||||||
| 35 | #| observed | expected | p_value | psisq | | ||||||||||||||||||||||||||||
| 36 | #| | | | | | ||||||||||||||||||||||||||||
| 37 | #+-----------+-----------+-----------+-----------+ | ||||||||||||||||||||||||||||
| 38 | #| '011' = 4 | 2.500 | 0.2058681 | 3.161 | | ||||||||||||||||||||||||||||
| 39 | #| '010' = 1 | | | | | ||||||||||||||||||||||||||||
| 40 | #| '111' = 2 | | | | | ||||||||||||||||||||||||||||
| 41 | #| '000' = 1 | | | | | ||||||||||||||||||||||||||||
| 42 | #| '101' = 2 | | | | | ||||||||||||||||||||||||||||
| 43 | #| '001' = 3 | | | | | ||||||||||||||||||||||||||||
| 44 | #| '100' = 3 | | | | | ||||||||||||||||||||||||||||
| 45 | #| '110' = 4 | | | | | ||||||||||||||||||||||||||||
| 46 | #'-----------+-----------+-----------+-----------' | ||||||||||||||||||||||||||||
| 47 | # psisq is Good's delta^2-psi^2, calculated with second backward differences, and has 2 degrees of freedom. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # following and other methods inherited from Statistics::Sequences and its parent, Statistics::Data: | ||||||||||||||||||||||||||||
| 50 | $vnomes->dump_data(delim => ','); # comma-separated single line of the loaded data | ||||||||||||||||||||||||||||
| 51 | $vnomes->save_to_file(path => 'seq.csv'); # for retrieval by load_from_file() method | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This module implements the Kendall-Babington-Smith and Good's serial tests of the independence of successive elements within a sequence. At the least, it can be used to test that the individual elements in the sequence are uniformly distributed--when giving a B<length> of 1 to the function L<psisq|Statistics::Sequences::Vnomes/psisq>. More generally, an array of data is given as an ordered categorical ("stringy") series of events, and a description of this array is wanted in terms of the frequency of its constituent "blocks" of length I<v>, and a test is made of how these counts differ from the counts expected for a sequence generated by the same process, for as many events, of the same length. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Successive elements are defined as I<v>-nomes, a.k.a I<N>-grams, I<v>-plets or I<v>-bits (for binary data); that is, as mononomes, monograms, singletons, or monobits of discrete events; or dinomes, bigrams, doublets, or so on for immediate repetitions and alternations; or as trinomes, trigrams, triplets, etc., and so on, for higher-order sequences. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The test implemented here is an alternative to the monobit "frequency" test, and the I<chi>-square and likelihood ratio (L<G-square|Statistics::Gtest>) tests of independence. These tests generally produce values that are only asymptotically distributed as I<chi>-square; a problem solved by Good in offering a I<chi>-square informed by backward differencing. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Take the following, for example. A sequence of heads and tails (H and T) is generated by coin-flipping. A serial test of the randomness (informativeness, predictability, instability, etc.) of this sequence might involve testing for equal representation of, say, the trinomes HTH, HTT, TTT, THT, and so on. Counting up these trinomes at overlapping points in the sequence (with a moving time window) yields a statistic--I<psi-square>--that is approximately distributed as I<chi>-square; the Kendall-Babington Smith statistic. However, these counts are not independent (given overlaps). Good's Generalized Serial Test--the default test-statistic returned by this module's L<psisq|Statistics::Sequences::Vnomes/psisq> routine--computes I<psi>-square by differencing, taking into account not only the specified B<length> of I<v> but also its value for the first two prior lengths (dinomes and mononomes). This yields the statistic I<delta-square-psi-square> (the "second backward difference" measure) that is I<exactly> distributed as I<chi>-square. (Anything less than a test for trinomes naturally has to fall back on first backward differencing or no differencing at all--which, again, is the Kendall-Babington-Smith test). | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Note also that (1) Good's serial test, as here implemented, is suitable for multi-state, multinomial data--not only binary, dichotomous sequences (and to which, say, the L<runs|Statistics::Sequences::Runs> or L<joins|Statistics::Sequences::Joins> tests are limited); that (2) Good's serial test is I<not> the same as the so-called "serial test" described by Knuth (1998, Ch. 2)--which involves I<non>-overlapping pairs of events within a sequence; and that (3) Good's original paper for this test (i.e., Good, 1953) misprinted the crucial calculation--i.e., without squaring the observation less expectation difference--so that versions of this module pre-0.20 (which implemented the former) are not compatible (to say the least) with earlier versions. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | This module is a "child" of L<Statistics::Sequences|Statistics::Sequences>, and so of L<Statistics:Data|Statistics:Data>. So it offers generic methods, as follows, aside from the specific methods for describing a sequence as per Good's test. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 new | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $vnomes = Statistics::Sequences::Vnomes->new(); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Returns a new Vnomes object. Expects/accepts no arguments but the classname. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 load | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | $vnomes->load(@data); # anonymously | ||||||||||||||||||||||||||||
| 78 | $vnomes->load(\@data); | ||||||||||||||||||||||||||||
| 79 | $vnomes->load('sample1' => \@data); # labelled whatever | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Loads data anonymously or by name - see L<load|Statistics::Data/load, add, unload> in L<Statistics::Data> for ways data can be loaded and retrieved (more than shown here). Every load unloads all previous loads and any additions to them. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Data for this test of sequences can be categorical or numerical, all being treated categorically. Also, the data do not have to be dichotomous (unlike in tests of L<runs|Statistics::Sequences::Runs> and L<joins|Statistics::Sequences::Joins>. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 add, access, unload | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | See L<Statistics::Data> for these and other operations on loaded data. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 observed | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | $href = $vnomes->observed(length => INT > 0, circularize => BOOL); # returns keyed distribution; assumes data have already been loaded | ||||||||||||||||||||||||||||
| 92 | @ari = $vnomes->observed(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1], length => INT > 0, circularize => BOOL); # returns frequencies only | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns the frequency distribution for the I<observed> number of v-nomes of the given B<length> in a sequence. These are counted up as overlapping. Called in array context, returns an array of the counts for each possible pattern; otherwise, where these are keyed by the pattern in a hash reference. For descriptives of the observed frequencies, try calling L<Statistics::Lite|Statistics::Lite> methods with this array. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | A value for B<length> greater than zero is required, and must be no more than the sample-size. So for the sequence | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 0 0 0 1 0 0 1 0 1 1 0 | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | there are 12 v-nomes of length 1 (mononomes, the number of elements) from the two (0 and 1) that are possible; 11 dinomes from the four (10, 00, 00, 01) that are possible; and 10 trinomes from eight (100, 000, 001, 010, etc.) that are possible. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | By default, the sequence is counted up for v-nomes as a cyclic sequence, treating the first element of the sequence as following the last one. So the count loops to the beginning of the sequence until all elements from the end are included, and instead of ending the count for trinomes in this sequence at '110', the count includes '101' and '010', increasing the observed sum of trinomes to 12. Set B<circularize> => 0 if the count is I<not> to be made cyclically. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | The data to test can already have been L<load|Statistics::Sequences/load, add, unload>ed, or you send it directly keyed as B<data>. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | In the code for this and/or other methods, following the work of Good (1953, 1957; Good & Gover, 1967), I<v> is used to define variables referring to the length of the subsequence (I<mono>nome, I<di>nome, I<tri>nome, etc.), I<t> defines the number of states (events, letters, etc.) in the sequence, and I<r> identifies the number of each possible subsequence of length I<v>. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 5 | ▶ | sub observed { | ||||||||||||||||||||||||||
|
Subroutine
observed: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 5 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 5 | ▶ | my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 5 | ▶ | my $v = $args->{'length'} or croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | '::observed v-nome length needs to be defined and greater than zero'; | ||||||||||||||||||||||||||||
| 116 | my $circularize = | ||||||||||||||||||||||||||||
| 117 | 5 | ▶ | defined $args->{'circularize'} ? $args->{'circularize'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 5 | my $states_aref = _get_stateslist( $data, $args->{'states'} ); # list unique states as given or from sequence itself | |||||||||||||||||||||||||||
| 119 | 5 | my $v_aref = _get_v_ari($v); | |||||||||||||||||||||||||||
| 120 | 5 | my ( @data_i, %psisq_v ) = (); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 5 | foreach my $v_i (@{$v_aref} ) { # loop through v, v-1 and v-2 lengths ... | |||||||||||||||||||||||||||
| 123 | 5 | @data_i = @{$data}; # append first $v_i - 1 elements to end | |||||||||||||||||||||||||||
| 124 | 5 | ▶ | push( @data_i, @data_i[ 0 .. $v_i - 2 ] ) if $circularize; # e.g., if v = 3, append elements [0] & [1]; if v = 2, append only 1st value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 5 | $psisq_v{$v_i} = _frequencies( \@data_i, $v_i, $states_aref ); # count of each sequence of length $v_i | |||||||||||||||||||||||||||
| 126 | 5 | ▶ | last if $v_i == $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 5 | ▶ | return wantarray ? values %{ $psisq_v{$v} } : $psisq_v{$v}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 4 | *vnomes_observed = \&observed; | |||||||||||||||||||||||||||
| 131 | 4 | *vno = \&observed; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 expected | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | $count = $vnomes->expected(length => INT > 0 > 0, circularize => BOOL); # assumes data have already been loaded | ||||||||||||||||||||||||||||
| 136 | $count = $vnomes->expected(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1], length => INT > 0, circularize => BOOL, states => [0, 1]); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Returns the I<expected> number of observations for each v-nome of the given B<length> in a sequence; i.e., the mean chance expectation, assuming that each event is generated by a random uniform process. Options are as for L<observed|Statistics::Sequences::Vnomes/observed>. This expected frequency is given by: | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =for html <p> <i>E[V]</i> = <i>N</i><i>t</i><sup>–<i>v</i></sup> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | where I<t> is the number of possible states (alternatives; as read from the data or as explicitly given as an array in B<states>), I<v> is the v-nome length, and I<N> is the length of the sequence, less I<v> + 1 if the count is not to be circularized (Good, 1953, Eq. 12). | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Another way to think of this is as the number of mononome observations in the sequence divided by the number of possible permutations of its states for the given length. So, for a sequence made up of 0s and 1s, there are four possible variations of length 2 (00, 10, 01 and 11), so that the expected frequency for each of these variations in a sequence of 20 values is 20 / 4, i.e., 5. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This is a theoretical (deductive) estimate based on the given states - not an empirical (inductive) estimate based on the given data. So all the expected frequencies for each combination are equal, unlike what you might get by way of conditional probabilities for combinations of states from the given data. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | sub expected { | ||||||||||||||||||||||||||
|
Subroutine
expected: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 1 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 1 | my $num = _get_n( $self, $args ); | |||||||||||||||||||||||||||
| 154 | 1 | ▶ | my $v = $args->{'length'} or croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | '::expected v-nome length needs to be defined and greater than zero'; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Sanitize length in proportion to size: | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | "::expected Sequence length v ($v) for testing should be no more than the sample-size ($num) - 2" | ||||||||||||||||||||||||||||
| 160 | if $v >= $num - 1; # or permit $v to extend to limit of circularized data? | ||||||||||||||||||||||||||||
| 161 | #my $lim = Math::Cephes::floor(Math::Cephes::log2($num)) - 2; # re NIST recommmendation that $v is too small for $num and $t if $v >= $lim | ||||||||||||||||||||||||||||
| 162 | my $circularize = | ||||||||||||||||||||||||||||
| 163 | defined $args->{'circularize'} | ||||||||||||||||||||||||||||
| 164 | 1 | ▶ | ? $args->{'circularize'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | : 1; # circularize by default | ||||||||||||||||||||||||||||
| 166 | # Compute expected freq of variations of $t of length $v: | ||||||||||||||||||||||||||||
| 167 | 1 | ▶ | $num = _count_lim( $num, $v ) if !$circularize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | my $p = $self->prob_r( length => $v ); | |||||||||||||||||||||||||||
| 169 | 1 | return $num * $p; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 4 | *vnomes_expected = \&expected; | |||||||||||||||||||||||||||
| 172 | 4 | *vne = \&expected; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 variance | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | $var = $vnomes->variance(length => INT > 0, circularize => BOOL); # assumes data have already been loaded | ||||||||||||||||||||||||||||
| 177 | $var = $vnomes->variance(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1], length => INT > 0, circularize => BOOL, states => [0, 1]); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Returns the variance in the expected frequency of each v-nome, as per Good (1953, Eqs. 11 and 14). | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub variance { | ||||||||||||||||||||||||||
|
Subroutine
variance: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | #croak 'variance() method is not defined for ' . __PACKAGE__; | ||||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | my $num = _get_n( $self, $args ); | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $v = $args->{'length'} or croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | '::variance v-nome length needs to be defined and greater than zero'; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Sanitize length in proportion to size: | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | "::variance Sequence length v ($v) for testing should be no more than the sample-size ($num) - 2" | ||||||||||||||||||||||||||||
| 195 | if $v >= $num - 1; # or permit $v to extend to limit of circularized data? | ||||||||||||||||||||||||||||
| 196 | #my $lim = Math::Cephes::floor(Math::Cephes::log2($num)) - 2; # re NIST recommmendation that $v is too small for $num and $t if $v >= $lim | ||||||||||||||||||||||||||||
| 197 | # Compute expected freq of variations of $t of length $v: | ||||||||||||||||||||||||||||
| 198 | 0 | my $pr = $self->prob_r( length => $v ); | |||||||||||||||||||||||||||
| 199 | 0 | my $var = $pr; | |||||||||||||||||||||||||||
| 200 | 0 | my ( $sum_1, $sum_2, $m, $pm ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 201 | 0 | foreach $m ( 1 .. $v - 1 ) { | |||||||||||||||||||||||||||
| 202 | 0 | $pm = $self->prob_r( length => $m ); | |||||||||||||||||||||||||||
| 203 | 0 | $sum_1 += $pm; | |||||||||||||||||||||||||||
| 204 | 0 | $sum_2 += ( $m + $v - 1 ) * $pm; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 0 | $var += 2 * $pr * $sum_1; | |||||||||||||||||||||||||||
| 207 | 0 | $var -= ( 2 * $v - 1 ) * $pr**2; | |||||||||||||||||||||||||||
| 208 | my $circularize = | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | defined $args->{'circularize'} ? $args->{'circularize'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | $var += | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | $num**-1 * $pr * | ||||||||||||||||||||||||||||
| 212 | ( ( ( $v - 1 ) * ( 3 * $v - 1 ) * $pr ) - ( $v - 1 ) - 2 * $sum_2 ) | ||||||||||||||||||||||||||||
| 213 | if !$circularize; | ||||||||||||||||||||||||||||
| 214 | 0 | return $var; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub obsdev { | ||||||||||||||||||||||||||
|
Subroutine
obsdev: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 0 | croak 'obsdev() method is not defined for ' . __PACKAGE__; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 4 | *observed_deviation = \&obsdev; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 stdev | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $var = $vnomes->stdev(length => INT > 0, circularize => BOOL); # assumes data have already been loaded | ||||||||||||||||||||||||||||
| 225 | $var = $vnomes->stdev(data => [1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1], length => INT > 0, circularize => BOOL, states => [0, 1]); | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Returns the standard deviation in the expected frequency of each v-nome, as per Good (1953, Eqs. 11 and 14). | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | sub stdev { | ||||||||||||||||||||||||||
|
Subroutine
stdev: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 0 | return sqrt( variance(@_) ); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 4 | *standard_deviation = \&stdev; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head2 psisq | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | $psisq = $vnomes->psisq(length => INT > 0, delta => '2|1|0', circularize => '1|0', states => [qw/A C G T/]); | ||||||||||||||||||||||||||||
| 239 | ($psisq, $df, $p_value) = $vnomes->psisq(length => INT > 0, delta => '2|1|0', circularize => '1|0', states => [qw/A C G T/]); | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Performs Good's Generalized Serial Test (by default), of v-nomes on the given or named distribution, yielding a I<psi-square> statistic. Returns the statistic itself, and, if called in array context, also the degrees of freedom. The option B<delta> specifies backward differencing. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =over 4 | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item delta =E<gt> 0 | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | The raw psi-square value for sub-sequences of length I<v> (Kendall-Babington Smith statistic), i.e., without backward differencing. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =for html <p> Ψ²<sub>v</sub> = ∑{<i>r</i>} ( <i>n<sub>r</sub></i>– (<i>N</i> – <i>v</i> + 1) / <i>t<sup>v</sup></i> )² ) / ( (<i>N</i> – <i>v</i> + 1) / <i>t<sup>v</sup></i> )</p> | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | for uncircularized sequences (Good, 1953, Eq. 1), and | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =for html <p> <span style="text-decoration:overline;">Ψ</span>²<sub>v</sub> = ∑{<i>r</i>} ( <i><span style="text-decoration:overline;">n</style><sub>r</sub></i>– <i>Nt<sup>–v</sup></i> ) / <i>Nt<sup>–v</sup></i></p> | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | for circularized sequences (Good, 1953, Eq. 2), where an overline indicates circularization, and where I<N> is the length of the sequence, I<v> is the v-nome length, I<t> is the number of unique states that each element of the sequence can take, and I<r> is the number of variations of length I<v> for the given number of unique states. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | This statistic is only asymptotically distributed as I<chi>-square if I<length> (I<v>) = 1, and is therefore not used as the default. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =item delta =E<gt> 1 | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | The "I<first> backward differences" of psi-square, which is the difference between the psi-square values for sub-sequences of length I<v> and length I<v> - 1. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =for html <p> ΔΨ²<sub>v</sub> = Ψ²<sub>v</sub> – Ψ²<sub>v–1</sub> (<i>v</i> ≥ 1)</p> | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | While it is I<chi>-square distributed, counts of first-differences are not statistically independent (Good, 1953; Good & Gover, 1967), and "the sequence of second differences forms a much better set of statistics for testing the hypothesis of flat-randomness" (Good & Gover, 1967, p. 104). | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item delta =E<gt> 2 (or undef) | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | This method returns by default (without specifying a value for B<delta>), or if B<delta> is not 1 or 0, the "second backward differences" psi-square (I<delta^2-psi^2>). This incorporates psi-square values for backwardly adjacent values of I<length> (I<v>), i.e., for sub-sequences of length I<v>, I<v> - 1, and I<v> - 2. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =for html <p> Δ²Ψ²<sub>v</sub> = Ψ²<sub>v</sub> – 2Ψ²<sub>v–1</sub> – Ψ²<sub>v–2</sub> (<i>v</i> ≥ 2)</p> | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | It is not only asymptotically I<chi>-square distributed, but uses statistically independent counts of all the possible variations of sequences of the given B<length> (Good, 1953). | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =back | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | See also Good's algorithm in individual papers describing application of the Serial Test (e.g., Davis & Akers, 1974). | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 7 | ▶ | sub psisq { | ||||||||||||||||||||||||||
|
Subroutine
psisq: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 282 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 283 | 7 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 7 | ▶ | my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 7 | my $num = _get_n( $self, $args ); | |||||||||||||||||||||||||||
| 286 | 7 | ▶ | my $v = $args->{'length'} or croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | '::psisq v-nome length needs to be defined and greater than zero'; | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Sanitize length in proportion to size: | ||||||||||||||||||||||||||||
| 290 | 7 | ▶ | croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | "::psisq Sequence 'length' ($v) for testing should be no more than the length of the sequence ($num) minus 2" | ||||||||||||||||||||||||||||
| 292 | if $v >= $num - 1; | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | #my $lim = Math::Cephes::floor(Math::Cephes::log2($num)) - 2; # re NIST recommmendation that $v is too small for $num and $t if $v >= $lim | ||||||||||||||||||||||||||||
| 295 | my $circularize = | ||||||||||||||||||||||||||||
| 296 | 7 | ▶ | defined $args->{'circularize'} ? $args->{'circularize'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 7 | ▶ | my $delta = defined $args->{'delta'} ? $args->{'delta'} : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 7 | my $t = _get_t( $self, $args ); | |||||||||||||||||||||||||||
| 299 | 7 | my $states_aref = _get_stateslist( $data, $args->{'states'} ) | |||||||||||||||||||||||||||
| 300 | ; # Get list of unique states as given or from sequence itself | ||||||||||||||||||||||||||||
| 301 | 7 | my $v_aref = _get_v_ari($v); | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # While looping through tests of v, v-1 and v-2 (= $v_i) sequence lengths ... | ||||||||||||||||||||||||||||
| 304 | 7 | my ( $v_i, $r_freq, @data_i, %psisq_v ) = (); | |||||||||||||||||||||||||||
| 305 | 7 | foreach $v_i (@$v_aref) { | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # counting vnome frequencies: handle circularization by firstly appending elements from the start to the sequence's end: | ||||||||||||||||||||||||||||
| 308 | 20 | ▶ | if ($circularize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 12 | @data_i = @{$data}; # copy the original sequence | |||||||||||||||||||||||||||
| 310 | 12 | push( @data_i, @data_i[ 0 .. $v_i - 2 ] ) | |||||||||||||||||||||||||||
| 311 | ; # e.g., if v = 3, append elements [0] & [1]; if v = 2, append only 1st value | ||||||||||||||||||||||||||||
| 312 | 12 | $r_freq = _frequencies( \@data_i, $v_i, $states_aref ) | |||||||||||||||||||||||||||
| 313 | ; # frequencies of each sequence of length $v_i | ||||||||||||||||||||||||||||
| 314 | 12 | $psisq_v{$v_i} = _psisq_circ( scalar @data_i, $t, $v_i, [values %{$r_freq}] ); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | else { | ||||||||||||||||||||||||||||
| 317 | 8 | $r_freq = _frequencies( $data, $v_i, $states_aref ); | |||||||||||||||||||||||||||
| 318 | 8 | $psisq_v{$v_i} = _psisq_uncirc( scalar @$data, $t, $v_i, [values %{$r_freq}] ); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 7 | my ( $psisq, $df ) = _sel_psisq_and_df( $v, $t, \%psisq_v, $delta ); | |||||||||||||||||||||||||||
| 322 | 7 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 3 | return ( $psisq, $df, Math::Cephes::igamc( $df / 2, $psisq / 2 ) ); | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | else { | ||||||||||||||||||||||||||||
| 326 | 4 | return $psisq; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =head2 p_value | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | $p = $vnomes->p_value(length => INT > 0); # using loaded data and default args | ||||||||||||||||||||||||||||
| 333 | $p = $vnomes->p_value(length => INT > 0, data => [1, 0, 1, 1, 0], exact => 1); # using given data (by-passing load and access) | ||||||||||||||||||||||||||||
| 334 | $p = $vnomes->p_value(length => INT > 0, trials => 20, observed => 10); # without using data | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | Returns probability of obtaining the psisq value for data already L<load|Statistics::Sequences/load, add, unload>ed, or directly keyed as B<data>. The I<p>-value is read off the complemented chi-square distribution (incomplete gamma integral) using L<Math::Cephes|Math::Cephes> C<igamc>. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =cut | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 3 | ▶ | sub p_value { | ||||||||||||||||||||||||||
|
Subroutine
p_value: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 341 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 342 | 3 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 3 | my ( $psisq, $df ) = $self->psisq($args); | |||||||||||||||||||||||||||
| 344 | 3 | return Math::Cephes::igamc( $df / 2, $psisq / 2 ); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | 4 | *test = \&p_value; | |||||||||||||||||||||||||||
| 347 | 4 | *vnomes_test = \&p_value; | |||||||||||||||||||||||||||
| 348 | 4 | *vnt = \&p_value; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =head2 dump | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | $vnomes->dump(length => 3, values => {psisq => 1, p_value => 1}, format => 'table|labline|csv', flag => 1, precision_s => 3, precision_p => 7, verbose => 1); | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Print Vnome-test results to STDOUT. See L<dump|Statistics::Sequences/dump> in the Statistics::Sequences manpage for details. If B<verbose> => 1, then you get (1) the actual test-statistic depending on the value of B<delta> tested (I<delta^2-psi^2> for the second difference measure (default), I<delta-psi^2> for the first difference measure, and I<psi2> for the raw measure), followed by degrees-of-freedom in parentheses; and (2) a warning, if relevant, that your B<length> value might be too large with respect to the sample size (see NIST reference, above, in discussing B<length>). If B<text> => 1, you just get the average observed and expected frequencies for each v-nome, the I<Z>-value, and its associated I<p>-value. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 359 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | my $args = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | $args->{'stat'} = 'vnomes'; | |||||||||||||||||||||||||||
| 362 | 0 | ▶ | $args->{'stat'} .= " ($args->{'length'})" if defined $args->{'length'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 0 | $self->SUPER::dump($args); | |||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ( $args->{'verbose'} && $args->{'format'} eq 'table' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"verbose"} and $args->{"format"} eq "table"
MC/DC0%0/2
$args->{"verbose"} and $args->{"format"} eq "table"
$args->{"verbose"}
$args->{"format"} eq "table"
Truth table0%0/3
A and B
A= $args->{"verbose"}B= $args->{"format"} eq "table"
| |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if ( $args->{'values'}->{'psisq'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | my $delta = defined $args->{'delta'} ? $args->{'delta'} : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 0 | my ( $psisq, $df ) = $self->psisq($args); | |||||||||||||||||||||||||||
| 368 | 0 | my $df_str = 'degree'; | |||||||||||||||||||||||||||
| 369 | 0 | ▶ | $df_str .= 's' if $df != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | 0 | my ($statname, $diff_type) = (); | |||||||||||||||||||||||||||
| 371 | 0 | ▶ | if ( $delta == 0 ) { # Raw psisq: | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 372 | 0 | $statname = 'the Kendall-Babington Smith statistic'; | |||||||||||||||||||||||||||
| 373 | 0 | $diff_type = 'without backward differencing'; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | elsif ( $delta == 1 ) { # First backward difference: | ||||||||||||||||||||||||||||
| 376 | 0 | $statname = 'Good\'s delta-psi^2'; | |||||||||||||||||||||||||||
| 377 | 0 | $diff_type = 'with first backward differences'; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | else { | ||||||||||||||||||||||||||||
| 380 | 0 | $statname = 'Good\'s delta^2-psi^2'; | |||||||||||||||||||||||||||
| 381 | 0 | $diff_type = 'with second backward differences'; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | 0 | printf("psisq is %s, calculated with %s, and has %s %s of freedom.\n", $statname, $diff_type, $df, $df_str); | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | 0 | return $self; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head2 nnomes | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | $r = $vnomes->nnomes(length => INT > 0, data => AREF); # supply the data directly, and assume all possible states are in the given sequence | ||||||||||||||||||||||||||||
| 393 | $r = $vnomes->nnomes(length => INT > 0); # assuming data have been "loaded" and all possible states are in the loaded sequence | ||||||||||||||||||||||||||||
| 394 | $r = $vnomes->nnomes(length => INT > 0, states => AREF); # ... or specify the states in case not all in the sequence | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Returns the number of possible subsequences of the given length (I<v>) for the given number of states (I<t>). This is the quantity denoted as I<r> in Good's (1953, 1957) papers; i.e., | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =for html <p> <i>r(v)</i> = <i>t</i><sup><i>v</i></sup> | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | The method needs to have, of course, a sequence a test. This can be directly given to the method (as the referenced array for the named argument B<data>) or it can be pre-loaded, as by using $vnomes->load(AREF). | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | Then, the method needs to know two things: the "v" value itself, i.e., the length of the possible subsequences to test (I<mono>nomes, I<di>nomes, I<tri>nomes, etc.), and the number of states (events, letters, etc.) that the process generating the data could take (from 1 to whatever). The "v" value is I<always> required to be specified by the named argument B<length>, and it should be a positive integer value no greater than the length of the (loaded/given) sequence. The states can be directly given in the named argument B<states> (recommended), or from the states that "empirically" exist in the loaded/given B<data>. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =cut | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | sub nnomes { | ||||||||||||||||||||||||||
|
Subroutine
nnomes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 407 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 408 | 0 | ▶ | my $args = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | 0 | my $t = _get_t( $self, $args ); | |||||||||||||||||||||||||||
| 410 | 0 | ▶ | my $v = $args->{'length'} or croak __PACKAGE__, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | '::nnomes v-nome length needs to be defined and greater than zero'; | ||||||||||||||||||||||||||||
| 412 | 0 | return $t**$v; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head2 prob_r | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | $Pr = $vnomes->prob_r(length => $v); # length is 1 (mononomes) by default | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Returns the probability of the occurrence of any of the individual elements ("digits") in the sequence (I<v> = 1), or of the given B<length>, assuming they are equally likely and independent. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =for html <p> <i>P<sub>r</sub></i> = <i>t</i><sup><i>–v</i></sup> | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =cut | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 1 | ▶ | sub prob_r { | ||||||||||||||||||||||||||
|
Subroutine
prob_r: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 426 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 427 | 1 | ▶ | my $args = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | 1 | my $t = _get_t( $self, $args ); | |||||||||||||||||||||||||||
| 429 | 1 | ▶ | my $v = $args->{'length'} || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'length'} || 1
MC/DC0%0/1
$args->{'length'} || 1
$args->{'length'}
Truth table50%1/2
A
A= $args->{'length'}
| |||||||||||||||||||||||||||||
| 430 | 1 | return $t**( -1 * $v ); | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Options common to the above stats methods. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head2 length | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | This is currently a I<required> "option", giving the length of the I<v>-nome of interest, i.e., the value of I<v> - an integer greater than or equal to 1, and smaller than than the sample-size. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | What is a meaningful maximal value of B<length>? As a I<chi>-square test, it is conventionally required that the expected frequency is at least 5 for each v-nome (Knuth, 1988). This can be judged to be too conservative (Delucchi, 1993). The NIST documentation on the serial test (Rukhin et al., 2010) recommends that length should be less than the floored value of log2 of the sample-size, minus 2. No tests are here made of these recommendations. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =head2 circularize | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | By default, L<observed|Statistics::Sequences::Vnomes/observed, vnomes_observed, vno> and L<expected|Statistics::Sequences::Vnomes/expected, vnomes_expected, vne> counts, and the value of L<psisq|Statistics::Sequences::Vnomes/psisq>, are made by treating the sequence as a cyclic one, where the first element of the sequence follows the last one. This affects (and simplifies) the calculation of the expected frequency of each v-nome, and so the value of each psi-square. Also, circularizing ensures that the expected frequencies are accurate; otherwise, they might only be approximate. As Good and Gover (1967) state, "It is convenient to circularize in order to get exact checks of the arithmetic and also in order to simplify some of the theoretical formulae" (p. 103). These methods, however, can also treat the sequence non-cyclically by calling them with B<circularize> => 0. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head2 states | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Optionally send a referenced array listing the unique states (or 'events', 'letters') in the population from which the sequence was sampled, e.g., B<states> => [qw/A C G T/]. This is useful if the sequence itself might not include all the possible states. If this is not specified, the states are identified from the sequence itself. If giving a list of states, a check in each test is made to ensure that the sequence contains I<only> those elements in the list. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =cut | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | # PRIVATMETHODEN | ||||||||||||||||||||||||||||
| 454 | sub _count_lim | ||||||||||||||||||||||||||||
| 455 | 20375 | ▶ | { # N - v + 1; if N = 5 and v = 3, starting the count-up for v-nomes must stop from the first 3 elements of the data | ||||||||||||||||||||||||||
|
Subroutine
_count_lim: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 456 | 20375 | return $_[0] - $_[1] + 1; | |||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 8 | ▶ | sub _get_n { # the size of the sequence | ||||||||||||||||||||||||||
|
Subroutine
_get_n: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 460 | 8 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 461 | 8 | ▶ | my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 462 | 8 | return scalar @$data; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | sub _get_t | ||||||||||||||||||||||||||||
| 466 | 8 | ▶ | { # the number of unique states; e.g., 2 for a binary sequences; 10 for a typical random digit sequence (0 .. 9) | ||||||||||||||||||||||||||
|
Subroutine
_get_t: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 467 | 8 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 468 | 8 | my $t; | |||||||||||||||||||||||||||
| 469 | 8 | ▶ | if ( ref $args->{'states'} and ref $args->{'states'} eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $args->{"states"} and ref $args->{"states"} eq "ARRAY"
MC/DC50%1/2
ref $args->{"states"} and ref $args->{"states"} eq "ARRAY"
ref $args->{"states"}
ref $args->{"states"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $args->{"states"}B= ref $args->{"states"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 470 | 3 | $t = scalar( @{ $args->{'states'} } ); | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | else { | ||||||||||||||||||||||||||||
| 473 | 5 | ▶ | my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 474 | 5 | my %hash = map { $_, 1 } @{$data}; | |||||||||||||||||||||||||||
| 475 | 5 | $t = scalar keys %hash; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | 8 | return $t; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 12 | ▶ | sub _get_stateslist { | ||||||||||||||||||||||||||
|
Subroutine
_get_stateslist: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 481 | 12 | my ( $data, $states ) = @_; | |||||||||||||||||||||||||||
| 482 | 12 | ▶ | if ( !ref $states ) { # Get states from the data themselves: | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 483 | 8 | my %hash = map { $_, 1 } @{$data}; | |||||||||||||||||||||||||||
| 484 | 8 | $states = [ keys %hash ]; | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | else { # Ensure that the data only contain states in the given list: | ||||||||||||||||||||||||||||
| 487 | DATA: | ||||||||||||||||||||||||||||
| 488 | 4 | foreach my $g ( @{$data} ) { | |||||||||||||||||||||||||||
| 489 | 260 | foreach my $h ( @{$states} ) { | |||||||||||||||||||||||||||
| 490 | 638 | ▶ | next DATA if $h eq $g; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | 0 | croak __PACKAGE__, | |||||||||||||||||||||||||||
| 493 | " The element $g in the data is not represented in the given states"; | ||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | #croak __PACKAGE__, ' At least two different values must be in the sequence to test its sub-sequences' if $t <= 1; | ||||||||||||||||||||||||||||
| 498 | 12 | return $states; | |||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 25 | ▶ | sub _frequencies { | ||||||||||||||||||||||||||
|
Subroutine
_frequencies: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 502 | 25 | my ( $data_i, $v_i, $states_aref ) = @_ | |||||||||||||||||||||||||||
| 503 | ; # list all possible combinations of states at the current length ($v_i): | ||||||||||||||||||||||||||||
| 504 | # Count up the frequency of each variation in the data: | ||||||||||||||||||||||||||||
| 505 | 25 | my $num = scalar( @{$data_i} ); | |||||||||||||||||||||||||||
| 506 | 25 | my ( $i, $probe_str, $test_str, %r_freq ) = (); | |||||||||||||||||||||||||||
| 507 | 25 | foreach ( variations_with_repetition( $states_aref, $v_i ) ) { | |||||||||||||||||||||||||||
| 508 | 277 | $probe_str = join '', @{$_}; | |||||||||||||||||||||||||||
| 509 | 277 | $r_freq{$probe_str} = 0; | |||||||||||||||||||||||||||
| 510 | 277 | ▶ | for ( $i = 0 ; $i < _count_lim( $num, $v_i ) ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | 20098 | $test_str = join '', @{$data_i}[ $i .. ( $i + $v_i - 1 ) ]; | |||||||||||||||||||||||||||
| 512 | 20098 | ▶ | $r_freq{$probe_str}++ if $probe_str eq $test_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | } # print "FREQ:\n"; while (my($key, $val) = each %freq) { print "\t$key = $val\n"; } print "\n"; | ||||||||||||||||||||||||||||
| 515 | 25 | return \%r_freq; | |||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 7 | ▶ | sub _sel_psisq_and_df { # get psisq and its df | ||||||||||||||||||||||||||
|
Subroutine
_sel_psisq_and_df: called CC=7 SCAR=23.2
| |||||||||||||||||||||||||||||
| 519 | 7 | my ( $v, $t, $psisq_v, $delta ) = @_; | |||||||||||||||||||||||||||
| 520 | 7 | my ( $psisq, $df ) = (); | |||||||||||||||||||||||||||
| 521 | 7 | ▶ | if ( $v == 1 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | { # psisq is asymptotically distributed chisq, can use psisq for chisq distribution: | ||||||||||||||||||||||||||||
| 523 | 0 | $psisq = $psisq_v->{1}; | |||||||||||||||||||||||||||
| 524 | 0 | $df = $t - 1; | |||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | else { | ||||||||||||||||||||||||||||
| 527 | 7 | ▶ | if ( $delta == 0 ) { # Raw psisq: | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 528 | 1 | $psisq = $psisq_v->{$v}; | |||||||||||||||||||||||||||
| 529 | 1 | $df = $t**$v - 1; # Good (1957, Eq. 6) - if circularized or not | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | elsif ( $delta == 1 ) { # First backward difference: | ||||||||||||||||||||||||||||
| 532 | $psisq = | ||||||||||||||||||||||||||||
| 533 | 0 | ▶ | $psisq_v->{$v} - ( $v - 1 <= 0 ? 0 : $psisq_v->{ $v - 1 } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 534 | 0 | $df = $t**$v - $t**( $v - 1 ); | |||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||
| 536 | else { # $delta == 2 # Second backward difference (default): | ||||||||||||||||||||||||||||
| 537 | $psisq = | ||||||||||||||||||||||||||||
| 538 | $psisq_v->{$v} - | ||||||||||||||||||||||||||||
| 539 | ( 2 * ( $v - 1 <= 0 ? 0 : $psisq_v->{ $v - 1 } ) ) + | ||||||||||||||||||||||||||||
| 540 | 6 | ▶ | ( $v - 2 <= 0 ? 0 : $psisq_v->{ $v - 2 } ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 541 | 6 | $df = ( $t**( $v - 2 ) ) * ( $t - 1 )**2; | |||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | 7 | return ( $psisq, $df ); | |||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 12 | ▶ | sub _get_v_ari { | ||||||||||||||||||||||||||
|
Subroutine
_get_v_ari: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 548 | 12 | my $v = shift | |||||||||||||||||||||||||||
| 549 | ; # Init a hash to keep the psi-square values for the v, v-1, and v-2 ( = $v_i) sequence lengths, where relevant: | ||||||||||||||||||||||||||||
| 550 | 12 | my @ari = (); | |||||||||||||||||||||||||||
| 551 | 12 | foreach ( 0 .. 2 ) { | |||||||||||||||||||||||||||
| 552 | 36 | ▶ | $v > $_ ? push @ari, $v - $_ : last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | } # print "v ari = ", join(' ', @ari), "\n"; #push @ari, $v - 1 if $v >= 2; #push @ari, $v - 2 if $v >= 3; | ||||||||||||||||||||||||||||
| 554 | 12 | return \@ari; | |||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | # Compute psi^2 for uncircularized sequence from freq of each variation of length $v for given states (Good, 1953, Eq. 1): | ||||||||||||||||||||||||||||
| 558 | 8 | ▶ | sub _psisq_uncirc { | ||||||||||||||||||||||||||
|
Subroutine
_psisq_uncirc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 559 | 8 | my ( $n, $t, $v, $obs ) = @_; | |||||||||||||||||||||||||||
| 560 | 8 | my $exp = ( $n - $v + 1 ) / $t**$v; | |||||||||||||||||||||||||||
| 561 | 8 | return _sum_sq($obs, $exp) / $exp; | |||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | # Compute psi^2 for circularized sequence from freq of each variation of length $v for given states | ||||||||||||||||||||||||||||
| 565 | # - Good (1953, Eq. 2) is in error by not squaring the (obs - exp) difference in the summation: | ||||||||||||||||||||||||||||
| 566 | 12 | ▶ | sub _psisq_circ { | ||||||||||||||||||||||||||
|
Subroutine
_psisq_circ: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 567 | 12 | my ( $n, $t, $v, $obs ) = @_; | |||||||||||||||||||||||||||
| 568 | 12 | my $exp = $n * $t**( -1 * $v ); | |||||||||||||||||||||||||||
| 569 | 12 | return _sum_sq($obs, $exp) / $exp; # same as ($t**$v * $sum )/ $n; | |||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 20 | ▶ | sub _sum_sq { | ||||||||||||||||||||||||||
|
Subroutine
_sum_sq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 573 | 20 | my ($obs, $exp) = @_; | |||||||||||||||||||||||||||
| 574 | 20 | return sum( map { ( $_ - $exp )**2 } @{$obs} ); # Good & Gover (1967, p. 103) | |||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | __END__ | ||||||||||||||||||||||||||||