123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace OTSExtremum
- {
- public partial class GridForm : Form
- {
- double[] vs;
- List<double[]> allData;
- List<double[]> LmaxData;
- double[] ret= new double[3];
- string title;
- public GridForm(double[] p,string text)
- {
- vs =p ;
- title = text;
- InitializeComponent();
- }
- private void GridForm_Load(object sender, EventArgs e)
- {
- this.Text = title + "统计参数";
- Calculation();
- }
- private void Calculation()
- {
- allData = new List<double[]>();
-
- int N = vs.Length;
- if (N > 0)
- {
- Array.Sort(vs);
- double mean = Business.Tools.Mean(vs);
- double sdev = Business.Tools.StDev(vs);
- double delta = sdev * Math.Sqrt(6) / 3.1416;//δ
- double lambda = mean - 0.5772 * delta;//λ
- double sumLL = 0;
- ret = Business.Tools.OPTMax(vs);
- double[] C = new double[N];
- double[] D = new double[N];
- double[] E = new double[N];
- double[] F = new double[N];
- double[] G = new double[N];
- double[] H = new double[N];
- double[] I = new double[N];
-
- for (int i = 0; i < N; i++)
- {
- double temp = i+1;
- double t = 1;
- C[i] = temp / (N + 1);
- D[i] = -1 * Math.Log(-1 * Math.Log(C[i]));
- //F[i] = (t / delta) * (Math.Exp((lambda - vs[i]) / delta)) * Math.Exp(-1 * Math.Exp((lambda - vs[i]) / delta));
- // E[i] = Math.Log(F[i]);
- F[i] = Math.Log(t / delta) - (vs[i] - lambda) / delta - Math.Exp((lambda - vs[i]) / delta);
- E[i] = Math.Log(t / ret[0]) - (vs[i] - ret[1]) / ret[0] - Math.Exp((ret[1] - vs[i]) / ret[0]);
- G[i] = ret[0] * D[i] + ret[1];
- double se = ret[0] * (Math.Sqrt((1.109 + 0.514 * D[i] + 0.608 * D[i] * D[i]) / N));
- H[i] = G[i] - 2 * se;
- I[i] = G[i] + 2 * se;
- sumLL += E[i];
- // qq.Add(new double[]{ D[i], vs[i]});
- }
-
- allData.Add(D);
- allData.Add(vs);
- allData.Add(G);
- allData.Add(H);
- allData.Add(I);
- //string data = JsonConvert.SerializeObject(qq);
- //先设置一下头的高度,否则会太矮不好看
- dtgrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
- dtgrid.ColumnHeadersHeight = 40;
- dtgrid.ColumnHeadersDefaultCellStyle.Font= new Font("微软雅黑", 12, FontStyle.Regular);
- dtgrid.ColumnHeadersDefaultCellStyle.BackColor= Color.WhiteSmoke;
- #region 第一行
- int add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "Mean";
- dtgrid.Rows[add_rowindex].Cells[1].Value = Math.Round(mean, 3).ToString();
- dtgrid.Rows[add_rowindex].Cells[2].Value = "";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "δmom";
- dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(delta, 3).ToString();
- dtgrid.Rows[add_rowindex].Cells[5].Value = "";
- dtgrid.Rows[add_rowindex].Cells[6].Value = "δml";
- dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(ret[0], 3).ToString() ;
- //dtgrid.Rows[add_rowindex].DefaultCellStyle.Font = new Font("微软雅黑", 12, FontStyle.Regular);
- #endregion
- #region 第二行
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "Sdev";
- dtgrid.Rows[add_rowindex].Cells[1].Value = Math.Round(sdev, 3).ToString();
- dtgrid.Rows[add_rowindex].Cells[2].Value = "";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "λmom";
- dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(lambda, 3).ToString();
- dtgrid.Rows[add_rowindex].Cells[5].Value = "";
- dtgrid.Rows[add_rowindex].Cells[6].Value = "λml";
- dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(ret[1], 3).ToString();
- #endregion
- #region 第三行
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "Length";
- dtgrid.Rows[add_rowindex].Cells[1].Value = "";
- dtgrid.Rows[add_rowindex].Cells[2].Value = "";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "Red.Var";
-
- #endregion
- #region 第四行
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "(Y)";
- dtgrid.Rows[add_rowindex].Cells[1].Value = "Rank";
- dtgrid.Rows[add_rowindex].Cells[2].Value = "Prob";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "(X)";
- dtgrid.Rows[add_rowindex].Cells[4].Value = "ln";
- dtgrid.Rows[add_rowindex].Cells[5].Value = "";
- dtgrid.Rows[add_rowindex].Cells[6].Value = "X";
- dtgrid.Rows[add_rowindex].Cells[7].Value = "Xlow";
- dtgrid.Rows[add_rowindex].Cells[8].Value = "Xhig";
- #endregion
- #region 第五行
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "Data";
- dtgrid.Rows[add_rowindex].Cells[1].Value = "";
- dtgrid.Rows[add_rowindex].Cells[2].Value = "";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "RV";
- dtgrid.Rows[add_rowindex].Cells[4].Value = "(f(xi.δ.λ)";
- #endregion
-
-
- dtgrid.Rows[4].Height = 40;
- dtgrid.Rows[4].DefaultCellStyle.Font = new Font("微软雅黑", 12, FontStyle.Regular);
- for (int j = 0; j < 9; j++)
- {
-
- dtgrid.Rows[4].Cells[j].Style.BackColor = Color.WhiteSmoke;
- }
-
- for (int i = 0; i < N; i++)
- {
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = vs[i].ToString();
- dtgrid.Rows[add_rowindex].Cells[1].Value = (i+1).ToString();
- dtgrid.Rows[add_rowindex].Cells[2].Value = C[i].ToString();
- dtgrid.Rows[add_rowindex].Cells[3].Value =Math.Round(D[i],5).ToString();
- dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(E[i], 5).ToString();
- dtgrid.Rows[add_rowindex].Cells[5].Value = Math.Round(F[i], 5).ToString();
- dtgrid.Rows[add_rowindex].Cells[6].Value = Math.Round(G[i], 2).ToString();
- dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(H[i], 1).ToString();
- dtgrid.Rows[add_rowindex].Cells[8].Value = Math.Round(I[i], 1).ToString();
- }
- add_rowindex = dtgrid.Rows.Add();
- dtgrid.Rows[add_rowindex].Cells[0].Value = "";
- dtgrid.Rows[add_rowindex].Cells[1].Value = "";
- dtgrid.Rows[add_rowindex].Cells[2].Value = "";
- dtgrid.Rows[add_rowindex].Cells[3].Value = "";
- dtgrid.Rows[add_rowindex].Cells[4].Value = "SUM(LL)";
- dtgrid.Rows[add_rowindex].Cells[5].Value = Math.Round(sumLL, 3).ToString();
- dtgrid.Rows[add_rowindex].Cells[6].Value = "";
- dtgrid.Rows[add_rowindex].Cells[7].Value = "";
- dtgrid.Rows[add_rowindex].Cells[8].Value = "";
- #region 极值分析
- LmaxData = new List<double[]>();
- double[] maxT = { 1.01, 1.11, 2, 20, 50, 100, 1000, 10000, 100000, 1000000, 10000000 };
- double[] maxP = { 1, 10, 50, 95, 98.0, 99, 99.9, 99.99, 99.999, 99.9999, 99.99999 };
- LmaxData.Add(maxT);
- LmaxData.Add(maxP);
- double[] maxy = new double[maxP.Length];
- double[] Lmax = new double[maxP.Length];
- for (int i = 0; i < maxP.Length; i++)
- {
- maxy[i] = -1 * Math.Log(-1 * Math.Log(maxP[i]*0.01));
- Lmax[i] = ret[0] * maxy[i] + ret[1];
- }
- LmaxData.Add(maxy);
- LmaxData.Add(Lmax);
- #endregion
- }
- }
-
- private void btnChart_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < allData.Count; i++)
- {
- for (int j = 0; j < allData[i].Length; j++)
- {
- if (double.IsNaN(allData[i][j]) || double.IsInfinity(allData[i][j]))
- {
- MessageBox.Show("数据不正常,会造成溢出");
- return;
- }
- }
- }
- ChartSherp chart = new ChartSherp(allData, ret[0], ret[1]);
- chart.Show();
- }
- private void btnLmax_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < allData.Count; i++)
- {
- for (int j = 0; j < allData[i].Length; j++)
- {
- if (double.IsNaN(allData[i][j]) || double.IsInfinity(allData[i][j]))
- {
- MessageBox.Show("数据不正常,会造成溢出");
- return;
- }
- }
- }
- LmaxForm lmaxForm = new LmaxForm(LmaxData);
- lmaxForm.Show();
- }
- public static void ExportToExcel(DataGridView dataGrid, string path)
- {
-
- StreamWriter sw = null;
- try
- {
- long totalCount = dataGrid.Rows.Count;
- sw = new StreamWriter(path, false, Encoding.Unicode);
- StringBuilder sb = new StringBuilder();
- //for (int i = 0; i < dataGrid.Columns.Count; i++)
- //{
- // sb.Append(dataGrid.Columns[i].HeaderText + "\t");
- //}
- //sb.Append(Environment.NewLine);
- for (int i = 0; i < dataGrid.Rows.Count; i++)
- {
- for (int j = 0; j < dataGrid.Columns.Count; j++)
- {
- sb.Append(dataGrid.Rows[i].Cells[j].Value==null?"": dataGrid.Rows[i].Cells[j].Value + "\t");
- }
- sb.Append(Environment.NewLine);
- }
- sw.Write(sb.ToString());
- sw.Flush();
- MessageBox.Show("导出完成!");
- }
- catch (IOException ioe)
- {
- throw ioe;
- }
- finally
- {
- if (sw != null)
- {
- sw.Close();
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
- dialog.Description = "请选择保存地址";
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- ExportToExcel(dtgrid,dialog.SelectedPath+"//"+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls");
- }
-
- }
- }
- }
|