public void GetStatistic() { Random rand = new Random(); double[] y = new double[N]; for (int i = 0; i < N; i++) { double ksi = rand.NextDouble(); double x = ksi * (b - a) + a; y[i] = 1 / (x + 1); } double[] v = y.OrderBy((x) => x).ToArray(); int _n; if (N <= 100) { _n = (int)Math.Sqrt(N); } else { _n = (int)(4 * Math.Log(N)); } double[] F = new double[N]; for (int i = 0; i < N; i++) { int w = 0; for (int j = 0; j < N; j++) { if (v[i] == v[j]) { w++; } } F[i] = (double)w / N; if (i > 0) { F[i] += F[i - 1]; } } Histogram = new LineSeries() { Title = "Эмпирическая функция распределения", Color = OxyColors.Green }; for (int i = 0; i < N; i++) { Histogram.Points.Add(new DataPoint(v[i], (i > 0) ? Math.Round(F[i - 1], 1) : 0)); Histogram.Points.Add(new DataPoint(v[i], Math.Round(F[i], 1))); } NormalDistributon _norm = new NormalDistributon(v); Func <double, double> tf = (arg) => { if (arg < -1) { return(0); } else if (arg >= -1 / 5) { return(1); } else { return(_norm.Function(arg)); } }; NormalFunction = new FunctionSeries(tf, -1, -1.0 / 5, dx) { Title = "Нормальный закон распределения", Color = OxyColors.Orange }; ExponentialDistribution _exp = new ExponentialDistribution(v); Func <double, double> tf1 = (arg) => { if (arg < -1) { return(0); } else if (arg >= -1 / 5) { return(1); } else { return(_exp.Function(arg)); } }; ExponentialFunction = new FunctionSeries(tf1, -1, -1.0 / 5, dx) { Title = "Экспоненциальный закон распределения", Color = OxyColors.Plum }; MyDistribution _myfun = new MyDistribution(); Func <double, double> tf3 = (arg) => { if (arg < -1) { return(0); } else if (arg >= -1 / 5) { return(1); } else { return(_myfun.Function(arg)); } }; MyFunction = new FunctionSeries(tf3, -1, -1.0 / 5, dx) { Title = "Теоретическая функция распределения", Color = OxyColors.Red }; UniformDistribution _uni = new UniformDistribution(v); Func <double, double> tf2 = (arg) => { if (arg < -1) { return(0); } else if (arg >= -1 / 5) { return(1); } else { return(_uni.Function(arg)); } }; UniformFunction = new FunctionSeries(tf2, -1, -1.0 / 5, dx) { Title = "Равномерный закон распределения", Color = OxyColors.Blue }; //////////////////////////////////////////////////////////////////////////// double[] _pirsonAnswers = new double[4]; Pirson _pirson = new Pirson(); _pirsonAnswers[0] = _pirson._Pirson(v, _norm); _pirsonAnswers[1] = _pirson._Pirson(v, _exp); _pirsonAnswers[2] = _pirson._Pirson(v, _uni); _pirsonAnswers[3] = _pirson._Pirson(v, _myfun); //////////////////////////////////////// int _N1 = 30; double[] y1 = new double[_N1]; for (int i = 0; i < 30; i++) { double ksi = rand.NextDouble(); double x = ksi * (b - a) + a; y1[i] = 1 / (x + 1); } v = y1.OrderBy((x) => x).ToArray(); double[] _colmoAnswers = new double[4]; Colmogorov _colmo = new Colmogorov(); _colmoAnswers[0] = _colmo._Colmogorov(v, _norm); _colmoAnswers[1] = _colmo._Colmogorov(v, _exp); _colmoAnswers[2] = _colmo._Colmogorov(v, _uni); _colmoAnswers[3] = _colmo._Colmogorov(v, _myfun); //////////////////////////////////////////////////////////////////////// int _N2 = 50; double[] y2 = new double[_N2]; for (int i = 0; i < 50; i++) { double ksi = rand.NextDouble(); double x = ksi * (b - a) + a; y2[i] = 1 / (x + 1); } v = y2.OrderBy((x) => x).ToArray(); double[] _mizesAnswers = new double[4]; Mizes _mizes = new Mizes(); _mizesAnswers[0] = _mizes._Mizes(v, _norm); _mizesAnswers[1] = _mizes._Mizes(v, _exp); _mizesAnswers[2] = _mizes._Mizes(v, _uni); _mizesAnswers[3] = _mizes._Mizes(v, _myfun); string[] answerString = { "-Нормальный з.р. ", "-Экспоненциальный з.р. ", "-Равномерный з.р. ", "-Теоретическую ф.р. " }; for (int i = 0; i < 4; i++) { bool _yes = false; if (_pirsonAnswers[i] != 0) { answerString[i] += "критерий Пирсона не отклоняет с вероятностью " + _pirsonAnswers[i].ToString() + ' '; _yes = true; } if (_colmoAnswers[i] != 0) { if (_yes) { answerString[i] += ",\n"; } answerString[i] += "критерий Колмогорова не отклоняет с вероятностью " + _colmoAnswers[i].ToString() + ' '; _yes = true; } if (_mizesAnswers[i] != 0) { if (_yes) { answerString[i] += ",\n"; } answerString[i] += "критерий Мизеса не отклоняет с вероятностью " + _mizesAnswers[i].ToString(); } if ((_mizesAnswers[i] == 0) && (_colmoAnswers[i] == 0) && (_pirsonAnswers[i] == 0)) { answerString[i] += "ни один из критериев не подтверждает"; } answerString[i] += ";\n"; } this.Result = ""; this.Result = answerString.Aggregate((working, next) => next + working); }
static void Main(string[] args) { Dictionary <string, List <string> > tableNameAndVectors = new Dictionary <string, List <string> >(); Console.WriteLine("Getting Connection ..."); MySqlConnection conn = DBUtils.GetDBConnection(); //Открытие соединения с БЛ try { Console.WriteLine("Openning Connection ..."); conn.Open(); //Запрос на использование схемы БД string sqlQuery = "USE golubev;"; MySqlCommand command = new MySqlCommand(sqlQuery, conn); command.ExecuteNonQuery(); //sqlQuery = "CREATE TABLE `params_for_distribution` (`id` INT NOT NULL AUTO_INCREMENT, `table_name` VARCHAR(450) NULL, `type_of_distributions` VARCHAR(450) NULL, `params` VARCHAR(450) NULL, PRIMARY KEY(`id`));"; //command = new MySqlCommand(sqlQuery, conn); //command.ExecuteNonQuery(); //Получение таблиц для которых будут подсчитаны векторы string sqlQuerySelectTebleName = "SELECT table_name FROM sample;"; command = new MySqlCommand(sqlQuerySelectTebleName, conn); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string tableName = reader[0].ToString(); tableNameAndVectors.Add(tableName, new List <string>()); } reader.Close(); //Получение порогового значения и векторов foreach (KeyValuePair <string, List <string> > entry in tableNameAndVectors) { Console.WriteLine("*"); string tableName = entry.Key; List <DateTime> dateTimes = new List <DateTime>(); // Время всех пакетов с соблюдением чередности List <TimeSpan> listForVector = new List <TimeSpan>(); // параметр промежуток между пакетами string queryTimeStamps = getQueryTimeStampsFromTable(tableName); command = new MySqlCommand(queryTimeStamps, conn); //string beginTime = "00:00:00"; string beginTime = command.ExecuteScalar().ToString(); DateTime tempTime = DateTime.Parse(beginTime); reader = command.ExecuteReader(); while (reader.Read()) { string current = reader[0].ToString(); DateTime currentTime = DateTime.Parse(current); dateTimes.Add(currentTime); insertToSortedList(listForVector, currentTime - tempTime); tempTime = currentTime; } reader.Close(); listForVector.Reverse(); // Теперь промежутки между пакетами идут на убывание //Пороговое значение TimeSpan limitValue = listForVector[100]; if (limitValue.CompareTo(new TimeSpan(0)) != 0) { List <double> durationGroups = new List <double>(); //последовательность длительности групп List <double> spanGroups = new List <double>(); //последовательность интервалов между группами double sumDuration = 0; double sumSpan = 0; DateTime beginBlock = dateTimes[0]; DateTime endBlock = dateTimes[0]; DateTime newBeginBlock = dateTimes[0]; for (int i = 1; i < dateTimes.Count; i++) { TimeSpan tempSpan = dateTimes[i] - dateTimes[i - 1]; if (tempSpan <= limitValue) { //Если интервал не превышает заданное пороговое значение continue; } else { //Если интервал превышает заданное пороговое значение endBlock = dateTimes[i - 1]; newBeginBlock = dateTimes[i]; double temp = (endBlock - beginBlock).TotalMinutes; durationGroups.Add(temp); sumDuration += temp; temp = (newBeginBlock - beginBlock).TotalMinutes; spanGroups.Add(temp); sumSpan += temp; beginBlock = dateTimes[i]; } } entry.Value.Add(durationGroups.ToString()); entry.Value.Add(spanGroups.ToString()); //Тут у нас уже есть два вектора durationGroups и spanGroups Dictionary <string, string> parametrs = Pirson.getParams(durationGroups, sumDuration); foreach (KeyValuePair <string, string> temp in parametrs) { string queryInsert = getQueryInsert("Длит.Блока " + tableName, temp.Key, temp.Value); command = new MySqlCommand(queryInsert, conn); command.ExecuteNonQuery(); } parametrs = Pirson.getParams(spanGroups, sumSpan); foreach (KeyValuePair <string, string> temp in parametrs) { string queryInsert = getQueryInsert("Длит.Инт. " + tableName, temp.Key, temp.Value); command = new MySqlCommand(queryInsert, conn); command.ExecuteNonQuery(); } } } //Выполнение без ошибок Console.WriteLine("End..."); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } finally { //Закрытие соединенися с БЛ conn.Close(); } Console.Read(); }