calculating variance in perl programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating variance in perl programming
# 1  
Old 01-12-2008
Java calculating variance in perl programming

#!/usr/bin/perl -w
use strict;


open(FH,"$ARGV[0]") or die;
my @temp=<FH>;
close FH;
my $mean = Mean(\@temp);
my $var = variance(\@temp);
print "$var\n";
sub estimate_variance {
my ($arrayref) = @_;
my ($mean,$result) = (mean($arrayref),0);
foreach (@$arrayref) { $result += ($_ - $mean)**2 }
return $result / $#{$arrayref};
}
error shown:
Undefined subroutine &main::variance called at variance_try1.pl line 9.

wht can be possible solution??

Last edited by cdfd123; 01-12-2008 at 10:47 AM.. Reason: correcting string
# 2  
Old 01-12-2008
variance () is not a Perl function. You will need to write your own or find a module that can do it.
# 3  
Old 01-12-2008
I though you were trying to do this in C Smilie
# 4  
Old 01-12-2008
It might be easier if you used a standard proven statistics module of which there are a number on CPAN.

For example, I have modified your code to work with the CPAN Module Statistics/Descriptive

Code:
#!/usr/local/bin/perl -w

use strict;
use Statistics::Descriptive;

open(FH, "$ARGV[0]") or die "No file specified\n";
my @temp=<FH>;
close FH;

my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(\@temp);
my $mean = $stat->mean();
my $variance  = $stat->variance();
my $num  = $stat->count();

print "Number of Values = $num\n",
      "Mean             = $mean\n",
      "Variance         = $variance\n";

# 5  
Old 01-15-2008
Quote:
Originally Posted by shamrock
I though you were trying to do this in C Smilie
Dear Shamrock
I have tried both using c and perl ..
So got results both in C and perl..
Thanks for curiosity
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare sum of two columns if variance is zero do nothing else send an email

11☺Hi, I have to data sets: One is in .txt format and other is in .csv format, please refer below two outputs from two files. File1.txt SOURCE PAYDATE TOTAL_DOLLARS RECORD_COUNT ASSET 05/25/2018 247643.94 ASSET 06/20/2018 ... (27 Replies)
Discussion started by: Tahir_M
27 Replies

2. Shell Programming and Scripting

AWK sample variance

I would like to calculate 1/n In awk, I wrote the following line for the sigma summation: { summ+=($1-average)^2 } Full code: BEGIN { Print "This script calculate error estimates"; sum=0 } { sum+=$1; n++ } END { average = sum/n } BEGIN { summ=0 } { summ+=($1-average)^2 } END { print... (8 Replies)
Discussion started by: chrisjorg
8 Replies

3. Shell Programming and Scripting

Calculating Running Variance Using Awk

Hi all, I am attempting to calculate a running variance for a file containing a column of numbers. I am using the formula variance=sum((x-mean(x))^2)/(n-1), where x is the value on the current row, and mean(x) is the average of all of the values up until that row. n represents the total number... (1 Reply)
Discussion started by: Jahn
1 Replies

4. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. Shell Programming and Scripting

perl programming

how to link the linux files in perl on the local webpage ???? suppose we have some results and want to get them published on the local webpage of our internal site. how this can be done using HTML and perl together , so that the results are published directly on the webpage. thanks kullu (0 Replies)
Discussion started by: kullu
0 Replies

6. Programming

Help me with perl programming

Hi, i am very beginer to perl, I am reading one xml file and i am creating hash table for that file. i written code like this #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; #Reading XML with a pull parser my $file; open( $file, 'formal.xml'); my $reader =... (8 Replies)
Discussion started by: veerubiji
8 Replies

7. Programming

Programming help - Perl !

I am having a text file with Vivek 50 Ram 34 Hulk 45 Vivek 23 Ram 23 Vivek 55 Now I need a perl script to display the fields of 1st column & the 2nd column with summation (& avoid the duplicates). Vivek 128 Ram 57 hulk 45 Plz help me... (1 Reply)
Discussion started by: gameboy87
1 Replies

8. Shell Programming and Scripting

Awk total and variance

File1 0358 Not Visible ***:* NA:NA RDF1+TDEV Grp'd (M) RW 102413 0359 Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035A Not Visible ***:* NA:NA RDF1+TDEV N/Grp'd (m) RW - 035B Not Visible ***:* NA:NA ... (2 Replies)
Discussion started by: greycells
2 Replies

9. Programming

C language to calculate mean,variance

Here I want to calculate mean,variance and sum from a file 1.1*2*4*22*211*22*12*22*22*11 2.2*2*22*12*22*11*11*122*33*22 3.9*7*22*88*87*98*67*66*56*66*11 As this is a large file and i am trying to write in c where formulae of MEAN = 1/N (X1...+..Xn) Variance = square root of 1/N-1... (9 Replies)
Discussion started by: cdfd123
9 Replies

10. UNIX for Dummies Questions & Answers

PERL - DB programming

Hi friends, What are the possible ways to connect to DB2 database from Perl (on unix). I need to connect to DB2 and get records for further processing. Can you please suggest the best possible way. I heard about DBI/DBD, if you have some sample scripts please post them too. Thanks in advance. (3 Replies)
Discussion started by: satguyz
3 Replies
Login or Register to Ask a Question