Skip to content

Commit 3e408ec

Browse files
committed
Polynomial Regression Score
1 parent 9747ebe commit 3e408ec

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

lib/learn_kit/regression/linear.ex

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ defmodule LearnKit.Regression.Linear do
117117
def predict(linear = %Linear{coefficients: _}, samples) when is_list(samples) do
118118
{
119119
:ok,
120-
Enum.map(samples, fn sample -> predict(linear, sample) end)
120+
do_predict(linear, samples)
121121
}
122122
end
123123

@@ -138,29 +138,6 @@ defmodule LearnKit.Regression.Linear do
138138
@spec predict(%Linear{coefficients: coefficients}, list) :: {:ok, list}
139139

140140
def predict(%Linear{coefficients: [alpha, beta]}, sample) do
141-
sample * beta + alpha
142-
end
143-
144-
@doc """
145-
Returns the coefficient of determination R^2 of the prediction
146-
147-
## Parameters
148-
149-
- predictor: %LearnKit.Regression.Linear{}
150-
151-
## Examples
152-
153-
iex> predictor |> LearnKit.Regression.Linear.score
154-
{:ok, 0.9876543209876543}
155-
156-
"""
157-
@spec score(%Linear{factors: factors, results: results, coefficients: coefficients}) ::
158-
{:ok, number}
159-
160-
def score(linear = %Linear{factors: _, results: _, coefficients: _}) do
161-
{
162-
:ok,
163-
calculate_score(linear)
164-
}
141+
{:ok, sample * beta + alpha}
165142
end
166143
end

lib/learn_kit/regression/linear/calculations.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ defmodule LearnKit.Regression.Linear.Calculations do
2929
[alpha, beta]
3030
end
3131

32+
defp do_predict(linear, samples) do
33+
Enum.map(samples, fn sample ->
34+
{:ok, prediction} = predict(linear, sample)
35+
prediction
36+
end)
37+
end
38+
3239
defp squared_error_gradient(linear, x, y) do
3340
error_variable = prediction_error(linear, x, y)
3441

lib/learn_kit/regression/polynomial.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ defmodule LearnKit.Regression.Polynomial do
66
defstruct factors: [], results: [], coefficients: [], degree: 2
77

88
alias LearnKit.Regression.Polynomial
9+
910
use Polynomial.Calculations
11+
use LearnKit.Regression.Score
1012

1113
@type factors :: [number]
1214
@type results :: [number]

lib/learn_kit/regression/score.ex

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
defmodule LearnKit.Regression.Score do
22
@moduledoc """
3-
Module for fit functions
3+
Module for scoring regression models
44
"""
55

66
alias LearnKit.Math
77

88
defmacro __using__(_opts) do
99
quote do
10+
@doc """
11+
Returns the coefficient of determination R^2 of the prediction
12+
13+
## Parameters
14+
15+
- predictor: %LearnKit.Regression.Linear{}
16+
17+
## Examples
18+
19+
iex> predictor |> LearnKit.Regression.Linear.score
20+
{:ok, 0.9876543209876543}
21+
22+
"""
23+
@spec score(%LearnKit.Regression.Linear{
24+
factors: factors,
25+
results: results,
26+
coefficients: coefficients
27+
}) :: {:ok, number}
28+
29+
def score(regression = %_{factors: _, results: _, coefficients: _}) do
30+
{
31+
:ok,
32+
calculate_score(regression)
33+
}
34+
end
35+
1036
defp calculate_score(%_{coefficients: []}, _, _), do: raise("There was no fit for model")
1137

1238
defp calculate_score(regression = %_{coefficients: _, factors: _, results: results}) do
1339
1.0 - sum_of_squared_errors(regression) / total_sum_of_squares(results)
1440
end
1541

1642
defp prediction_error(regression, x, y) do
17-
y - predict(regression, x)
43+
{:ok, prediction} = predict(regression, x)
44+
y - prediction
1845
end
1946

2047
defp sum_of_squared_errors(

test/learn_kit/regression/polynomial_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,11 @@ defmodule LearnKit.Regression.PolynomialTest do
5959
|> Polynomial.fit(degree: 2)
6060
|> Polynomial.predict([3, 5]) == {:ok, [-0.009459989001544572, 0.10916263554608596]}
6161
end
62+
63+
test "returns coefficient of determination R^2 of the prediction", state do
64+
predictor = state.predictor |> Polynomial.fit()
65+
66+
assert {:ok, result} = predictor |> Polynomial.score()
67+
assert result == 0.9614116660464942
68+
end
6269
end

0 commit comments

Comments
 (0)