• API
• FAQ
• Tools
• Archive
daily pastebin goal
34%
SHARE
TWEET

# Untitled

a guest Mar 8th, 2018 45 Never
ENDING IN00days00hours00mins00secs

1. module Statistics
2.   def self.residuals(xdata,ydata)
3.     residuals = []
4.     xdata.each_with_index {|x, i|
5.       y = ydata[i]
6.       y_hat = self.y_hat(x,xdata,ydata)
7.       residuals << y - y_hat
8.     }
9.     residuals
10.   end
11.
12.   def self.y_hat(x,xdata,ydata)
13.     lin_reg_a(xdata,ydata) + lin_reg_b(xdata,ydata)*x.to_f
14.   end
15.
16.   def self.lin_reg_b(xdata,ydata)
17.     std_x = self.standard_deviation(xdata)
18.     std_y = self.standard_deviation(ydata)
19.
20.     (self.correlation(xdata,ydata))*(std_y/std_x)
21.   end
22.
23.   def self.lin_reg_a(xdata,ydata)
24.     x_bar = self.mean(xdata)
25.     y_bar = self.mean(ydata)
26.
27.     b = self.lin_reg_b(xdata,ydata)
28.
29.     y_bar - (b*x_bar)
30.   end
31.
32.   def self.correlation(xdata,ydata)
33.     raise ArgumentError unless xdata.length == ydata.length
34.     std_x = self.standard_deviation(xdata)
35.     std_y = self.standard_deviation(ydata)
36.     x_bar = self.mean(xdata)
37.     y_bar = self.mean(ydata)
38.
39.     s = 0.0
40.     xdata.each_with_index {|x,i|
41.       y = ydata[i]
42.       s = s + (self.z_score(x,xdata)*self.z_score(y,ydata)).to_f
43.     }
44.
45.     (1.to_f/(xdata.size-1).to_f)*s
46.   end
47.
48.   def self.z_score(datum,data)
49.     (datum - self.mean(data)).to_f/self.standard_deviation(data)
50.   end
51.
52.   def self.standard_deviation(data)
53.     s = data.inject(0) {|m,o| m + (o - self.mean(data).to_f )**2.to_f }
54.     std = (1.to_f/(data.size-1).to_f)*s
55.     Math.sqrt(std).to_f
56.   end
57.
58.   def self.mode(data)
59.     x = data.inject(Hash.new(0)) {|m,o| m[o] += 1;m }
60.     if x.values.uniq.size == 1
61.       nil
62.     else
63.       x.to_a.max {|a,o| a[1] <=> o[1] }[0]
64.     end
65.   end
66.
67.   def self.median(data)
68.     if data.size % 2 == 0
69.       x = (data.size - 2) / 2
70.       y = data[x-1,2]
71.       return y.inject(0) {|m,o| m+o}.to_f / 2.0
72.     else
73.       x = (data.size - 1) / 2
74.       return *data[x-1,1]
75.     end
76.   end
77.
78.   def self.mean(data)
79.     data.inject(0) {|m,o| m+o }.to_f / data.size.to_f
80.   end
81.
82.   def self.sum(data)
83.     data.inject(0) {|m,o| m+o }.to_f
84.   end
85. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top