func main() { // create file storing ks and vard f, err := os.Create(fname + ".csv") if err != nil { panic(err) } defer f.Close() ksarray := []float64{} // store ks vdarray := []float64{} // store VarD ngarray := []float64{} // store generation number // do evolution for i := 0; i < ngen; i++ { pop.Evolve() // we make 10 samples and average ksmean := desc.NewMean() vdmean := desc.NewMean() for j := 0; j < sampleTime; j++ { sample := fwd.Sample(pop.Genomes, sampleSize) dmatrix := fwd.GenerateDistanceMatrix(sample) cmatrix := covs.NewCMatrix(sampleSize, pop.Length, dmatrix) ks, vard := cmatrix.D() ksmean.Increment(ks) vdmean.Increment(vard) } // write to csv f.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult())) if (i+1)%1000 == 0 { fmt.Println("Generation: ", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult()) } // store array for draw ksarray = append(ksarray, ksmean.GetResult()) vdarray = append(vdarray, vdmean.GetResult()) ngarray = append(ngarray, float64(pop.NumOfGen)) } // draw svger := render.NewSVG(fname, 1, 2, 800, 200) pl := chart.ScatterChart{Title: "KS"} pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "VarD"} pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() // save population jf, err := os.Create(fname + ".json") if err != nil { panic(err) } defer jf.Close() b, err := json.Marshal(pop) if err != nil { panic(err) } jf.Write(b) }
func (s *Statistics) Add(c *chart.ScatterChart, name, col string, eval func() float64) { xs, ys := []float64{}, []float64{} rgbac := color.RGBAModel.Convert(hexcolor.Hex(col)) c.AddDataPair(name, xs, ys, chart.PlotStyleLines, chart.Style{LineColor: rgbac, LineWidth: 1}) *s = append(*s, Statistic{Eval: eval}) for i := range c.Data { (*s)[i].Samples = &c.Data[i].Samples } }
// the converge of KS equalibrium. func main() { ksarray := []float64{} // store ks vdarray := []float64{} // store VarD ngarray := []float64{} // store generation number // do evolution for i := 0; i < ngen; i++ { pop.Evolve() // select 10 samples and averge mean := desc.NewMean() vmean := desc.NewMean() ch := make(chan dResult) num := 10 for j := 0; j < num; j++ { sample := fwd1.Sample(pop.Genomes, sampleSize) go calculateD(sample, pop.Length, ch) } for j := 0; j < num; j++ { dr := <-ch mean.Increment(dr.ks) vmean.Increment(dr.vard) } ksarray = append(ksarray, mean.GetResult()) vdarray = append(vdarray, vmean.GetResult()) ngarray = append(ngarray, float64(pop.NumOfGen)) } // draw svger := render.NewSVG(fname, 1, 2, 800, 200) pl := chart.ScatterChart{Title: "KS"} pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "VarD"} pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() }
func campaign() { x := []float64{0, 1, 2, 3, 4, 5, 6} const plotStyle = chart.PlotStyleLines const lineStyle = chart.SolidLine dumper := NewDumper("scatter", 2, 1, 450, 400) defer dumper.Close() TTFBLow := []float64{40, 55, 40, 55, 122, 145, 166} // Categorized Bar Chart c := chart.ScatterChart{} c.YRange.Label = "Pro-EU" c.XRange.Label = "Succesfuld karriere" c.XRange.Category = months // Et klogt dyr bider ej hånden som føder den. // Magt fører mere magt med sig. Stå i opposition til magten, og magten har ej brug for dig. c.AddDataPair("Politiker", x, TTFBLow, plotStyle, chart.Style{LineColor: turquise, LineStyle: chart.LongDashLine}) dumper.Plot(&c) // Du tjener systemet. Systemet belønner dig. Skønt tusind år forgår, forbliver menneskelig natur altid den samme. // //Det er lettere at gå i medvind, en at gå imod vinden og den herskende ånd. // Den der går imod vinden, går fattig, udskældt og ene. TTFBFileHigh20 := []float64{160, 135, 177, 130, 150, 250, 260} // Categorized Bar Chart c = chart.ScatterChart{} c.YRange.Label = "Idealisme" c.XRange.Label = "Karrierens længde" c.XRange.Category = months c.AddDataPair("Arbejdsløs", x, TTFBFileHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: red, LineStyle: lineStyle}) dumper.Plot(&c) }
func main() { // ks file ksfilestr := dir + "/" + prex + "_ks.txt" ksfile, err := os.Create(ksfilestr) if err != nil { log.Fatalf("Can not create file: %s, %v", ksfilestr, err) } defer ksfile.Close() ksMean := desc.NewMean() ksVar := desc.NewVarianceWithBiasCorrection() vardMean := desc.NewMean() vardVar := desc.NewVarianceWithBiasCorrection() scovsMeanArray := make([]*desc.Mean, maxL) scovsVarArray := make([]*desc.Variance, maxL) rcovsMeanArray := make([]*desc.Mean, maxL) rcovsVarArray := make([]*desc.Variance, maxL) xyPLMeanArray := make([]*desc.Mean, maxL) xyPLVarArray := make([]*desc.Variance, maxL) xsysPLMeanArray := make([]*desc.Mean, maxL) xsysPLVarArray := make([]*desc.Variance, maxL) smXYPLMeanArray := make([]*desc.Mean, maxL) smXYPLVarArray := make([]*desc.Variance, maxL) for i := 0; i < maxL; i++ { scovsMeanArray[i] = desc.NewMean() scovsVarArray[i] = desc.NewVarianceWithBiasCorrection() rcovsMeanArray[i] = desc.NewMean() rcovsVarArray[i] = desc.NewVarianceWithBiasCorrection() xyPLMeanArray[i] = desc.NewMean() xyPLVarArray[i] = desc.NewVarianceWithBiasCorrection() xsysPLMeanArray[i] = desc.NewMean() xsysPLVarArray[i] = desc.NewVarianceWithBiasCorrection() smXYPLMeanArray[i] = desc.NewMean() smXYPLVarArray[i] = desc.NewVarianceWithBiasCorrection() } stepNum := etime / stepSize for i := 0; i < stepNum; i++ { // simulate pop.Evolve(stepSize) // create distance matrix dmatrix := pop.DistanceMatrix(sampleSize) c := covs.NewCMatrix(dmatrix, sampleSize, pop.Length) // calculate ks ks := c.CalculateKS() vard := c.CalculateVarD() ksfile.WriteString(fmt.Sprintf("%d\t%g\t%g\n", pop.Time, ks, vard)) ksMean.Increment(ks) ksVar.Increment(ks) vardMean.Increment(vard) vardVar.Increment(vard) log.Printf("KsMean: %g, KsVar: %g, VardMean: %g, VardVar: %g\n", ksMean.GetResult(), ksVar.GetResult(), vardMean.GetResult(), vardVar.GetResult()) // calculate covs scovs, rcovs, xyPL, xsysPL, smXYPL := c.CalculateCovs(maxL) for j := 0; j < maxL; j++ { scovsMeanArray[j].Increment(scovs[j]) scovsVarArray[j].Increment(scovs[j]) rcovsMeanArray[j].Increment(rcovs[j]) rcovsVarArray[j].Increment(rcovs[j]) xyPLMeanArray[j].Increment(xyPL[j]) xyPLVarArray[j].Increment(xyPL[j]) xsysPLMeanArray[j].Increment(xsysPL[j]) xsysPLVarArray[j].Increment(xsysPL[j]) smXYPLMeanArray[j].Increment(smXYPL[j]) smXYPLVarArray[j].Increment(smXYPL[j]) } svger := render.NewSVG(dir+"/"+prex+"_covs", 1, 5, 800, 200) scovMeans := make([]float64, maxL-1) rcovMeans := make([]float64, maxL-1) xyPLMeans := make([]float64, maxL-1) xsysPLMeans := make([]float64, maxL-1) smXYPLMeans := make([]float64, maxL-1) xs := make([]float64, maxL-1) for j := 1; j < maxL; j++ { xs[j-1] = float64(j) scovMeans[j-1] = scovsMeanArray[j].GetResult() rcovMeans[j-1] = rcovsMeanArray[j].GetResult() xyPLMeans[j-1] = xyPLMeanArray[j].GetResult() xsysPLMeans[j-1] = xsysPLMeanArray[j].GetResult() smXYPLMeans[j-1] = smXYPLMeanArray[j].GetResult() } pl := chart.ScatterChart{Title: "scovs"} pl.AddDataPair("struc covs", xs, scovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "rcovs"} pl.AddDataPair("rate covs", xs, rcovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "xyPL"} pl.AddDataPair("xyPL", xs, xyPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "xsysPL"} pl.AddDataPair("xsysPL", xs, xsysPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "smXYPL"} pl.AddDataPair("smXYPL", xs, smXYPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() // write to file covsfilestr := dir + "/" + prex + "_covs.txt" covsfile, err := os.Create(covsfilestr) if err != nil { log.Fatalf("Can not create file: %s, %v", covsfilestr, err) } for j := 0; j < maxL; j++ { covsfile.WriteString( fmt.Sprintf("%d\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n", j, scovsMeanArray[j].GetResult(), scovsVarArray[j].GetResult(), rcovsMeanArray[j].GetResult(), rcovsVarArray[j].GetResult(), xyPLMeanArray[j].GetResult(), xyPLVarArray[j].GetResult(), xsysPLMeanArray[j].GetResult(), xsysPLVarArray[j].GetResult(), smXYPLMeanArray[j].GetResult(), smXYPLVarArray[j].GetResult(), ), ) } covsfile.Close() } tnow = time.Now() log.Printf("Done at %v\n", tnow) }
func Scatter() { x := []float64{0, 1, 2, 3, 4, 5, 6} const plotStyle = chart.PlotStyleLines const lineStyle = chart.SolidLine dumper := NewDumper("scatter", 2, 1, 450, 400) defer dumper.Close() ping := []float64{1, 3, 5, 9, 22, 1, 56} TTFBLow := []float64{40, 55, 40, 55, 122, 145, 166} TTFBMed := []float64{89, 77, 102, 95, 232, 333, 254} TTFBFile := []float64{133, 55, 77, 20, 99, 102, 69} // Categorized Bar Chart c := chart.ScatterChart{} c.XRange.Label = "Måned" c.YRange.Label = "MS" c.XRange.Category = months c.AddDataPair("TTFB-Med", x, TTFBMed, plotStyle, chart.Style{LineColor: green, LineStyle: lineStyle}) c.AddDataPair("TTFB-Low", x, TTFBLow, plotStyle, chart.Style{LineColor: turquise, LineStyle: chart.LongDashLine}) c.AddDataPair("TTFB-File", x, TTFBFile, plotStyle, chart.Style{LineColor: yellow, LineStyle: chart.LongDashLine}) c.AddDataPair("Ping", x, ping, plotStyle, chart.Style{LineColor: brown, LineStyle: lineStyle}) dumper.Plot(&c) TTFBHigh20 := []float64{200, 300, 900, 1500, 1600, 1300} TTFBHigh20HotHour := []float64{250, 350, 1150, 1900, 1800, 1500} TTFBFileHigh20 := []float64{160, 135, 177, 130, 150, 250, 260} TTFBFileHigh20HotHour := []float64{360, 235, 277, 190, 180, 270, 290} // Categorized Bar Chart c = chart.ScatterChart{} c.XRange.Label = "Måned" c.YRange.Label = "MS" c.XRange.Category = months c.AddDataPair("TTFB-High", x, TTFBHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: green, LineStyle: lineStyle}) c.AddDataPair(" - kl. 17-21", x, TTFBHigh20HotHour, plotStyle, chart.Style{ Symbol: '#', LineColor: violet, LineStyle: chart.LongDashLine}) c.AddDataPair("TTFB-FileHigh", x, TTFBFileHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: red, LineStyle: lineStyle}) c.AddDataPair(" - kl. 17-21", x, TTFBFileHigh20HotHour, plotStyle, chart.Style{Symbol: '#', LineColor: blue, LineStyle: chart.LongDashLine}) dumper.Plot(&c) }