Пример #1
0
func MyDf(v *vector.GslVector, params interface{}, df *vector.GslVector) {
	p := params.([]float64)
	x := vector.Get(v, 0)
	y := vector.Get(v, 1)
	vector.Set(df, 0, 2.0*p[2]*(x-p[0]))
	vector.Set(df, 1, 2.0*p[3]*(y-p[1]))
}
Пример #2
0
func Rosenbrock(x *vector.GslVector, params interface{}, f *vector.GslVector) int {
	rp := params.(*Rparams)
	x0 := vector.Get(x, 0)
	x1 := vector.Get(x, 1)
	y0 := rp.A * (1.0 - x0)
	y1 := rp.B * (x1 - x0*x0)
	vector.Set(f, 0, y0)
	vector.Set(f, 1, y1)
	return int(gogsl.GSL_SUCCESS)
}
Пример #3
0
func TestMultirootFdf(t *testing.T) {

	var n int = 2

	rp := &Rparams{
		A: 1.0,
		B: 10.0,
	}

	f := &multiroot.GslMultirootFunctionFdf{
		Function:   Rosenbrock,
		Derivative: RosenbrockDf,
		Fdf:        RosenbrockFdf,
		Dimension:  n,
		Params:     rp,
	}
	multiroot.InitializeGslMultirootFunctionFdf(f)

	xInit := []float64{-10.0, -5.0}
	x := vector.VectorAlloc(n)

	vector.Set(x, 0, xInit[0])
	vector.Set(x, 1, xInit[1])

	T := multiroot.GSL_MULTIROOT_FDFSOLVER_GNEWTON
	s := multiroot.FdfsolverAlloc(T, 2)
	multiroot.FdfsolverSet(s, f, x)

	var iter int

	PrintStateFdf(iter, s)

	for {
		iter += 1
		status := gogsl.GslError(multiroot.FdfsolverIterate(s))
		PrintStateFdf(iter, s)
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multiroot.TestResidual(s.F_(), 1e-7))
		if status != gogsl.GSL_CONTINUE || iter >= 1000 {
			break
		}
		fmt.Printf("status = %s\n", status.String())
	}
}
Пример #4
0
func TestMultimin(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunction{
		Function:  MyF,
		Dimension: 2,
		Params:    par,
	}
	multimin.InitializeGslMultiminFunction(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	/* Set initial step sizes to 1 */
	ss := vector.VectorAlloc(2)
	vector.SetAll(ss, 1.0)

	T := multimin.GSL_MULTIMIN_FMINIMIZER_NSIMPLEX2
	s := multimin.FminimizerAlloc(T, 2)
	multimin.FminimizerSet(s, f, x, ss)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		size := multimin.FminimizerSize(s)
		status = gogsl.GslError(multimin.TestSize(size, 1e-2))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("converged to minimum at\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval(), size)
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
Пример #5
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)
}
Пример #6
0
func TestMultiminFdf(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunctionFdf{
		Function:   MyF,
		Derivative: MyDf,
		Fdf:        MyFdf,
		Dimension:  2,
		Params:     par,
	}
	multimin.InitializeGslMultiminFunctionFdf(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	T := multimin.GSL_MULTIMIN_FDFMINIMIZER_CONJUGATE_FR
	s := multimin.FdfminimizerAlloc(T, 2)
	multimin.FdfminimizerSet(s, f, x, 0.01, 1e-4)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FdfminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multimin.TestGradient(s.Gradient_(), 1e-3))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Minimum found at:\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval())
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
Пример #7
0
func TestSortVectorIndex(t *testing.T) {
	var n int = 10000
	var k int = 5
	v := vector.VectorAlloc(n)
	p := permutation.PermutationAlloc(n)
	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)
	for i := 0; i < n; i++ {
		vector.Set(v, i, rng.Uniform(r))
	}
	sort.SortVectorIndex(p, v)
	pData := p.Slice_().([]int)
	for i := 0; i < k; i++ {
		vpi := vector.Get(v, pData[i])
		fmt.Printf("order = %d, value = %g\n", i, vpi)
	}
}
Пример #8
0
func TestVector(t *testing.T) {
	var stopDoingIt bool
	gogsl.SetErrorHandler(&gogsl.GslErrorHandler{
		Handler: func(reason string, file string, line int, gslError gogsl.GslError) {
			fmt.Println("Caught error: " + reason)
			stopDoingIt = true
		},
	})
	v := vector.VectorAlloc(3)
	for i := 0; i < 3; i++ {
		vector.Set(v, i, float64(1.23)+float64(i))
	}
	for i := 0; i < 100; i++ { // OUT OF RANGE ERROR
		fmt.Printf("v_%d = %g\n", i, vector.Get(v, i))
		if stopDoingIt {
			break
		}
	}
}
Пример #9
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))
}
Пример #10
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)
	}
}