fit

package
v3.0.0-...-8217f41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2018 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Fit: Chapter 4, Curve Fitting.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CorrelationCoefficient

func CorrelationCoefficient(p []struct{ X, Y float64 }) float64

CorrelationCoefficient returns a correlation coefficient for sample data. 求解相关系数 r

Example
package main

import (
	"fmt"

	"github.com/mooncaker816/learnmeeus/v3/fit"
)

func main() {
	// Example 4.b, p. 40.
	data := []struct{ X, Y float64 }{
		{73, 90.4},
		{38, 125.3},
		{35, 161.8},
		{42, 143.4},
		{78, 52.5},
		{68, 50.8},
		{74, 71.5},
		{42, 152.8},
		{52, 131.3},
		{54, 98.5},
		{39, 144.8},

		{61, 78.1},
		{42, 89.5},
		{49, 63.9},
		{50, 112.1},
		{62, 82.0},
		{44, 119.8},
		{39, 161.2},
		{43, 208.4},
		{54, 111.6},
		{44, 167.1},
		{37, 162.1},
	}
	a, b := fit.Linear(data)
	fmt.Printf("y = %.2f - %.2fx\n", b, -a)
	fmt.Printf("r = %.3f\n", fit.CorrelationCoefficient(data))
}
Output:

y = 244.18 - 2.49x
r = -0.767

func Func1

func Func1(p []struct{ X, Y float64 }, f func(float64) float64) float64

Func1 fits a linear multiple of a function to sample data.

Given sample data and a function in x, Func1 returns coefficient a fitting y = aƒ(x).

Example
package main

import (
	"fmt"
	"math"

	"github.com/mooncaker816/learnmeeus/v3/fit"
)

func main() {
	data := []struct{ X, Y float64 }{
		{0, 0},
		{1, 1.2},
		{2, 1.4},
		{3, 1.7},
		{4, 2.1},
		{5, 2.2},
	}
	a := fit.Func1(data, math.Sqrt)
	fmt.Printf("y = %.3f√x\n", a)
}
Output:

y = 1.016√x

func Func3

func Func3(p []struct{ X, Y float64 }, f0, f1, f2 func(float64) float64) (a, b, c float64)

Func3 implements multiple linear regression for a linear combination of three functions.

Given sample data and three functions in x, Func3 returns coefficients a, b, and c fitting y = aƒ₀(x) + bƒ₁(x) + cƒ₂(x) to sample data. 多重线性回归

Example
package main

import (
	"fmt"
	"math"

	"github.com/mooncaker816/learnmeeus/v3/fit"
)

func main() {
	// Example 4.c, p. 44.
	data := []struct{ X, Y float64 }{
		{3, .0433},
		{20, .2532},
		{34, .3386},
		{50, .3560},
		{75, .4983},
		{88, .7577},
		{111, 1.4585},
		{129, 1.8628},
		{143, 1.8264},
		{160, 1.2431},
		{183, -.2043},
		{200, -1.2431},
		{218, -1.8422},
		{230, -1.8726},
		{248, -1.4889},
		{269, -.8372},
		{290, -.4377},
		{303, -.3640},
		{320, -.3508},
		{344, -.2126},
	}
	// fix up data to have X in radians
	for i := range data {
		data[i].X *= math.Pi / 180
	}
	f0 := math.Sin
	f1 := func(x float64) float64 { return math.Sin(2 * x) }
	f2 := func(x float64) float64 { return math.Sin(3 * x) }
	a, b, c := fit.Func3(data, f0, f1, f2)
	// output four decimal places corresponding to precision of Y values.
	fmt.Printf("%.4f, %.4f, %.4f\n", a, b, c)
}
Output:

1.2000, -0.7700, 0.3900

func Linear

func Linear(p []struct{ X, Y float64 }) (a, b float64)

Linear fits a line to sample data.

Argument p is a list of data points. Results a and b are coefficients of the best fit line y = ax + b. 求解线性拟合直线 sx = ∑x sy = ∑y sxy = ∑xy sx2 = ∑x^2

Example
package main

import (
	"fmt"

	"github.com/mooncaker816/learnmeeus/v3/fit"
)

func main() {
	// Example 4.a, p. 37.
	a, b := fit.Linear([]struct{ X, Y float64 }{
		{.2982, 10.92},
		{.2969, 11.01},
		{.2918, 10.99},
		{.2905, 10.78},
		{.2707, 10.87},

		{.2574, 10.80},
		{.2485, 10.75},
		{.2287, 10.14},

		{.2238, 10.21},
		{.2156, 9.97},
		{.1992, 9.69},

		{.1948, 9.57},
		{.1931, 9.66},
		{.1889, 9.63},
		{.1781, 9.65},
		{.1772, 9.44},
		{.1770, 9.44},

		{.1755, 9.32},
		{.1746, 9.20},
	})
	fmt.Printf("a = %.2f  b = %.2f\n", a, b)
	fmt.Printf("m = %.2f + 5 logΔ  + %.2f log r\n", b, a)
}
Output:

a = 13.67  b = 7.03
m = 7.03 + 5 logΔ  + 13.67 log r

func Quadratic

func Quadratic(p []struct{ X, Y float64 }) (a, b, c float64)

Quadratic fits y = ax² + bx + c to sample data.

Argument p is a list of data points. Results a, b, and c are coefficients of the best fit quadratic y = ax² + bx + c. 求解二次拟合曲线系数

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL