Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Variance, Mean, Normalizing Functions, Euclidean And Other Distances

·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views
```Here sum, mean and variance were inspired by the Peter's inline sum code:

```
class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end
class Array; def mean; self.sum/self.size.to_f; end; end
class Array; def variance; mean = self.mean; Math.sqrt(inject( nil ) { |var,x| var ? var+((x-mean)**2) : ((x-mean)**2)}/self.size.to_f); end; end
```

If you want to normalize a random variable (array) so that mean = 0 and variance = 1, you can transform your array x by calling:
```
# inputs a random variable, sets mean = 0 and variance = 1
def standardize_random_variable(x)
mean = x.mean
variance = x.variance
x.map!{|a| (a-mean)/variance }
end
```

If you want to compute distance, call these functions between two arrays of data, a and b.

```
## Distance Functions

# Sum of (x-y)^2
def euclidean_squared_distance(a,b)
b = b.to_a
a = a.to_a
sum_of_diff_sq = 0
(0...a.size).each { |i| sum_of_diff_sq+=((a[i].to_f-b[i].to_f)**2)}
sum_of_diff_sq
end

# Square root of sum of (x-y)^2
def euclidean_distance(neighbor,xq)
Math.sqrt(euclidean_squared_distance(neighbor,xq))
end

# Sum of abs(x,y)
def cityblock_distance(neighbor,xq)
xq = xq.to_a
abs_diff = 0
(0...xq.size).each { |i| abs_diff+=(Math.abs(xq[i].to_f-neighbor[i].to_f)}
abs_diff
end
``````
Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.