- 积分
- 126
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-8-12
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 guxing-345 于 2019-8-31 16:14 编辑
想用C#计算突变点了,网上各种版本的都有就是没C#版的,于是自己写了个,供大家一起用吧。- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- //输入文件及输出文件定义
- string in_file = @"E:\year.txt";
- string out_file = @"E:\out_year.txt";
- //导入数据
- List<mkdata> list = new List<mkdata>();
- StreamReader sr = new StreamReader(in_file, Encoding.Default);
- sr.ReadLine();
- while (sr.Peek() > -1)
- {
- string[] line = sr.ReadLine().Trim().Split(new char[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- mkdata mk = new mkdata();
- mk.year = Int32.Parse(line[0]);
- mk.value = double.Parse(line[1]);
- list.Add(mk);
- }
- sr.Close();
- sr.Dispose();
- //计算MK突变点
- int n = list.Count;
- //正序列计算
- double[] UFk = new double[n];
- double[] y = new double[n];
- for (int i = 0; i < n; i++)
- {
- y[i] = list[i].value;
- }
- UFk = GetUFk(y);
- //逆序列计算
- double[] UBk = new double[n];
- double[] y2 = new double[n];
- for (int i = 0; i < n; i++)
- {
- y2[i] = list[n - i - 1].value;
- }
- UBk = GetUFk(y2);
- double[] UBk2 = new double[n];
- for (int i = 0; i < n; i++)
- {
- UBk2[i] = UBk[n - i - 1];
- }
- StreamWriter sw = new StreamWriter(out_file, false, Encoding.Default);
- for (int i = 0; i < n; i++)
- {
- sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", list[i].year, Math.Round(UFk[i], 3), Math.Round(UBk2[i], 3), -1.96, 1.96);
- }
- sw.Flush();
- sw.Close();
- sw.Dispose();
- Console.WriteLine("计算完成!");
- Console.ReadKey();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- }
- /// <summary>
- /// 计算UF或者UB
- /// </summary>
- /// <param name="y"></param>
- /// <returns></returns>
- static double[] GetUFk(double[] y)
- {
- try
- {
- int n = y.Length;
- double[] Sk = new double[n];
- double[] UFk = new double[n];
- double s = 0;
- for (int i = 1; i < n; i++)
- {
- for (int j = 0; j < i; j++)
- {
- if (y[i] > y[j])
- {
- s += 1;
- }
- }
- Sk[i] = s;
- double E = ((i + 1) * i) / 4.0;
- double var = ((i + 1) * i * (2 * (i + 1) + 5)) / 72.0;
- UFk[i] = (Sk[i] - E) / Math.Sqrt(var);
- }
- return UFk;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- public struct mkdata
- {
- public int year;
- public double value;
- }
复制代码 |
评分
-
查看全部评分
|