|
| 1 | +defmodule LearnKit.Regression.Linear.Calculations do |
| 2 | + @moduledoc """ |
| 3 | + Module for fit functions |
| 4 | + """ |
| 5 | + |
| 6 | + alias LearnKit.Math |
| 7 | + |
| 8 | + defmacro __using__(_opts) do |
| 9 | + quote do |
| 10 | + defp fit_data(method, factors, results) when method == "gradient descent" do |
| 11 | + gradient_descent_iteration([:rand.uniform, :rand.uniform], 0.0001, nil, 1000000, Enum.zip(factors, results), 0) |
| 12 | + end |
| 13 | + |
| 14 | + defp fit_data(_, factors, results) do |
| 15 | + beta = Math.correlation(factors, results) * Math.standard_deviation(results) / Math.standard_deviation(factors) |
| 16 | + alpha = Math.mean(results) - beta * Math.mean(factors) |
| 17 | + [alpha, beta] |
| 18 | + end |
| 19 | + |
| 20 | + defp predict_sample(sample, [alpha, beta]) do |
| 21 | + sample * beta + alpha |
| 22 | + end |
| 23 | + |
| 24 | + defp calculate_score([], _, _), do: raise("There was no fit for model") |
| 25 | + |
| 26 | + defp calculate_score(coefficients, factors, results) do |
| 27 | + 1.0 - sum_of_squared_errors(coefficients, factors, results) / total_sum_of_squares(results) |
| 28 | + end |
| 29 | + |
| 30 | + defp total_sum_of_squares(list) do |
| 31 | + mean_list = Math.mean(list) |
| 32 | + list |
| 33 | + |> Enum.map(fn x -> :math.pow(x - mean_list, 2) end) |
| 34 | + |> Enum.sum |
| 35 | + end |
| 36 | + |
| 37 | + defp sum_of_squared_errors(coefficients, factors, results) do |
| 38 | + Enum.zip(factors, results) |
| 39 | + |> Enum.map(fn {xi, yi} -> squared_prediction_error(coefficients, xi, yi) end) |
| 40 | + |> Enum.sum |
| 41 | + end |
| 42 | + |
| 43 | + defp squared_prediction_error(coefficients, x, y) do |
| 44 | + coefficients |
| 45 | + |> prediction_error(x, y) |
| 46 | + |> :math.pow(2) |
| 47 | + end |
| 48 | + |
| 49 | + defp squared_error_gradient(coefficients, x, y) do |
| 50 | + error_variable = coefficients |> prediction_error(x, y) |
| 51 | + [ |
| 52 | + -2 * error_variable, |
| 53 | + -2 * error_variable * x |
| 54 | + ] |
| 55 | + end |
| 56 | + |
| 57 | + defp prediction_error(coefficients, x, y) do |
| 58 | + y - predict_sample(x, coefficients) |
| 59 | + end |
| 60 | + |
| 61 | + defp gradient_descent_iteration(_, _, min_theta, _, _, iterations_with_no_improvement) when iterations_with_no_improvement >= 100, do: min_theta |
| 62 | + |
| 63 | + defp gradient_descent_iteration(theta, alpha, min_theta, min_value, data, iterations_with_no_improvement) do |
| 64 | + [ |
| 65 | + min_theta, |
| 66 | + min_value, |
| 67 | + iterations_with_no_improvement, |
| 68 | + alpha |
| 69 | + ] = data |> check_value(min_value, theta, min_theta, iterations_with_no_improvement, alpha) |
| 70 | + |
| 71 | + theta = data |
| 72 | + |> Enum.shuffle |
| 73 | + |> Enum.reduce(theta, fn {xi, yi}, acc -> |
| 74 | + gradient_i = squared_error_gradient(acc, xi, yi) |
| 75 | + acc |> Math.vector_subtraction(alpha |> Math.scalar_multiply(gradient_i)) |
| 76 | + end) |
| 77 | + gradient_descent_iteration(theta, alpha, min_theta, min_value, data, iterations_with_no_improvement) |
| 78 | + end |
| 79 | + |
| 80 | + defp check_value(data, min_value, theta, min_theta, iterations_with_no_improvement, alpha) do |
| 81 | + value = data |
| 82 | + |> Enum.map(fn {xi, yi} -> squared_prediction_error(theta, xi, yi) end) |
| 83 | + |> Enum.sum |
| 84 | + cond do |
| 85 | + value < min_value -> |
| 86 | + [theta, value, 0, 0.0001] |
| 87 | + true -> |
| 88 | + [min_theta, min_value, iterations_with_no_improvement + 1, alpha * 0.9] |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments