#!/usr/bin/perl use strict; use warnings; use Finance::YahooQuote; my @symbols = ("IBM","DELL","GOOG","YHOO","MSFT","ORCL","SAP","COGN", "BOBJ"); my @columns = ("Last Trade (Price Only)","Last Trade Date","Last Trade Time","Day's Range","52-week Range","EPS Est. Next Year","P/E Ratio","PEG Ratio","Dividend Yield"); my $arrptr = getcustomquote(\@symbols, \@columns); my $i = 0; foreach my $symbol (@symbols){ my @quotes = @{$arrptr->[$i++]}; print "$symbol\t@quotes\n"; } #### use strict; use warnings; use PDL; use PDL::NiceSlice; # ================================ # fcm # ( $performance_index, $prototypes, $current_partition_matrix) = # fcm( $patterns, $partition_matrix, $fuzzification_factor, # $tolerance, $max_iter ) # ================================ sub fcm { # # fuzzy c means implementation # my ( $patterns, $current_partition_matrix, $fuzzification_factor, $tolerance, $max_iter ) = @_; my ( $number_of_patterns, $number_of_clusters ) = $current_partition_matrix->dims(); my ( $prototypes, $performance_index ); my $iter = 0; while (1) { # computing each prototype my $temporal_partition_matrix = $current_partition_matrix ** $fuzzification_factor; my $temp_prototypes = ($temporal_partition_matrix x $patterns)->xchg(1,0) / sumover($temporal_partition_matrix); $prototypes = $temp_prototypes->xchg(1,0); # copying partition matrix my $previous_partition_matrix = $current_partition_matrix->copy; # updating the partition matrix my $dist = zeroes($number_of_patterns, $number_of_clusters); for my $j (0..$number_of_clusters - 1){ my $diff = $patterns - $prototypes(:,$j)->dummy(1, $number_of_patterns); $dist(:,$j) .= (sumover( $diff ** 2 )) ** 0.5; } my $temp_variable = $dist ** (-2/($fuzzification_factor - 1)); $current_partition_matrix = $temp_variable / sumover($temp_variable->xchg(1,0)); # # Performance Index calculation # $temporal_partition_matrix = $current_partition_matrix ** $fuzzification_factor; $performance_index = sum($temporal_partition_matrix * ( $dist ** 2 )); # checking stop conditions my $diff_partition_matrix = $current_partition_matrix - $previous_partition_matrix; $iter++; if ( ($diff_partition_matrix->max < $tolerance) || ($iter > $max_iter) ) { last; } print "iter = $iter\n"; } return ( $performance_index, $prototypes, $current_partition_matrix ); }