lib/AI/MicroStructure/Fitnes.pm untested

filter: Statementstmt 0.0% 0 / 168 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 197 CC 2 SCAR 8.8
CC
2
Cov
0%
connectStats
6.0
import
2.0
new
2.0
CRAP 2.4
1 #!/usr/bin/perl
2 package AI::MicroStructure::Fitnes;
3 use strict;
4 use warnings;
5 use JSON;
6 use Data::Printer;
7 use Statistics::MVA::HotellingTwoSample;
8 use Algorithm::BaumWelch;
9 use Statistics::Distributions::Ancova;
10 use Statistics::MVA::BayesianDiscrimination;
11 use Statistics::MVA::HotellingTwoSample;
12 use Statistics::Contingency;
13  
14  
15  
16  
17 use vars qw(
18 $configure
19 $pi
20 $driver
21 );
22  
23  
24  
25  
26 sub import {
27 ##$ex->{log} .= sprintf "import called with @_\n";
28 local $configure = @_; # shame that Getopt::Long isn't structured better!
29 use Getopt::Long ();
30  
31  
32
33  
34  
35 Getopt::Long::GetOptions( '-',
36 'driver=f'=>\$driver,
37 'configure=f'=>\$configure,
38 ) or croak("bad import arguments");
39  
40
41 } # end subroutine import definition
42  
43  
44 sub new {
45
46 my($class,$args) = @_;
47
48 my $self = bless { cache => [] }, $class;
49 $configure = $args;
50 $self->{"log"}="";
51 $class->import;
52 $class->connectStats;
53  
54
55 return $self;
56 }
57  
58  
59  
60  
61 sub initialize {
62 my $self = shift();
63 %$self= @_;
64  
65 }
66  
67  
68 sub defaultDriver {
69  
70 my $self = shift;
71  
72 return $configure;
73
74 }
75  
76  
77
78 sub connectStats{
79  
80  
81 my $self = shift;
82  
83  
84  
85 if( scalar keys %$configure < 8){
86  
87
88 }else{
89
90 $self->{configure} = $configure;
91 }
92
93 END{
94
95 my $anc = Statistics::Distributions::Ancova->new ( { significance => 0.005, input_verbosity => 1, output_verbosity => 1 } );
96 use Statistics::Descriptive;
97 my $stat = Statistics::Descriptive::Full->new();
98 $Statistics::Descriptive::Tolerance = 1e-10;
99  
100 $stat->add_data(1,2,3,40);
101 my $mean = $stat->mean();
102 my $var = $stat->variance();
103 my $tm = $stat->trimmed_mean(0.25000);
104
105 # The observation series see http://www.cs.jhu.edu/~jason/.
106 my $obs_series = [qw/ obs2 obs3 obs3 obs2 obs3 obs2 obs3 obs2 obs2
107 obs3 obs1 obs3 obs3 obs1 obs1 obs1 obs2 obs1
108 obs1 obs1 obs3 obs1 obs2 obs1 obs1 obs1 obs2
109 obs3 obs3 obs2 obs3 obs2 obs2
110 /];
111  
112 # The emission matrix - each nested array corresponds to the probabilities of a single observation type.
113 my $emis = {
114 obs1 => [0.3, 0.3],
115 obs2 => [0.3, 0.4],
116 obs3 => [0.4, 0.3],
117 };
118  
119 # The transition matrixi - each row and column correspond to a particular state e.g. P(state1_x|state1_x-1) = 0.9...
120 my $trans = [
121 [0.9, 0.1],
122 [0.1, 0.9],
123 ];
124  
125 # The probabilities of each state at the start of the series.
126 my $start = [0.5, 0.5];
127  
128 # Create an Algorithm::BaumWelch object.
129 my $ba = Algorithm::BaumWelch->new;
130  
131 # Feed in the observation series.
132 $ba->feed_obs($obs_series);
133  
134 # Feed in the transition and emission matrices and the starting probabilities.
135 $ba->feed_values($trans, $emis, $start);
136  
137 # Alternatively you can randomly initialise the values - pass it the number of hidden states -
138 # i.e. to determine the parameters we need to make a first guess).
139 # $ba->random_initialise(2);
140  
141 # Perform the algorithm.
142 $ba->baum_welch;
143  
144 # Use results to pass data.
145 # In VOID-context prints formated results to STDOUT.
146 # In LIST-context returns references to the predicted transition & emission matrices and the starting parameters.
147 $ba->results;
148  
149  
150  
151  
152  
153 # we have two groups of data each with 4 variables and 9 observations.
154 my $data_x = [
155 [qw/ 292 222 52 57/],
156 [qw/ 100 227 51 45/],
157 [qw/ 272 218 49 36/],
158 [qw/ 101 221 17 47/],
159 [qw/ 181 208 12 35/],
160 [qw/ 111 118 51 54/],
161 [qw/ 288 321 51 49/],
162 [qw/ 286 219 52 45/],
163 [qw/ 262 225 47 44/],
164 ];
165 my $data_y = [
166 [qw/ 286 107 29 62/],
167 [qw/ 311 122 29 63/],
168 [qw/ 272 131 52 86/],
169 [qw/ 182 88 23 69/],
170 [qw/ 211 118 61 57/],
171 [qw/ 323 127 51 79/],
172 [qw/ 385 332 70 63/],
173 [qw/ 373 127 85 60/],
174 [qw/ 408 95 57 71/],
175 ];
176
177 # Create a Statistics::MVA::HotellingTwoSample object and pass the data as two Lists-of-Lists within an anonymous array.
178 my $mva = Statistics::MVA::HotellingTwoSample->new([ $data_x, $data_y ]);
179  
180  
181  
182  
183 my @all_categories = 0..3;
184 my $s = new Statistics::Contingency(categories => \@all_categories);
185
186 foreach(@all_categories) {
187 my $assigned_categories = $_;
188 my $correct_categories=sprintf "%d" ,$_+rand 10;
189 $s->add_result($assigned_categories, $correct_categories);
190 }
191
192  
193 my $ex = AI::MicroStructure::Fitnes->new;
194
195 $ex->{log} .= $s->stats_table; # Show several stats in table form
196  
197 my $stats = $s->category_stats;
198 my $show = {};
199
200 while (my ($cat, $value) = each %$stats) {
201 $show->{$cat} = $value;
202 }
203
204
205  
206  
207  
208 while (my ($cat, $value) = each %$stats) {
209 $ex->{log} .= sprintf "Category '$cat': \n"; $ex->{log} .= sprintf " Accuracy: $value->{accuracy}\n";
210 $ex->{log} .= sprintf " Precision: $value->{precision}\n";
211 $ex->{log} .= sprintf " F1: $value->{F1}\n";
212 }
213  
214  
215  
216  
217  
218  
219  
220 my $data_X = [
221 [qw/ 191 131 53/],
222 [qw/ 185 134 50/],
223 [qw/ 200 137 52/],
224 [qw/ 173 127 50/],
225 [qw/ 171 128 49/],
226 [qw/ 160 118 47/],
227 [qw/ 188 134 54/],
228 [qw/ 186 129 51/],
229 [qw/ 174 131 52/],
230 [qw/ 163 115 47/],
231 ];
232  
233 my $data_Y = [
234 [qw/ 186 107 49/],
235 [qw/ 211 122 49/],
236 [qw/ 201 144 47/],
237 [qw/ 242 131 54/],
238 [qw/ 184 108 43/],
239 [qw/ 211 118 51/],
240 [qw/ 217 122 49/],
241 [qw/ 223 127 51/],
242 [qw/ 208 125 50/],
243 [qw/ 199 124 46/],
244 ];
245  
246  
247 # Pass the data as a list of the two LISTS-of-LISTS above (termed X and Y). The module by default assumes equal prior probabilities.
248 my $bld = Statistics::MVA::BayesianDiscrimination->new($data_X,$data_Y);
249 $bld->output;
250  
251 # Pass the data but telling the module to calculate the prior probabilities as the ratio of observations for the two groups (e.g. P(X) X_obs_num / Total_obs.
252 $bld = Statistics::MVA::BayesianDiscrimination->new({priors => 1 },$data_X,$data_Y);
253 $bld->output;
254  
255 # Create an Ancova object and set significance value of p = 0.05 for statistical test. See METHODS for optional named arguments and default values.
256 $anc = Statistics::Distributions::Ancova->new ( { significance => 0.005, input_verbosity => 1, output_verbosity => 1 } );
257  
258 # Example using k=3 groups. Data includes our dependent variable of interest (Y) and covariant data (X) that is used to eliminate obscuring effects of covariance.
259 my @Drug_A_Y = ('29','27','31','33','32','24','16');
260 my @Drug_A_X = ('53','64','55','67','55','45','35');
261 my @Drug_B_Y = ('39','34','20','35','57','28','32','17');
262 my @Drug_B_X = ('24','19','13','18','25','16','16','13');
263 my @Drug_C_Y = ('12','21','26','17','25','9','12');
264 my @Drug_C_X = ('5','12','12','9','12','3','3');
265  
266 # Data is sent to object as nested HASH reference. Individual group names are option, but to distinguish IV/DV, the names Y and X for the variables are compulsory.
267 my $h_ref = { 'group_A' => {
268 Y => \@Drug_A_Y,
269 X => \@Drug_A_X,
270 },
271 'group_B' => {
272 Y => \@Drug_B_Y,
273 X => \@Drug_B_X,
274 },
275 'group_C' => {
276 Y => \@Drug_C_Y,
277 X => \@Drug_C_X,
278 },
279 };
280  
281 # Feed the object the data pass data HASH reference with named argument 'data'.
282 $anc->load_data ( { data => $h_ref } );
283  
284 # Perform analysis
285 $anc->ancova_analysis;
286  
287 # To access results use results method. The return of this method is context dependent (see METHODS).
288 # To$ex->{log} .= sprintf a report to STDOUT call results in VOID context.
289 $anc->results();
290  
291 p $mva;
292 }
293  
294 }
295  
296 1;