| 1 | package Statistics::RatioVariance; | ||
| 2 | |||
| 3 | 0 | ▶ | use Statistics::Basic qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use Statistics::Zed; | ||
| 5 | |||
| 6 | our $VERSION = '1.00'; | ||
| 7 | |||
| 8 | sub calc{ | ||
| 9 | |||
| 10 | # This variables must be references to arrays. | ||
| 11 | my($self, $xvector, $yvector, $xvariance, $yvariance) = @_; | ||
| 12 | |||
| 13 | # Calculates ยท | ||
| 14 | my $kxvector = 0; | ||
| 15 | my $kyvector = 0; | ||
| 16 | my $kxvariance = 0; | ||
| 17 | my $kyvariance = 0; | ||
| 18 | |||
| 19 | #my $counter = 0; | ||
| 20 | #print "X-Vector lenght: ".$#{$xvector}." - ".${$xvector}[0]."\n"; | ||
| 21 | #print "Y-Vector lenght: ".$#{$yvector}."\n"; | ||
| 22 | #print "X-Var lenght: ".$#{$xvariance}."\n"; | ||
| 23 | #print "Y-Var lenght: ".$#{$yvariance}."\n"; | ||
| 24 | |||
| 25 | for(my $i=0;$i<$#{$xvector};$i++){ | ||
| 26 | |||
| 27 | $kxvector = $kxvector + ${$xvector}[$i] if ${$xvector}[$i] ; | ||
| 28 | $kyvector = $kyvector + ${$yvector}[$i] if ${$yvector}[$i] ; | ||
| 29 | $kxvariance = $kxvariance + ${$xvariance}[$i] if ${$xvariance}[$i] ; | ||
| 30 | $kyvariance = $kyvariance + ${$yvariance}[$i] if ${$yvariance}[$i] ; | ||
| 31 | |||
| 32 | #print "$counter:kxvector: $kxvector\n"; | ||
| 33 | #print "$counter:kyvector: $kyvector\n"; | ||
| 34 | #print "$counter:kxvariance: $kxvariance\n"; | ||
| 35 | #print "$counter:kyvariance: $kyvariance\n"; | ||
| 36 | |||
| 37 | #$counter++; | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | # Calculates ^x and ^y (means) | ||
| 42 | my $xmean = $kxvector; | ||
| 43 | my $ymean = $kyvector; | ||
| 44 | |||
| 45 | # Calculates correlation coefficent | ||
| 46 | my $correlation = correlation($xvector, $yvector); | ||
| 47 | |||
| 48 | # Calculates the cocient | ||
| 49 | my $cocient = $xmean/$ymean; | ||
| 50 | |||
| 51 | # Calculates the variance (STEP-BY-STEP) | ||
| 52 | my $first_term = 1/($ymean*$ymean); | ||
| 53 | |||
| 54 | my $second_term_first_term = $kxvariance; | ||
| 55 | my $second_term_second_term = ($cocient*$cocient)*$kyvariance; | ||
| 56 | my $second_term_third_term = 2*$cocient*$correlation*(sqrt($kxvariance))*(sqrt($kyvariance)); | ||
| 57 | my $second_term = $second_term_first_term + $second_term_second_term - $second_term_third_term; | ||
| 58 | |||
| 59 | my $cocient_variance = $first_term*$second_term; | ||
| 60 | |||
| 61 | # Wanna debug? | ||
| 62 | #print "Correlation: $correlation\n"; | ||
| 63 | #print "First term: 1/$ymean $first_term\n"; | ||
| 64 | #print "Second term - First term: $second_term_first_term\n"; | ||
| 65 | #print "Second term - Second term: $second_term_second_term\n"; | ||
| 66 | #print "Second term - Third term: $second_term_third_term\n"; | ||
| 67 | #print "Second term: $second_term\n"; | ||
| 68 | #print "Cocient variance: $cocient_variance\n"; | ||
| 69 | |||
| 70 | # ..Fiu! | ||
| 71 | |||
| 72 | # It seems a zero-value for variance is not allowed in function $zed->score | ||
| 73 | #$cocient_variance == 0 ? $cocient_variance = 0.000001 : $cocient_variance; | ||
| 74 | |||
| 75 | # Creates the ztest obj with default values (see perldoc Statistics::Zed to settings this values) | ||
| 76 | my $zed = Statistics::Zed->new(); | ||
| 77 | my ($z_value, $p_value, $observed_deviation, $standar_deviation) = $zed->score(observed => $cocient, expected => 1, variance => $cocient_variance); | ||
| 78 | |||
| 79 | # Create a hash for the returning value with all the calculated params | ||
| 80 | my %hash = (); | ||
| 81 | |||
| 82 | $hash{xsum} = $kxvector; | ||
| 83 | $hash{ysum} = $kyvector; | ||
| 84 | $hash{x_var_sum} = $kxvariance; | ||
| 85 | $hash{y_var_sum} = $kyvariance; | ||
| 86 | $hash{cocient} = $cocient; | ||
| 87 | $hash{correlation} = $correlation; | ||
| 88 | $hash{cocient_variance} = $cocient_variance; | ||
| 89 | $hash{z_value} = $z_value; | ||
| 90 | $hash{p_value} = $p_value; | ||
| 91 | $hash{observed_deviation} = $observed_deviation; | ||
| 92 | $hash{standar_deviation} = $standar_deviation; | ||
| 93 | |||
| 94 | |||
| 95 | return %hash; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||