GridForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace OTSExtremum
  12. {
  13. public partial class GridForm : Form
  14. {
  15. double[] vs;
  16. List<double[]> allData;
  17. List<double[]> LmaxData;
  18. double[] ret= new double[3];
  19. string title;
  20. public GridForm(double[] p,string text)
  21. {
  22. vs =p ;
  23. title = text;
  24. InitializeComponent();
  25. }
  26. private void GridForm_Load(object sender, EventArgs e)
  27. {
  28. this.Text = title + "统计参数";
  29. Calculation();
  30. }
  31. private void Calculation()
  32. {
  33. allData = new List<double[]>();
  34. int N = vs.Length;
  35. if (N > 0)
  36. {
  37. Array.Sort(vs);
  38. double mean = Business.Tools.Mean(vs);
  39. double sdev = Business.Tools.StDev(vs);
  40. double delta = sdev * Math.Sqrt(6) / 3.1416;//δ
  41. double lambda = mean - 0.5772 * delta;//λ
  42. double sumLL = 0;
  43. ret = Business.Tools.OPTMax(vs);
  44. double[] C = new double[N];
  45. double[] D = new double[N];
  46. double[] E = new double[N];
  47. double[] F = new double[N];
  48. double[] G = new double[N];
  49. double[] H = new double[N];
  50. double[] I = new double[N];
  51. for (int i = 0; i < N; i++)
  52. {
  53. double temp = i+1;
  54. double t = 1;
  55. C[i] = temp / (N + 1);
  56. D[i] = -1 * Math.Log(-1 * Math.Log(C[i]));
  57. //F[i] = (t / delta) * (Math.Exp((lambda - vs[i]) / delta)) * Math.Exp(-1 * Math.Exp((lambda - vs[i]) / delta));
  58. // E[i] = Math.Log(F[i]);
  59. F[i] = Math.Log(t / delta) - (vs[i] - lambda) / delta - Math.Exp((lambda - vs[i]) / delta);
  60. E[i] = Math.Log(t / ret[0]) - (vs[i] - ret[1]) / ret[0] - Math.Exp((ret[1] - vs[i]) / ret[0]);
  61. G[i] = ret[0] * D[i] + ret[1];
  62. double se = ret[0] * (Math.Sqrt((1.109 + 0.514 * D[i] + 0.608 * D[i] * D[i]) / N));
  63. H[i] = G[i] - 2 * se;
  64. I[i] = G[i] + 2 * se;
  65. sumLL += E[i];
  66. // qq.Add(new double[]{ D[i], vs[i]});
  67. }
  68. allData.Add(D);
  69. allData.Add(vs);
  70. allData.Add(G);
  71. allData.Add(H);
  72. allData.Add(I);
  73. //string data = JsonConvert.SerializeObject(qq);
  74. //先设置一下头的高度,否则会太矮不好看
  75. dtgrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  76. dtgrid.ColumnHeadersHeight = 40;
  77. dtgrid.ColumnHeadersDefaultCellStyle.Font= new Font("微软雅黑", 12, FontStyle.Regular);
  78. dtgrid.ColumnHeadersDefaultCellStyle.BackColor= Color.WhiteSmoke;
  79. #region 第一行
  80. int add_rowindex = dtgrid.Rows.Add();
  81. dtgrid.Rows[add_rowindex].Cells[0].Value = "Mean";
  82. dtgrid.Rows[add_rowindex].Cells[1].Value = Math.Round(mean, 3).ToString();
  83. dtgrid.Rows[add_rowindex].Cells[2].Value = "";
  84. dtgrid.Rows[add_rowindex].Cells[3].Value = "δmom";
  85. dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(delta, 3).ToString();
  86. dtgrid.Rows[add_rowindex].Cells[5].Value = "";
  87. dtgrid.Rows[add_rowindex].Cells[6].Value = "δml";
  88. dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(ret[0], 3).ToString() ;
  89. //dtgrid.Rows[add_rowindex].DefaultCellStyle.Font = new Font("微软雅黑", 12, FontStyle.Regular);
  90. #endregion
  91. #region 第二行
  92. add_rowindex = dtgrid.Rows.Add();
  93. dtgrid.Rows[add_rowindex].Cells[0].Value = "Sdev";
  94. dtgrid.Rows[add_rowindex].Cells[1].Value = Math.Round(sdev, 3).ToString();
  95. dtgrid.Rows[add_rowindex].Cells[2].Value = "";
  96. dtgrid.Rows[add_rowindex].Cells[3].Value = "λmom";
  97. dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(lambda, 3).ToString();
  98. dtgrid.Rows[add_rowindex].Cells[5].Value = "";
  99. dtgrid.Rows[add_rowindex].Cells[6].Value = "λml";
  100. dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(ret[1], 3).ToString();
  101. #endregion
  102. #region 第三行
  103. add_rowindex = dtgrid.Rows.Add();
  104. dtgrid.Rows[add_rowindex].Cells[0].Value = "Length";
  105. dtgrid.Rows[add_rowindex].Cells[1].Value = "";
  106. dtgrid.Rows[add_rowindex].Cells[2].Value = "";
  107. dtgrid.Rows[add_rowindex].Cells[3].Value = "Red.Var";
  108. #endregion
  109. #region 第四行
  110. add_rowindex = dtgrid.Rows.Add();
  111. dtgrid.Rows[add_rowindex].Cells[0].Value = "(Y)";
  112. dtgrid.Rows[add_rowindex].Cells[1].Value = "Rank";
  113. dtgrid.Rows[add_rowindex].Cells[2].Value = "Prob";
  114. dtgrid.Rows[add_rowindex].Cells[3].Value = "(X)";
  115. dtgrid.Rows[add_rowindex].Cells[4].Value = "ln";
  116. dtgrid.Rows[add_rowindex].Cells[5].Value = "";
  117. dtgrid.Rows[add_rowindex].Cells[6].Value = "X";
  118. dtgrid.Rows[add_rowindex].Cells[7].Value = "Xlow";
  119. dtgrid.Rows[add_rowindex].Cells[8].Value = "Xhig";
  120. #endregion
  121. #region 第五行
  122. add_rowindex = dtgrid.Rows.Add();
  123. dtgrid.Rows[add_rowindex].Cells[0].Value = "Data";
  124. dtgrid.Rows[add_rowindex].Cells[1].Value = "";
  125. dtgrid.Rows[add_rowindex].Cells[2].Value = "";
  126. dtgrid.Rows[add_rowindex].Cells[3].Value = "RV";
  127. dtgrid.Rows[add_rowindex].Cells[4].Value = "(f(xi.δ.λ)";
  128. #endregion
  129. dtgrid.Rows[4].Height = 40;
  130. dtgrid.Rows[4].DefaultCellStyle.Font = new Font("微软雅黑", 12, FontStyle.Regular);
  131. for (int j = 0; j < 9; j++)
  132. {
  133. dtgrid.Rows[4].Cells[j].Style.BackColor = Color.WhiteSmoke;
  134. }
  135. for (int i = 0; i < N; i++)
  136. {
  137. add_rowindex = dtgrid.Rows.Add();
  138. dtgrid.Rows[add_rowindex].Cells[0].Value = vs[i].ToString();
  139. dtgrid.Rows[add_rowindex].Cells[1].Value = (i+1).ToString();
  140. dtgrid.Rows[add_rowindex].Cells[2].Value = C[i].ToString();
  141. dtgrid.Rows[add_rowindex].Cells[3].Value =Math.Round(D[i],5).ToString();
  142. dtgrid.Rows[add_rowindex].Cells[4].Value = Math.Round(E[i], 5).ToString();
  143. dtgrid.Rows[add_rowindex].Cells[5].Value = Math.Round(F[i], 5).ToString();
  144. dtgrid.Rows[add_rowindex].Cells[6].Value = Math.Round(G[i], 2).ToString();
  145. dtgrid.Rows[add_rowindex].Cells[7].Value = Math.Round(H[i], 1).ToString();
  146. dtgrid.Rows[add_rowindex].Cells[8].Value = Math.Round(I[i], 1).ToString();
  147. }
  148. add_rowindex = dtgrid.Rows.Add();
  149. dtgrid.Rows[add_rowindex].Cells[0].Value = "";
  150. dtgrid.Rows[add_rowindex].Cells[1].Value = "";
  151. dtgrid.Rows[add_rowindex].Cells[2].Value = "";
  152. dtgrid.Rows[add_rowindex].Cells[3].Value = "";
  153. dtgrid.Rows[add_rowindex].Cells[4].Value = "SUM(LL)";
  154. dtgrid.Rows[add_rowindex].Cells[5].Value = Math.Round(sumLL, 3).ToString();
  155. dtgrid.Rows[add_rowindex].Cells[6].Value = "";
  156. dtgrid.Rows[add_rowindex].Cells[7].Value = "";
  157. dtgrid.Rows[add_rowindex].Cells[8].Value = "";
  158. #region 极值分析
  159. LmaxData = new List<double[]>();
  160. double[] maxT = { 1.01, 1.11, 2, 20, 50, 100, 1000, 10000, 100000, 1000000, 10000000 };
  161. double[] maxP = { 1, 10, 50, 95, 98.0, 99, 99.9, 99.99, 99.999, 99.9999, 99.99999 };
  162. LmaxData.Add(maxT);
  163. LmaxData.Add(maxP);
  164. double[] maxy = new double[maxP.Length];
  165. double[] Lmax = new double[maxP.Length];
  166. for (int i = 0; i < maxP.Length; i++)
  167. {
  168. maxy[i] = -1 * Math.Log(-1 * Math.Log(maxP[i]*0.01));
  169. Lmax[i] = ret[0] * maxy[i] + ret[1];
  170. }
  171. LmaxData.Add(maxy);
  172. LmaxData.Add(Lmax);
  173. #endregion
  174. }
  175. }
  176. private void btnChart_Click(object sender, EventArgs e)
  177. {
  178. for (int i = 0; i < allData.Count; i++)
  179. {
  180. for (int j = 0; j < allData[i].Length; j++)
  181. {
  182. if (double.IsNaN(allData[i][j]) || double.IsInfinity(allData[i][j]))
  183. {
  184. MessageBox.Show("数据不正常,会造成溢出");
  185. return;
  186. }
  187. }
  188. }
  189. ChartSherp chart = new ChartSherp(allData, ret[0], ret[1]);
  190. chart.Show();
  191. }
  192. private void btnLmax_Click(object sender, EventArgs e)
  193. {
  194. for (int i = 0; i < allData.Count; i++)
  195. {
  196. for (int j = 0; j < allData[i].Length; j++)
  197. {
  198. if (double.IsNaN(allData[i][j]) || double.IsInfinity(allData[i][j]))
  199. {
  200. MessageBox.Show("数据不正常,会造成溢出");
  201. return;
  202. }
  203. }
  204. }
  205. LmaxForm lmaxForm = new LmaxForm(LmaxData);
  206. lmaxForm.Show();
  207. }
  208. public static void ExportToExcel(DataGridView dataGrid, string path)
  209. {
  210. StreamWriter sw = null;
  211. try
  212. {
  213. long totalCount = dataGrid.Rows.Count;
  214. sw = new StreamWriter(path, false, Encoding.Unicode);
  215. StringBuilder sb = new StringBuilder();
  216. //for (int i = 0; i < dataGrid.Columns.Count; i++)
  217. //{
  218. // sb.Append(dataGrid.Columns[i].HeaderText + "\t");
  219. //}
  220. //sb.Append(Environment.NewLine);
  221. for (int i = 0; i < dataGrid.Rows.Count; i++)
  222. {
  223. for (int j = 0; j < dataGrid.Columns.Count; j++)
  224. {
  225. sb.Append(dataGrid.Rows[i].Cells[j].Value==null?"": dataGrid.Rows[i].Cells[j].Value + "\t");
  226. }
  227. sb.Append(Environment.NewLine);
  228. }
  229. sw.Write(sb.ToString());
  230. sw.Flush();
  231. MessageBox.Show("导出完成!");
  232. }
  233. catch (IOException ioe)
  234. {
  235. throw ioe;
  236. }
  237. finally
  238. {
  239. if (sw != null)
  240. {
  241. sw.Close();
  242. }
  243. }
  244. }
  245. private void button1_Click(object sender, EventArgs e)
  246. {
  247. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  248. dialog.Description = "请选择保存地址";
  249. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  250. {
  251. ExportToExcel(dtgrid,dialog.SelectedPath+"//"+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls");
  252. }
  253. }
  254. }
  255. }