callidus 1.0.67 → 1.0.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e8348b821e13ceed061213ebf3f311f32c52853
4
- data.tar.gz: b9231b12ef42ef76f52129149c367d724d4280cf
3
+ metadata.gz: 64ccfbdf3a87cae055d4adb41fde3f0e3b4401ac
4
+ data.tar.gz: a63079883594328163839c8f881c1c92f129ae0c
5
5
  SHA512:
6
- metadata.gz: 46ac0cbdd0e5bf0f50475505a6ed89c60537ac0bfa9879872238ac269e460bebe6347e194c694f044d5aac371add9cfc28dba2f16d5787fec4704c03ed1d6834
7
- data.tar.gz: 075e233a7eb2c697aea13d7966a548dcd8d310220d7db2d743d611658020e74c11ee488a15184cb3679799119be5d917af9839ba8355de90d2594a52b5379916
6
+ metadata.gz: 50a366f39e18b3dab2820f946a26229ede811951790ea093e79a2d842ccd18307d71ca28670a1465f5be80613bbd6e45a9ca56d4be057405a8e3401526f38438
7
+ data.tar.gz: 3f88ebb2f1b8e2b8ce1c384367f7aece0c45cf274b2ef5a921909aa46fcc46e198d7e2b7cbf71ab06678ee7014127b5277afb8d1473b2ea7936f57e157f08d37
@@ -1,3 +1,4 @@
1
1
  require_relative "./src/KNN.rb"
2
2
  require_relative "./src/LinearRegression.rb"
3
- require_relative "./src/Winnow.rb"
3
+ require_relative "./src/Winnow.rb"
4
+ require_relative "./src/ExponentialRegression.rb"
@@ -0,0 +1,89 @@
1
+ require_relative "../util/Comp"
2
+
3
+ module Callidus
4
+ class ExponentialRegression
5
+ attr_accessor :input
6
+ attr_accessor :output
7
+ attr_accessor :predicted_output
8
+
9
+ attr_reader :a
10
+ attr_reader :b
11
+
12
+ attr_reader :correlation
13
+ attr_reader :standard_error
14
+
15
+ def initialize(ip = [], op = [])
16
+ @input = ip
17
+ @output = op
18
+
19
+ @trained = false
20
+ end
21
+
22
+ private def assert_trained(method = "train")
23
+ if !@trained
24
+ raise Util::UntrainedError.new("ExponentialRegression")
25
+ end
26
+ end
27
+
28
+ def formatted
29
+ assert_trained()
30
+
31
+ "f(x) = #{@a}e^#{@b}x"
32
+ end
33
+
34
+ def train
35
+ mean_x = @input.mean
36
+ mean_lny = @output.inject(0.0) { |s, n| s + Math.log(n) } / @output.size
37
+
38
+ sum_xx = 0.0
39
+ sum_xy = 0.0
40
+ sum_yy = 0.0
41
+
42
+ @input.each_index do |i|
43
+ sum_xx += @input[i] * @input[i]
44
+ sum_xy += @input[i] * Math.log(@output[i])
45
+ sum_yy += Math.log(@output[i]) * Math.log(@output[i])
46
+ end
47
+
48
+ sum_xx = (sum_xx / @input.size) - (mean_x * mean_x)
49
+ sum_xy = (sum_xy / @input.size) - (mean_x * mean_lny)
50
+ sum_yy = (sum_yy / @output.size) - (mean_lny * mean_lny)
51
+
52
+ @b = sum_xy / sum_xx
53
+ @a = 2.7182818284590452353602 ** (mean_lny - b * mean_x)
54
+
55
+ @predicted_output = @input.map { |x| (@a * (2.7182818284590452353602 ** (@b * x))).round(3) }
56
+
57
+ @correlation = (sum_xy / (Math.sqrt(sum_xx) * Math.sqrt(sum_yy))).round(3)
58
+
59
+ @trained = true
60
+
61
+ @a = @a.round(3)
62
+ @b = @b.round(3)
63
+
64
+ self.find_standard_error
65
+ end
66
+
67
+ def find_standard_error
68
+ assert_trained()
69
+
70
+ n = @input.size > @output.size ? @output.size : @input.size
71
+
72
+ diffs = []
73
+
74
+ n.times do |i|
75
+ diffs << (@predicted_output[i] - @output[i]) ** 2
76
+ end
77
+
78
+ @standard_error = (Math.sqrt(diffs.sum/(n - 2.0))).round(3)
79
+
80
+ self
81
+ end
82
+
83
+ def predict(x)
84
+ assert_trained()
85
+
86
+ @a * (2.7182818284590452353602 ** (@b * x))
87
+ end
88
+ end
89
+ end
@@ -22,7 +22,7 @@ module Callidus
22
22
  tdiffs.pop(ind)
23
23
  end
24
24
 
25
- return @output[diffs.index(Comp::Arrays.MostFrequent(mins))];
25
+ @output[diffs.index(Comp::Arrays.MostFrequent(mins))];
26
26
  end
27
27
  end
28
28
  end
@@ -91,6 +91,8 @@ module Callidus
91
91
  end
92
92
 
93
93
  def predict(x)
94
+ assert_trained()
95
+
94
96
  x * @slope + @y_intercept
95
97
  end
96
98
  end
@@ -29,7 +29,7 @@ module Callidus
29
29
  end
30
30
 
31
31
  def train(a = @a)
32
- if (@input[0] and @input[0].size != @weights.size)
32
+ if (@input.size > 0 and @input[0] and @input[0].size > 0 and @input[0].size != @weights.size)
33
33
  @weights = Array.new(@input[0].size, @default_weight)
34
34
  end
35
35
 
@@ -43,7 +43,7 @@ module Callidus
43
43
 
44
44
  @trained = true
45
45
 
46
- return self
46
+ self
47
47
  end
48
48
 
49
49
  def predict(x, options = {})
@@ -58,7 +58,7 @@ module Callidus
58
58
  probability += @weights[i] * x[i] + bias
59
59
  end
60
60
 
61
- return (probability > threshold ? 1 : 0)
61
+ (probability > threshold ? 1 : 0)
62
62
  end
63
63
  end
64
64
  end
@@ -32,6 +32,10 @@ class ::Array
32
32
 
33
33
  m
34
34
  end
35
+
36
+ def mean
37
+ self.sum/self.size.to_f
38
+ end
35
39
  end
36
40
 
37
41
  module Comp
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callidus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.67
4
+ version: 1.0.73
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hayden Higginbotham
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/callidus.rb
21
+ - lib/src/ExponentialRegression.rb
21
22
  - lib/src/KNN.rb
22
23
  - lib/src/LinearRegression.rb
23
24
  - lib/src/Winnow.rb
@@ -36,7 +37,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="
38
39
  - !ruby/object:Gem::Version
39
- version: '0'
40
+ version: 1.9.0
40
41
  required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  requirements:
42
43
  - - ">="