Created
June 26, 2013 07:46
-
-
Save bjmorgan/5865523 to your computer and use it in GitHub Desktop.
Calculated the mean squared error between data in two files. Files should be single columns of the same length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /home/morgan/bin/ruby/bin/ruby | |
# Calculates the mean squared error between two data sets | |
# http://en.wikipedia.org/wiki/Mean_squared_error | |
# BJM 26/06/13 | |
def check_files_exist( filenames ) | |
all_files_exist = true | |
filenames.each do |filename| | |
unless File.exist?( filename ) | |
all_files_exist = false | |
warn( "#{$executable_name}: #{filename} not found" ) | |
end | |
end | |
abort unless all_files_exist | |
end | |
def read_column_of_data( filename ) | |
return File.new( filename, 'r' ).readlines.map{ |l| l.to_f } | |
end | |
$executable_name = File.basename( $PROGRAM_NAME ) | |
file1 = ARGV[0] | |
file2 = ARGV[1] | |
check_files_exist( [file1, file2] ) | |
data1 = read_column_of_data( file1 ) | |
data2 = read_column_of_data( file2 ) | |
if ( data1.size != data2.size ) | |
abort( "#{$executable_name}: incommensurate data\n#{data1.size} lines in #{file1}\n#{data2.size} lines in #{file2}" ) | |
end | |
puts data1.zip( data2 ).inject(0) { |sum, d| sum + ( d[0] - d[1] )**2.0 } / data1.size.to_f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment