How to resolve the algorithm Polynomial regression step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Polynomial regression step by step in the Go programming language

Table of Contents

Problem Statement

Find an approximating polynomial of known degree for a given data. Example: For input data: The approximating polynomial is: Here, the polynomial's coefficients are (3, 2, 1). This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polynomial regression step by step in the Go programming language

First Code

This code uses the gonum library to solve a linear least squares problem using QR factorization.

  • The code creates two slices of floats, x and y, which represent the input data.
  • It sets the degree of the polynomial to fit, which is 2.
  • It creates a Vandermonde matrix a from x and the degree.
  • It creates a column vector b from y.
  • It creates a column vector c to store the coefficients of the polynomial.
  • It uses the QR factorization to solve the linear least squares problem.
  • It prints the coefficients of the polynomial.

Second Code

This code uses the go.matrix library to solve a linear least squares problem using QR factorization.

  • The code creates two slices of floats, yGiven and xGiven, which represent the input data.
  • It sets the degree of the polynomial to fit, which is 2.
  • It creates a matrix y from yGiven.
  • It creates a matrix x from xGiven and the degree.
  • It uses the QR factorization to solve the linear least squares problem.
  • It prints the coefficients of the polynomial.

Differences between the two codes

The main differences between the two codes are:

  • The first code uses the gonum library, while the second code uses the go.matrix library.
  • The first code creates a Vandermonde matrix, while the second code creates a matrix from scratch.
  • The first code uses the SolveTo method to solve the linear least squares problem, while the second code uses the QR factorization directly.

Overall

Both codes solve the same problem using different libraries and approaches. The first code is more concise, while the second code is more explicit.

Source code in the go programming language

package main

import (
	"fmt"
	"log"

	"gonum.org/v1/gonum/mat"
)

func main() {
	var (
		x = []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
		y = []float64{1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}

		degree = 2

		a = Vandermonde(x, degree+1)
		b = mat.NewDense(len(y), 1, y)
		c = mat.NewDense(degree+1, 1, nil)
	)

	var qr mat.QR
	qr.Factorize(a)

	const trans = false
	err := qr.SolveTo(c, trans, b)
	if err != nil {
		log.Fatalf("could not solve QR: %+v", err)
	}
	fmt.Printf("%.3f\n", mat.Formatted(c))
}

func Vandermonde(a []float64, d int) *mat.Dense {
	x := mat.NewDense(len(a), d, nil)
	for i := range a {
		for j, p := 0, 1.0; j < d; j, p = j+1, p*a[i] {
			x.Set(i, j, p)
		}
	}
	return x
}


package main

import (
    "fmt"

    "github.com/skelterjohn/go.matrix"
)

var xGiven = []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var yGiven = []float64{1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}
var degree = 2

func main() {
    m := len(yGiven)
    n := degree + 1
    y := matrix.MakeDenseMatrix(yGiven, m, 1)
    x := matrix.Zeros(m, n)
    for i := 0; i < m; i++ {
        ip := float64(1)
        for j := 0; j < n; j++ {
            x.Set(i, j, ip)
            ip *= xGiven[i]
        }
    }

    q, r := x.QR()
    qty, err := q.Transpose().Times(y)
    if err != nil {
        fmt.Println(err)
        return
    }
    c := make([]float64, n)
    for i := n - 1; i >= 0; i-- {
        c[i] = qty.Get(i, 0)
        for j := i + 1; j < n; j++ {
            c[i] -= c[j] * r.Get(i, j)
        }
        c[i] /= r.Get(i, i)
    }
    fmt.Println(c)
}


  

You may also check:How to resolve the algorithm Factors of an integer step by step in the Factor programming language
You may also check:How to resolve the algorithm Password generator step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Wren programming language
You may also check:How to resolve the algorithm Galton box animation step by step in the Julia programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Julia programming language