コード例 #1
0
 public QuadraticLinearRegression(double[] XDATA, double[] YDATA)
 {
     try
     {
         if ((XDATA.Length < 2 || YDATA.Length < 2) || (XDATA.Length != YDATA.Length))
         {
             throw new ArgumentOutOfRangeException("Exponential fit can't work with less then 2 points or unequal matrices");
         }
         if (XDATA.Length != YDATA.Length)
         {
             throw new ArgumentException("Arrays of unequal size were passed to the quadratic fitter");
         }
         //deep copy the data to protect its integrity
         y = YDATA.ToArray();
         y = (from b in y select Math.Log(b)).ToArray();
         x = XDATA.ToArray();
         DoubleArray dx   = DoubleArray.From(XDATA);
         DoubleArray dy   = DoubleArray.From(y);
         var         x2   = dx.ElementMultiply(dx);
         var         xmat = DoubleArray.VertStack(dx, x2);
         xmat               = xmat.T;
         r                  = new Regress(dy, xmat);
         this.name          = "ExpDecreasing";
         pParameters        = r.Beta;
         this.SuccessfulFit = true;
     }
     catch
     {
         this.SuccessfulFit = false;
     }
 }
コード例 #2
0
        public static void Run()
        {
            int  yearsToAggregate = 300;
            bool expectedUtility  = false;

            double[]      prtps = { 0.001, 0.01, 0.03 };
            double[]      etas  = { 1.0, 1.5, 2.0 };
            WelfareType[] swfs  = { WelfareType.Global, WelfareType.Utilitarian, WelfareType.Regional, WelfareType.Tol, WelfareType.Pearce };

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            Console.WriteLine("Computing base CpC");

            var baseCpC = Compute2010CpC();

            Console.WriteLine("Doing a run with monteCarloRuns={0}, yearsToAggregate={1}",
                              monteCarloRuns,
                              yearsToAggregate);

            var runConfigs = (from prtp in prtps
                              from eta in etas
                              from swf in swfs
                              select new RunConfig()
            {
                Prtp = prtp,
                Eta = eta,
                SWF = swf
            }).ToArray();

            string[] regions = new string[16];

            var parameterDefinition = new Parameters();

            parameterDefinition.ReadExcelFile(@"Data\Parameter - base.xlsm");

            // Run model once to prep for multi tasking
            {
                var m = new Esmf.Model.ModelTyped <FundWorkflow>();
                m.Run(parameterDefinition.GetBestGuess());
            }

            var relevantKeys = new List <ParameterElementKey>();

            foreach (var p in parameterDefinition.GetElements())
            {
                if (p is ParameterElement <double> )
                {
                    if (!(p is ParameterElementConstant <double>))
                    {
                        relevantKeys.Add(p.Key);
                    }
                }
            }

            var yValues                   = new DoubleArray[runConfigs.Length, 16];
            var correlations              = new Dictionary <ParameterElementKey, double> [runConfigs.Length, 16];
            var regressions               = new Dictionary <ParameterElementKey, double> [runConfigs.Length, 16];
            var regressionsConfIntervLow  = new Dictionary <ParameterElementKey, double> [runConfigs.Length, 16];
            var regressionsConfIntervHigh = new Dictionary <ParameterElementKey, double> [runConfigs.Length, 16];
            var regressionsFStat          = new DoubleArray(runConfigs.Length, 16);
            var regressionsPVal           = new DoubleArray(runConfigs.Length, 16);
            var regressionsRsq            = new DoubleArray(runConfigs.Length, 16);
            var xValues                   = new DoubleArray(monteCarloRuns, relevantKeys.Count);
            var standardizedXValues       = new DoubleArray(monteCarloRuns, relevantKeys.Count);
            var standardizedYValues       = new DoubleArray[runConfigs.Length, 16];

            for (int i = 0; i < runConfigs.Length; i++)
            {
                for (int l = 0; l < 16; l++)
                {
                    yValues[i, l]                   = new DoubleArray(monteCarloRuns, 1);
                    standardizedYValues[i, l]       = new DoubleArray(monteCarloRuns);
                    correlations[i, l]              = new Dictionary <ParameterElementKey, double>();
                    regressions[i, l]               = new Dictionary <ParameterElementKey, double>();
                    regressionsConfIntervHigh[i, l] = new Dictionary <ParameterElementKey, double>();
                    regressionsConfIntervLow[i, l]  = new Dictionary <ParameterElementKey, double>();
                }
            }

            var xMeans = new DoubleArray(relevantKeys.Count);
            var xStd   = new DoubleArray(relevantKeys.Count);

            var rand = new MersenneTwister();

            int currentRun = 0;

            {
                var m = new MarginalDamage2()
                {
                    EmissionYear         = Timestep.FromYear(2010),
                    Gas                  = MarginalGas.C,
                    Parameters           = parameterDefinition.GetBestGuess(),
                    YearsToAggregate     = yearsToAggregate,
                    GlobalCpCAtBase      = baseCpC.Item1,
                    ExpectedUtilityMode  = expectedUtility,
                    AdditionalInitMethod = (Esmf.Model.Model fw) =>
                    {
                        fw["scenariouncertainty"].Parameters["timeofuncertaintystart"].SetValue(Timestep.FromYear(2010));
                    }
                };

                for (int i = 0; i < 16; i++)
                {
                    m.RegionalCpCAtBase[i] = baseCpC.Item2[i];
                }

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    m.SWFs.Add(new WelfareSpec(runConfigs[i].SWF, runConfigs[i].Prtp, runConfigs[i].Eta));
                }

                m.Start();
            }

            Parallel.ForEach(
                parameterDefinition.GetRandom(rand, monteCarloRuns),
                () =>
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                Thread.CurrentThread.Priority       = ThreadPriority.BelowNormal;
                return(0);
            },
                (pv, pls, dummy) =>
            {
                int tempCurrentCount = Interlocked.Increment(ref currentRun);
                Console.Write("\rRun {0}                ", tempCurrentCount);

                var m = new MarginalDamage2()
                {
                    EmissionYear         = Timestep.FromYear(2010),
                    Gas                  = MarginalGas.C,
                    Parameters           = pv,
                    YearsToAggregate     = yearsToAggregate,
                    GlobalCpCAtBase      = baseCpC.Item1,
                    ExpectedUtilityMode  = false,
                    AdditionalInitMethod = (Esmf.Model.Model fw) =>
                    {
                        fw["scenariouncertainty"].Parameters["timeofuncertaintystart"].SetValue(Timestep.FromYear(2010));
                    }
                };

                for (int i = 0; i < 16; i++)
                {
                    m.RegionalCpCAtBase[i] = baseCpC.Item2[i];
                }

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    m.SWFs.Add(new WelfareSpec(runConfigs[i].SWF, runConfigs[i].Prtp, runConfigs[i].Eta));
                }

                m.Start();

                var dimensions = m.Result1.Dimensions;

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    foreach (var r in dimensions.GetValues <Region>())
                    {
                        regions[r.Index] = r.ToString();
                        yValues[i, r.Index][(int)pv.RunId - 1] = m.SCCs[i][r];
                    }
                }

                for (int l = 0; l < relevantKeys.Count; l++)
                {
                    var p      = relevantKeys[l];
                    double val = ((ParameterValueElement <double>)pv.GetElementByKey(p)).Value;

                    xValues[pv.RunId.Value - 1, l] = val;
                }

                return(0);
            },
                (dummy) => { });

            Console.WriteLine();
            Console.WriteLine();

            if (!Directory.Exists("Output"))
            {
                Directory.CreateDirectory("Output");
            }

            Console.WriteLine("Write summary");

            // Output summary
            using (var f = File.CreateText(@"Output\summary.csv"))
            {
                f.WriteLine("Prtp;Eta;Swf;SccRegion;Mean;Variance;StdDev;StandardError");

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        double standardError = yValues[i, l].Std() / Math.Sqrt(monteCarloRuns);
                        f.WriteLine("{0};{1};{2:f3};{3:f3};{4:f3};{5:f3}",
                                    runConfigs[i],
                                    regions[l],
                                    yValues[i, l].Mean(),
                                    yValues[i, l].Var(),
                                    yValues[i, l].Std(),
                                    standardError);
                    }
                }
            }

            Console.WriteLine("Compute correlations");

            // Compute correlations
            for (int k = 0; k < relevantKeys.Count; k++)
            {
                var xSlice = xValues.GetCol(k);

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        // Compute correlation between parameter i and SCC
                        var    pearson = new Pearson(yValues[i, l], xSlice);
                        double corr    = (double)pearson.Rho;
                        correlations[i, l].Add(relevantKeys[k], corr);
                    }
                }
            }

            Console.WriteLine("Compute regressions");

            // Standardize everything
            for (int k = 0; k < relevantKeys.Count; k++)
            {
                var xSlice = xValues.GetCol(k);

                xMeans[k] = xSlice.Mean();
                xStd[k]   = xSlice.Std();
            }

            for (int k = 0; k < monteCarloRuns; k++)
            {
                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        standardizedYValues[i, l][k] = (yValues[i, l][k] - yValues[i, l].Mean()) / yValues[i, l].Std();
                    }
                }

                for (int l = 0; l < relevantKeys.Count; l++)
                {
                    standardizedXValues[k, l] = (xValues[k, l] - xMeans[l]) / xStd[l];
                }
            }

            // Compute regression
            for (int i = 0; i < runConfigs.Length; i++)
            {
                for (int l = 0; l < 16; l++)
                {
                    var regress = new Regress(standardizedYValues[i, l], standardizedXValues, 0.1);

                    regressionsFStat[i, l] = regress.FStat;
                    regressionsPVal[i, l]  = regress.PVal;
                    regressionsRsq[i, l]   = regress.Rsq;

                    for (int k = 0; k < relevantKeys.Count; k++)
                    {
                        regressions[i, l].Add(relevantKeys[k], regress.Beta[k + 1]);

                        regressionsConfIntervLow[i, l].Add(relevantKeys[k], regress.BetaInt[k + 1, 0]);
                        regressionsConfIntervHigh[i, l].Add(relevantKeys[k], regress.BetaInt[k + 1, 1]);
                    }
                }
            }

            Console.WriteLine("Write regression summary");

            // Write regression summaries
            using (var f = File.CreateText(@"Output\regression summary.csv"))
            {
                f.WriteLine("Prtp;Eta;Swf;SccRegion;FStat;PVal;Rsq");

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        f.WriteLine("{0};{1};{2:f15};{3:f15};{4:f15}", runConfigs[i], regions[l], regressionsFStat[i, l], regressionsPVal[i, l], regressionsRsq[i, l]);
                    }
                }
            }

            Console.WriteLine("Write correlation");

            // Write correlation
            using (var f = File.CreateText(@"Output\correlation.csv"))
            {
                f.WriteLine("Prtp;Eta;Swf;SccRegion;Name;Correlation;RegressCoefficient;RegressConfIntLower;RegressConfIntUpper");

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        foreach (var key in relevantKeys)
                        {
                            string s = key.Name;
                            if (key is ParameterElementKey1Dimensional)
                            {
                                s += "-" + regions[((ParameterElementKey1Dimensional)key).D1];
                            }
                            else if (key is ParameterElementKey2Dimensional)
                            {
                                s += "-" + regions[((ParameterElementKey2Dimensional)key).D1] + "-" + regions[((ParameterElementKey2Dimensional)key).D2];
                            }

                            f.WriteLine("{0};{1};\"{2}\";{3:f10};{4:f10};{5:f10};{6:f10}", runConfigs[i], regions[l], s, correlations[i, l][key], regressions[i, l][key], regressionsConfIntervLow[i, l][key], regressionsConfIntervHigh[i, l][key]);
                        }
                    }
                }
            }

            Console.WriteLine("Write SCC values");

            using (var f = File.CreateText(@"Output\scc values.csv"))
            {
                f.WriteLine("Prtp;Eta;Swf;SccRegion;RundId;Scc");

                for (int i = 0; i < runConfigs.Length; i++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        for (int k = 0; k < monteCarloRuns; k++)
                        {
                            double standardError = yValues[i, l].Std() / Math.Sqrt(monteCarloRuns);
                            f.WriteLine("{0};{1};{2};{3:f14}",
                                        runConfigs[i],
                                        regions[l],
                                        k,
                                        yValues[i, l][k]);
                        }
                    }
                }
            }

            stopwatch.Stop();

            Console.WriteLine(stopwatch.Elapsed);
        }
コード例 #3
0
        // 根据单调性对栅格打分
        private Result monotoneLoc()
        {
            int agMaxX = 0, agMaxY = 0, agMinX = 0, agMinY = 0, agZ = 0;

            GridHelper.getInstance().XYZToAccGrid(leftBound, downBound, 0, ref agMinX, ref agMinY, ref agZ);
            GridHelper.getInstance().XYZToAccGrid(rightBound, upBound, 0, ref agMaxX, ref agMaxY, ref agZ);

            Dictionary <string, int> dicMon = new Dictionary <string, int>();
            Regress regress1 = new Regress();

            for (int div = 0; div < roadDivide.Count - 1; div++)
            {
                List <double> pwrDbmSub = new List <double>();
                List <double> x         = new List <double>();

                x.Add(0);
                for (int j = roadDivide[div]; j < roadDivide[div + 1]; j++)
                {
                    pwrDbmSub.Add(pwrDbm[j]);
                    x.Add(j - roadDivide[div] + 1);
                }

                #region 提取信号强度突变点
                Divide divide = new Divide(100, 0.8, ref pwrDbmSub);
                divide.run();
                #endregion

                #region 对每段进行线性拟合

                for (int i = 0; i < divide.posV.Count - 1; i++)
                {
                    int id1 = divide.posV[i];
                    int id2 = divide.posV[i + 1] - 1;
                    if (id2 < 0)
                    {
                        id2 = 0;
                    }

                    double a2 = 0, b2 = 0, err2 = 0;
                    regress1.CalcRegress(ref x, divide.m, id1, id2, out a2, out b2, out err2); // y = ax + b

                    double x1, y1, x2, y2;                                                     // (x1, y1) 单调上升的尽头
                    if (a2 < 0)
                    {
                        x1 = gx[id1 + roadDivide[div]];
                        y1 = gy[id1 + roadDivide[div]];
                        x2 = gx[id2 + roadDivide[div]];
                        y2 = gy[id2 + roadDivide[div]];
                    }
                    else
                    {
                        x1 = gx[id2 + roadDivide[div]];
                        y1 = gy[id2 + roadDivide[div]];
                        x2 = gx[id1 + roadDivide[div]];
                        y2 = gy[id1 + roadDivide[div]];
                    }

                    double k     = (x1 - x2) / (y2 - y1);
                    double b     = y1 - (x1 - x2) / (y2 - y1) * x1;
                    double y2Tmp = k * x2 + b;

                    if (y2 < y2Tmp)
                    {
                        for (int tx = agMinX + 1; tx < agMaxX - 1; tx++)
                        {
                            for (int ty = agMinY + 1; ty < agMaxY - 1; ty++)
                            {
                                double cx = 0, cy = 0, cz = 0;
                                GridHelper.getInstance().AccGridToXYZ(tx, ty, 0, ref cx, ref cy, ref cz);
                                if (cy > k * cx + b)
                                {
                                    string key = string.Format("{0},{1}", tx, ty);
                                    if (dicMon.Keys.Contains(key))
                                    {
                                        dicMon[key]++;
                                    }
                                    else
                                    {
                                        dicMon[key] = 0;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int tx = agMinX + 1; tx < agMaxX - 1; tx++)
                        {
                            for (int ty = agMinY + 1; ty < agMaxY - 1; ty++)
                            {
                                double cx = 0, cy = 0, cz = 0;
                                GridHelper.getInstance().AccGridToXYZ(tx, ty, 0, ref cx, ref cy, ref cz);
                                if (cy < k * cx + b)
                                {
                                    string key = string.Format("{0},{1}", tx, ty);
                                    if (dicMon.Keys.Contains(key))
                                    {
                                        dicMon[key]++;
                                    }
                                    else
                                    {
                                        dicMon[key] = 0;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
            }

            // 挑选分数最高的格子
            if (dicMon.Count == 0)
            {
                return(new Result(false, "单调性未推出合理结果"));
            }
            var    maxKeyMon = (from d in dicMon orderby d.Value descending select d.Key).First();
            var    allMaxKeyMon = (from d in dicMon where d.Value == dicMon[maxKeyMon] select d.Key).ToArray();
            double X = 0, Y = 0, Z = 0;

            int cnt = 0;
            if (allMaxKeyMon.Count() > 5)
            {
                foreach (string a in allMaxKeyMon)  // 有多个分数最高的格子
                {
                    string[] num = a.Split(',');
                    GridHelper.getInstance().AccGridToXYZ(Convert.ToInt32(num[0]), Convert.ToInt32(num[1]), 0, ref X, ref Y, ref Z);

                    Pt3D pt = new Pt3D(X, Y, 0);
                    if (inBound(ref pt))
                    {
                        MonotoneResult.Add(pt);
                    }
                }
            }
            else
            {
                var dicSort = from objDic in dicMon orderby objDic.Value descending select objDic;
                foreach (KeyValuePair <string, int> kvp in dicSort)
                {
                    string[] num = kvp.Key.Split(',');
                    GridHelper.getInstance().AccGridToXYZ(Convert.ToInt32(num[0]), Convert.ToInt32(num[1]), 0, ref X, ref Y, ref Z);

                    ++cnt;
                    MonotoneResult.Add(new Pt3D(X, Y, 0));

                    if (cnt > 5)
                    {
                        break;
                    }
                }
            }
            return(new Result(true));
        }
コード例 #4
0
        public void LoadData()
        {
            DataLoad.Run();

            var range = _meltDataMdb
                        .FindMeltRange(MeltNumber)
                        .Select(x => x.ToDouble())
                        .ToArray();

            Tube.Чугун.C  = range[2];
            Tube.Чугун.Si = range[3];
            Tube.Чугун.Mn = range[4];
            Tube.Чугун.P  = range[5];
            Tube.Чугун.S  = range[6];

            Tube.Лом.C  = range[7];
            Tube.Лом.Si = range[8];
            Tube.Лом.Mn = range[9];
            Tube.Лом.P  = range[10];
            Tube.Лом.S  = range[11];

            Tube.Чугун.GEstimated = range[12];
            Tube.Лом.GEstimated   = range[13];

            Tube.ОставленныйШлак.G = range[14] * 1000;
            Tube.МиксерныйШлак.G   = range[15] * 1000;
            Tube.Шлак.GEnd         = range[16] * 1000;

            Tube.Сталь.GYield = range[17] * 1000;
            Tube.Сталь.C      = range[18];
            Tube.Сталь.Si     = range[19];
            Tube.Сталь.Mn     = range[20];
            Tube.Сталь.P      = range[21];
            Tube.Сталь.S      = range[22];

            Tube.Шлак.FeOEnd  = range[23];
            Tube.Шлак.MnOEnd  = range[24];
            Tube.Шлак.P2O5End = range[25];
            Tube.Шлак.BEnd    = range[26];

            Tube.Известь.G   = range[27] * 1000;
            Tube.Известняк.G = range[28] * 1000;

            Tube.Доломит.G        = range[29] * 1000;
            Tube.ВлажныйДоломит.G = range[30] * 1000;
            Tube.Имф.G            = range[31] * 1000;
            Tube.Песок.G          = range[32] * 1000;
            Tube.Кокс.G           = range[33] * 1000;
            Tube.Окатыши.G        = range[34] * 1000;
            Tube.уда.G            = range[35] * 1000;
            Tube.Окалина.G        = range[36] * 1000;
            Tube.Агломерат.G      = range[37] * 1000;
            Tube.Шпат.G           = range[38] * 1000;

            Tube.Дутье.V = range[39];

            Params.AirTemp          = Convert.ToInt32(range[40]) + 273;
            Tube.Чугун.T            = range[41] + 273;
            Tube.Сталь.T            = range[42] + 273;
            Tube.Дутье.VarBlow[0]   = range[43];
            Tube.Лом.DolyaLegkovesa = range[51];

            Regress.Load();
        }