Trevor Berg: The Blog

Avatar

Standard Deviation and Correlation Coefficient in ruby

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

BinaryURL Launches!

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

BDD with Shoulda

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

RailsConf for those who didn’t go to RailsConf

So you’re one of the many like me who were unable to attend RailsConf this year in Portland and want to see some of the presentations.  I’ve gathered as many of the presentations, videos, slides as I could find for you.  

Scott Chacon’s Introduction to Git 

Scott gave a great introduction for those of us who have heard about Git, but haven’t yet given it a try.  Scott was nice enough to release his slides from his presentation, as well as add the audio from his presentation to the slides.  

 

 

Greg Pollack interviews everyone he can find

Greg wandered around RailsConf with a camera and asked pretty much all the presenters he could find to summarize their presentations.  

 
Railsconf in 36 minutes from Gregg Pollack on Vimeo.

 
RailsConf 2008 David Heinemeier Hansson’s Keynote Video


RailsConf 2008 David Heinemeier Hansson Keynote from daniel wanja on Vimeo.

You can view the slides from all the presentations at the RailsConf 2008 speakers site.
 
I’m continuing to look for additional presentations and I’ll add new ones to this post as I find them.  If you know any that I’m missing drop me a message and I’ll add them.

Continue