Exemplo n.º 1
0
func RosenbrockDf(x *vector.GslVector, params interface{}, j *matrix.GslMatrix) int {
	rp := params.(*Rparams)
	x0 := vector.Get(x, 0)
	matrix.Set(j, 0, 0, -rp.A)
	matrix.Set(j, 0, 1, 0)
	matrix.Set(j, 1, 0, -2*rp.B*x0)
	matrix.Set(j, 1, 1, rp.B)
	return int(gogsl.GSL_SUCCESS)
}
Exemplo n.º 2
0
func Df(t float64, y []float64, dfdy []float64, dfdt []float64, params interface{}) int {
	mu := *params.(*float64)
	dfdyMat := matrix.ViewArray(dfdy, 2, 2)
	m := dfdyMat.Matrix()
	matrix.Set(m, 0, 0, 0.0)
	matrix.Set(m, 0, 1, 1.0)
	matrix.Set(m, 1, 0, -2.0*mu*y[0]*y[1]-1.0)
	matrix.Set(m, 1, 1, -mu*(y[0]*y[0]-1.0))
	dfdt[0] = 0.0
	dfdt[1] = 0.0
	return int(gogsl.GSL_SUCCESS)
}
Exemplo n.º 3
0
func TestWeightedQuadtricFit(t *testing.T) {

	var n int = 19

	data := MakeData(0.1, 2.0, 0.1)

	X := matrix.MatrixAlloc(n, 3)
	y := vector.VectorAlloc(n)
	w := vector.VectorAlloc(n)

	c := vector.VectorAlloc(3)
	cov := matrix.MatrixAlloc(3, 3)

	for i := 0; i < n; i++ {
		xi := data[i*3]
		yi := data[i*3+1]
		ei := data[i*3+2]
		fmt.Printf("%g %g +/- %g\n", xi, yi, ei)

		matrix.Set(X, i, 0, 1.0)
		matrix.Set(X, i, 1, xi)
		matrix.Set(X, i, 2, xi*xi)

		vector.Set(y, i, yi)
		vector.Set(w, i, 1.0/(ei*ei))
	}

	work := multifit.LinearAlloc(n, 3)
	_, chisq := multifit.Wlinear(X, w, y, c, cov, work)

	fmt.Printf("# best fit: Y = %g + %g X + %g X^2\n",
		vector.Get(c, 0), vector.Get(c, 1), vector.Get(c, 2))
	fmt.Printf("# covariance matrix:\n")
	fmt.Printf("[ %+.5e, %+.5e, %+.5e  \n",
		matrix.Get(cov, 0, 0), matrix.Get(cov, 0, 1), matrix.Get(cov, 0, 2))
	fmt.Printf("  %+.5e, %+.5e, %+.5e  \n",
		matrix.Get(cov, 1, 0), matrix.Get(cov, 1, 1), matrix.Get(cov, 1, 2))
	fmt.Printf("  %+.5e, %+.5e, %+.5e ]\n",
		matrix.Get(cov, 2, 0), matrix.Get(cov, 2, 1), matrix.Get(cov, 2, 2))
	fmt.Printf("# chisq = %g\n", chisq)
}
Exemplo n.º 4
0
func TestRobust(t *testing.T) {
	var p int = 2 // linear fit

	var a float64 = 1.45 // data slope
	var b float64 = 3.88 // data intercept

	var n int = 20

	X := matrix.MatrixAlloc(n, p)
	x := vector.VectorAlloc(n)
	y := vector.VectorAlloc(n)

	c := vector.VectorAlloc(p)
	cOls := vector.VectorAlloc(p)
	cov := matrix.MatrixAlloc(p, p)

	r := rng.RngAlloc(rng.DefaultRngType())

	// generate linear dataset
	for i := 0; i < n-3; i++ {
		dx := 10.0 / (float64(n) - 1.0)
		ei := rng.Uniform(r)
		xi := -5.0 + float64(i)*dx
		yi := a*xi + b

		vector.Set(x, i, xi)
		vector.Set(y, i, yi+ei)
	}

	// add a few outliers
	vector.Set(x, n-3, 4.7)
	vector.Set(y, n-3, -8.3)

	vector.Set(x, n-2, 3.5)
	vector.Set(y, n-2, -6.7)

	vector.Set(x, n-1, 4.1)
	vector.Set(y, n-1, -6.0)

	// construct design matrix X for linear fit
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)
		matrix.Set(X, i, 0, 1.0)
		matrix.Set(X, i, 1, xi)
	}

	// perform robust and OLS fit
	DoFit(multifit.GSL_MULTIFIT_ROBUST_OLS, X, y, cOls, cov)
	DoFit(multifit.GSL_MULTIFIT_ROBUST_BISQUARE, X, y, c, cov)

	// output data and model
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)
		yi := vector.Get(y, i)
		v := matrix.Row(X, i).Vector()
		_, yRob, _ := multifit.RobustEst(v, c, cov)
		_, yOls, _ := multifit.RobustEst(v, cOls, cov)

		fmt.Printf("%g %g %g %g\n", xi, yi, yRob, yOls)
	}

	fmt.Printf("# best fit: Y = %g + %g X\n", vector.Get(c, 0), vector.Get(c, 1))
	fmt.Printf("# covariance matrix:\n")
	fmt.Printf("# [ %+.5e, %+.5e\n", matrix.Get(cov, 0, 0), matrix.Get(cov, 0, 1))
	fmt.Printf("#   %+.5e, %+.5e\n", matrix.Get(cov, 1, 0), matrix.Get(cov, 1, 1))
}
Exemplo n.º 5
0
func TestBspline(t *testing.T) {

	var n int = 200
	var ncoeffs int = 12
	var nbreak int = ncoeffs - 2

	rng.EnvSetup()
	r := rng.RngAlloc(rng.DefaultRngType())

	// allocate a cubic bspline workspace (k = 4)
	bw := bspline.Alloc(4, nbreak)
	B := vector.VectorAlloc(ncoeffs)

	x := vector.VectorAlloc(n)
	y := vector.VectorAlloc(n)
	X := matrix.MatrixAlloc(n, ncoeffs)
	c := vector.VectorAlloc(ncoeffs)
	w := vector.VectorAlloc(n)
	cov := matrix.MatrixAlloc(ncoeffs, ncoeffs)
	mw := multifit.LinearAlloc(n, ncoeffs)

	fmt.Printf("#m=0,S=0\n")
	// this is the data to be fitted
	for i := 0; i < n; i++ {
		xi := (15.0 / (float64(n) - 1)) * float64(i)
		yi := math.Cos(xi) * math.Exp(-0.1*xi)
		sigma := 0.1 * yi
		dy := randist.Gaussian(r, sigma)
		vector.Set(x, i, xi)
		vector.Set(y, i, yi+dy)
		vector.Set(w, i, 1.0/(sigma*sigma))
		fmt.Printf("%f %f\n", xi, yi+dy)
	}

	// use uniform breakpoints on [0, 15]
	bspline.KnotsUniform(0.0, 15.0, bw)

	// construct the fit matrix X
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)

		// compute B_j(xi) for all j
		bspline.Eval(xi, B, bw)

		// fill in row i of X
		for j := 0; j < ncoeffs; j++ {
			matrix.Set(X, i, j, vector.Get(B, j))
		}
	}

	// do the fit
	_, chisq := multifit.Wlinear(X, w, y, c, cov, mw)
	dof := float64(n - ncoeffs)
	tss := stats.Wtss(w.Data_(), w.Stride(), y.Data_(), y.Stride(), n)
	rsq := 1.0 - chisq/tss
	fmt.Printf("chisq/dof = %e, Rsq = %f\n", chisq/dof, rsq)

	fmt.Printf("#m=1,S=0\n")
	for xi := 0.0; xi < 15.0; xi += 0.1 {
		bspline.Eval(xi, B, bw)
		_, yi, _ := multifit.LinearEst(B, c, cov)
		fmt.Printf("%f %f\n", xi, yi)
	}
}