Skip to content

Commit 4fe972a

Browse files
committed
fixup Fit sigs
1 parent 417b2a9 commit 4fe972a

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

lib/compsci/fit.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def self.constant xs, ys
2020
#
2121

2222
def self.best xs, ys
23-
vals = []
23+
vals = [0.0, 0.0, 0.0, :default]
2424
max_r2 = 0
2525
[:logarithmic, :linear, :exponential, :power].each { |fn|
2626
a, b, r2 = Fit.send(fn, xs, ys)
@@ -45,8 +45,7 @@ def self.best xs, ys
4545
# sigma([1, 2, 3]) { |n| n ** 2 } # => 1 + 4 + 9 => 14
4646

4747
def self.sigma enum, &block
48-
enum = enum.map(&block) if block
49-
enum.inject { |sum, n| sum + n }
48+
(block ? enum.map(&block) : enum).sum
5049
end
5150

5251
##
@@ -76,7 +75,7 @@ def self.error xys, &blk
7675

7776
def self.logarithmic xs, ys
7877
n = xs.size
79-
xys = xs.zip(ys)
78+
xys = xs.zip(ys).take(n)
8079
slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }
8180
slnx = sigma(xys) { |x, _| Math.log(x) }
8281
sylnx = sigma(xys) { |x, y| y * Math.log(x) }

sig/fit.rbs

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
module CompSci
22
module Fit
3-
def self.constant: (Array[Numeric] xs, Array[Numeric] ys) ->
4-
Array[Numeric]
5-
def self.best: (Array[Numeric] xs, Array[Numeric] ys) -> Array[untyped]
6-
def self.sigma: (Enumerable enum) ?{ (?) -> Numeric } -> Numeric
7-
def self.error: (untyped xys) { (untyped) -> untyped } -> untyped
8-
def self.logarithmic: (untyped xs, untyped ys) -> ::Array[untyped]
9-
def self.linear: (untyped xs, untyped ys) -> ::Array[untyped]
10-
def self.exponential: (untyped xs, untyped ys) -> ::Array[untyped]
11-
def self.power: (untyped xs, untyped ys) -> ::Array[untyped]
12-
def self.predict: (untyped model, untyped a, untyped b, untyped x) -> untyped
3+
def self.constant: (Array[Float | Integer] xs,
4+
Array[Float | Integer] ys) -> [Float, (Float | Integer)]
5+
def self.best: (Array[Float | Integer] xs,
6+
Array[Float | Integer] ys) -> Array[Float | Symbol]
7+
8+
def self.sigma: (Array[Float | Integer] enum) ?{ (untyped) -> untyped } -> Float
9+
| (Array[[Float | Integer, Float | Integer]] enum) ?{ (untyped) -> untyped } -> Float
10+
| (Array[Array[Float | Integer]] enum) ?{ (untyped) -> untyped } -> Float
11+
| (Array[untyped] enum) ?{ (untyped) -> untyped } -> Float
12+
13+
def self.error: (Array[Float | Integer] xys) { (untyped) -> untyped } -> Float
14+
| (Array[[Float | Integer, Float | Integer]] xys) { (untyped) -> untyped } -> Float
15+
| (Array[Array[Float | Integer]] xys) { (untyped) -> untyped } -> Float
16+
| (Array[untyped] xys) { (untyped) -> untyped } -> Float
17+
18+
def self.logarithmic: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]
19+
20+
def self.linear: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]
21+
22+
def self.exponential: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]
23+
24+
def self.power: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]
25+
26+
def self.predict: (Symbol model, Float a, Float b, (Float | Integer) x) -> Float
1327
end
1428
end

0 commit comments

Comments
 (0)