Example #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]))
}
Example #2
0
func PrintStateFdf(iter int, s *multiroot.GslMultirootFdfsolver) {
	fmt.Printf("iter = %3d x = % .3f % .3f f(x) = % .3e % .3e\n",
		iter,
		vector.Get(s.X_(), 0),
		vector.Get(s.X_(), 1),
		vector.Get(s.F_(), 0),
		vector.Get(s.F_(), 1))
}
Example #3
0
/* Paraboloid centered on (p[0],p[1]), with
   scale factors (p[2],p[3]) and minimum p[4] */
func MyF(v *vector.GslVector, params interface{}) float64 {
	p := params.([]float64)
	x := vector.Get(v, 0)
	y := vector.Get(v, 1)
	return p[2]*(x-p[0])*(x-p[0]) +
		p[3]*(y-p[1])*(y-p[1]) +
		p[4]
}
Example #4
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)
}
Example #5
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)
}
Example #6
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
		}
	}
}
Example #7
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)
}
Example #8
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
		}
	}
}
Example #9
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)
	}
}
Example #10
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
		}
	}
}
Example #11
0
func TestEigen(t *testing.T) {

	data := []float64{1.0, 1 / 2.0, 1 / 3.0, 1 / 4.0,
		1 / 2.0, 1 / 3.0, 1 / 4.0, 1 / 5.0,
		1 / 3.0, 1 / 4.0, 1 / 5.0, 1 / 6.0,
		1 / 4.0, 1 / 5.0, 1 / 6.0, 1 / 7.0}
	m := matrix.ViewArray(data, 4, 4)
	eval := vector.VectorAlloc(4)
	evec := matrix.MatrixAlloc(4, 4)
	w := eigen.SymmvAlloc(4)
	eigen.Symmv(m.Matrix(), eval, evec, w)
	eigen.SymmvSort(eval, evec, eigen.EIGEN_SORT_ABS_ASC)
	for i := 0; i < 4; i++ {
		evalI := vector.Get(eval, i)
		evecI := matrix.Column(evec, i)
		fmt.Printf("eigenvalue = %g\n", evalI)
		fmt.Printf("eigenvector = \n")
		vector.Fprintf(os.Stdout, evecI.Vector(), "%g")
	}
}
Example #12
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))
}
Example #13
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)
	}
}