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

Dropbox Redux

As I posted a few days ago, I flew out to San Francisco a week or two ago to checkout the dropbox guys. I did some design stuff for them, and since I’ve got back I haven’t been able to get them out of my head. I wanted to keep thing simple, but add some color and some contrast. I’m pretty happy with this very unpolished version I’ve come up with. Anyways, here’s the results of my efforts:

Original My Version

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

Blogging Again

So for the third time in the past year, I’ve decided to reopen my blog.  I’ll mainly be focusing on ruby on rails related stuff, so watch for some more content in the future.

Continue