Skip to content

Commit

Permalink
fixup Fit sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhull committed Jun 17, 2024
1 parent 417b2a9 commit 4fe972a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
7 changes: 3 additions & 4 deletions lib/compsci/fit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.constant xs, ys
#

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

def self.sigma enum, &block
enum = enum.map(&block) if block
enum.inject { |sum, n| sum + n }
(block ? enum.map(&block) : enum).sum
end

##
Expand Down Expand Up @@ -76,7 +75,7 @@ def self.error xys, &blk

def self.logarithmic xs, ys
n = xs.size
xys = xs.zip(ys)
xys = xs.zip(ys).take(n)
slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }
slnx = sigma(xys) { |x, _| Math.log(x) }
sylnx = sigma(xys) { |x, y| y * Math.log(x) }
Expand Down
34 changes: 24 additions & 10 deletions sig/fit.rbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
module CompSci
module Fit
def self.constant: (Array[Numeric] xs, Array[Numeric] ys) ->
Array[Numeric]
def self.best: (Array[Numeric] xs, Array[Numeric] ys) -> Array[untyped]
def self.sigma: (Enumerable enum) ?{ (?) -> Numeric } -> Numeric
def self.error: (untyped xys) { (untyped) -> untyped } -> untyped
def self.logarithmic: (untyped xs, untyped ys) -> ::Array[untyped]
def self.linear: (untyped xs, untyped ys) -> ::Array[untyped]
def self.exponential: (untyped xs, untyped ys) -> ::Array[untyped]
def self.power: (untyped xs, untyped ys) -> ::Array[untyped]
def self.predict: (untyped model, untyped a, untyped b, untyped x) -> untyped
def self.constant: (Array[Float | Integer] xs,
Array[Float | Integer] ys) -> [Float, (Float | Integer)]
def self.best: (Array[Float | Integer] xs,
Array[Float | Integer] ys) -> Array[Float | Symbol]

def self.sigma: (Array[Float | Integer] enum) ?{ (untyped) -> untyped } -> Float
| (Array[[Float | Integer, Float | Integer]] enum) ?{ (untyped) -> untyped } -> Float
| (Array[Array[Float | Integer]] enum) ?{ (untyped) -> untyped } -> Float
| (Array[untyped] enum) ?{ (untyped) -> untyped } -> Float

def self.error: (Array[Float | Integer] xys) { (untyped) -> untyped } -> Float
| (Array[[Float | Integer, Float | Integer]] xys) { (untyped) -> untyped } -> Float
| (Array[Array[Float | Integer]] xys) { (untyped) -> untyped } -> Float
| (Array[untyped] xys) { (untyped) -> untyped } -> Float

def self.logarithmic: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]

def self.linear: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]

def self.exponential: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]

def self.power: (Array[Float | Integer] xs, Array[Float | Integer] ys) -> [Float, Float, Float]

def self.predict: (Symbol model, Float a, Float b, (Float | Integer) x) -> Float
end
end

0 comments on commit 4fe972a

Please sign in to comment.