Aug 13, 2008
I recently had to write a few math functions for some Filtered Averaging stuff. I thought somebody else out there might find them useful
def std_dev(list)
# Calculate neccesary values
list_squared = list.map {|item| item*item }
n = list.size
#Calculate the std deviation
right = (Float (sum(list)**2))/n
((Float (sum(list_squared)) - right) / (n-1)) ** 0.5
end
def correlation(x, y)
# Calculate the necessary values
n = x.size
sum_x = sum(x)
sum_y = sum(y)
x_squared = x.map {|item| item*item }
y_squared = y.map {|item| item*item }
sum_x_squared = sum(x_squared)
sum_y_squared = sum(y_squared)
xy = []
x.each_with_index do |value, key|
xy << value * y[key]
end
sum_xy = sum(xy)
# Calculate the correlation value
left = n * sum_xy - sum_x * sum_y
right = ((n * sum_x_squared - sum_x**2) * (n * sum_y_squared - sum_y**2)) ** 0.5
left / right
end
def sum(list)
list.inject( nil ) { |sum,x| sum ? sum+x : x };
end
Jul 7, 2008
I always forget I have a blog where I’m supposed to announce everything that I do.
I spent a few hours the last couple of days working on a fun little small project called BinaryURL. It does pretty much the opposite of tinyurl, converts a url to a binary string.
check it out
Jun 13, 2008
Over the past few days I’ve been looking at some of the plugins/frameworks that can be used with rails to improve testing. I came across this video of Tammer Saleh giving a presentation titled “BDD with Shoulda” at MountainWest RubyConf 2008. He did a good job giving a brief intro to Shoulda, fixtures, and good testing practices. For anyone who doesn’t know about Shoulda:
The Shoulda Rails plugin makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It’s fully compatible with your existing tests, and requires no retooling to use.
Helpers – context and should give you rSpec like test blocks. In addition, you get nested contexts and a much more readable syntax.
Macros – Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros. They get you started quickly, and can help you ensure that your application is conforming to best practices.
Assertions – Many common rails testing idioms have been distilled into a set of useful assertions.
The presentation is a little over an hour long, but I think it’s definitely worth a watch.
BDD with Shoulda - Tammer Saleh at MountainWest RubyConf 2008