Ejemplo n.º 1
0
func q3() {
	fns := []linreg.TransformFunc{phi0, phi1, phi2, phi3, phi4, phi5, phi6, phi7}

	ks := []int{3, 4, 5, 6, 7}

	data := getData("data/in.dta")
	for _, k := range ks {
		linreg := linreg.NewLinearRegression()

		linreg.InitializeFromData(data[25:])
		linreg.InitializeValidationFromData(data[:25])

		linreg.TransformFunction = fns[k]

		linreg.ApplyTransformation()
		linreg.ApplyTransformationOnValidation()
		linreg.Learn()
		eIn := linreg.Ein()
		eVal := linreg.EValIn()
		eOut, _ := linreg.EoutFromFile("data/out.dta")

		fmt.Printf("EVal = %f, for k = %d\n", eVal, k)
		fmt.Printf("EIn = %f, for k = %d\n", eIn, k)
		fmt.Printf("EOut = %f, for k = %d\n", eOut, k)
		fmt.Println()
	}
}
Ejemplo n.º 2
0
func q5() {
	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 100
	var avgEin, avgEout float64

	for run := 0; run < runs; run++ {
		linreg.Initialize()

		linreg.Learn()
		avgEin += linreg.Ein()
		avgEout += linreg.Eout()
	}
	avgEin = avgEin / float64(runs)
	avgEout = avgEout / float64(runs)
	fmt.Printf("average of In sample error 'Ein' for Linear regresion for N = 100 is %4.2f\n", avgEin)
	fmt.Printf("average of Out of sample error 'Eout' for Linear regresion for N = 100 is %4.2f\n", avgEout)
}
Ejemplo n.º 3
0
func q2() {
	linreg := linreg.NewLinearRegression()
	linreg.InitializeFromFile("data/in.dta")
	linreg.TransformFunction = nonLinearFeature
	linreg.ApplyTransformation()
	linreg.Learn()
	ein := linreg.Ein()
	eout, _ := linreg.EoutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f\n", ein, eout)
	linreg.K = -3
	linreg.LearnWeightDecay()
	eAugIn := linreg.EAugIn()
	eAugOut, _ := linreg.EAugOutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, linreg.K)

	linreg.K = 3
	linreg.LearnWeightDecay()
	eAugIn = linreg.EAugIn()
	eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, linreg.K)

	k := []int{2, 1, 0, -1, -2}
	for _, ki := range k {
		linreg.K = ki
		linreg.LearnWeightDecay()
		eAugIn = linreg.EAugIn()
		eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
		fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, ki)
	}

	minK := 0
	minEAug := float64(1000)
	for i := -10; i < 10; i++ {
		linreg.K = i
		linreg.LearnWeightDecay()
		eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
		if minEAug > eAugOut {
			minK = i
			minEAug = eAugOut
		}
	}
	fmt.Printf("mininum Eout = %f with k = %d for k in {-10; 10}\n", minEAug, minK)
}
Ejemplo n.º 4
0
func q8() {

	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 1000
	linreg.RandomTargetFunction = false
	linreg.TwoParams = true
	linreg.Noise = 0.1
	var avgEin float64

	for run := 0; run < runs; run++ {

		linreg.TargetFunction = f //non linear function
		linreg.Initialize()
		linreg.Learn()
		avgEin += linreg.Ein()
	}

	avgEin = avgEin / float64(runs)
	fmt.Printf("average of In sample error 'Ein' for Linear regresion for N = 100 is %4.2f\n", avgEin)
}