| | |
| | | this.data = data; |
| | | |
| | | double[] xlist = data.Select(p => (double)p.X).ToArray(); |
| | | coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2); |
| | | isFitted = true; |
| | | if (data.Count>2) |
| | | { |
| | | coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2); |
| | | isFitted = true; |
| | | } |
| | | else if(data.Count==2) |
| | | { |
| | | //使用两个点的线性拟合 |
| | | var tuple = Fit.Line(xlist, data.Select(p => (double)p.Y).ToArray()); |
| | | coefficients= new DenseVector(new double[] { tuple.Item1, tuple.Item2 }); |
| | | isFitted = true; |
| | | } |
| | | else if (data.Count==1) |
| | | { |
| | | coefficients = new DenseVector(new double[] { data[0].Y }); |
| | | isFitted = true; |
| | | } |
| | | else |
| | | { |
| | | coefficients = new DenseVector(new double[] { 0}); |
| | | isFitted = true; |
| | | } |
| | | |
| | | var list = xlist.ToList(); |
| | | xMax = list.Max(); |
| | | xMin = list.Min(); |
| | |
| | | |
| | | public double Evaluate(double x) |
| | | { |
| | | while (!isFitted) ; |
| | | if (!isFitted) return double.NaN; |
| | | double y = 0; |
| | | for (int i = 0; i < coefficients.Count; i++) |
| | | { |
| | |
| | | public List<PointF> GetFitCurve(int count) |
| | | { |
| | | List<PointF> curve = new List<PointF>(); |
| | | for (double x = xMin; x < xMax; x += (xMax - xMin) / count) |
| | | |
| | | for (double x = xMin; x <= xMax && curve.Count<count; x += (xMax - xMin) / count) |
| | | { |
| | | double y = Evaluate(x); |
| | | curve.Add(new PointF((float)x, (float)y)); |
| | |
| | | /// <param name="numIntervals"></param> |
| | | /// <param name="filterNum">以最小值作为过滤的系数</param> |
| | | /// <returns></returns> |
| | | public static List<DataPoint> GetFrequencyDistribution(List<double> data, out List<double>[] datapoints, int numIntervals = 10,double filterNum=5 ) |
| | | public static List<DataPoint> GetFrequencyDistribution(List<Result> data, out List<DataPoint>[] datapoints, int numIntervals = 10,double filterNum=5 ) |
| | | { |
| | | |
| | | List<DataPoint> resultPoints = new List<DataPoint>(); |
| | | datapoints= new List<double>[numIntervals]; |
| | | datapoints= new List<DataPoint>[numIntervals]; |
| | | for(int i=0;i<numIntervals;i++) |
| | | { |
| | | datapoints[i]= new List<double>(); |
| | | datapoints[i]= new List<DataPoint>(); |
| | | } |
| | | // 设置置信水平 |
| | | |
| | |
| | | |
| | | |
| | | // 计算数据的最小值和最大值 |
| | | double minValue = data.Min(); |
| | | double maxValue = data.Max(); |
| | | double minValue = data.Min(x=>x.ObjFunctionValue); |
| | | double maxValue = data.Max(x => x.ObjFunctionValue); |
| | | |
| | | // 计算区间宽度 |
| | | double intervalWidth = (maxValue - minValue) / numIntervals; |
| | |
| | | int[] freqDist = new int[numIntervals]; |
| | | |
| | | // 计算各个区间的频率 |
| | | foreach (double x in data) |
| | | foreach (var xd in data) |
| | | { |
| | | var x = xd.ObjFunctionValue; |
| | | int index = (int)((x - minValue) / intervalWidth); |
| | | if (index < 0) // 数据小于最小值 |
| | | { |
| | | freqDist[0]++; |
| | | datapoints[0].Add(x); |
| | | datapoints[0].Add(new DataPoint() {XValue= x,Tag=xd }); |
| | | } |
| | | else if (index >= numIntervals) // 数据大于最大值 |
| | | { |
| | | freqDist[numIntervals - 1]++; |
| | | datapoints[numIntervals - 1].Add(x); |
| | | datapoints[numIntervals - 1].Add(new DataPoint() { XValue = x, Tag = xd }); |
| | | } |
| | | else // 数据在区间内 |
| | | { |
| | | freqDist[index]++; |
| | | datapoints[index].Add(x); |
| | | datapoints[index].Add(new DataPoint() { XValue = x, Tag = xd }); |
| | | } |
| | | } |
| | | double[] freqDistPersent =new double[numIntervals]; |