/* * Create mining model */ private void CreateMiningModel(Microsoft.AnalysisServices.MiningStructure objStructure, string sName, string sAlgorithm, List <string> lsAtrPredict, List <string> lsMeasurePredict, List <bool> lbPredictItems, int parOne, int parTwo) { Microsoft.AnalysisServices.MiningModel myMiningModel = objStructure.CreateMiningModel(true, sName); /* Notes: * Each mining column must have its' input and predict columns * Input and key columns are added automatically when they are created in the mining structure * Predict columns can be added in the mining model * An input column can be also a predict column */ myMiningModel.Algorithm = sAlgorithm; switch (sAlgorithm) { case MiningModelAlgorithms.MicrosoftClustering: myMiningModel.AlgorithmParameters.Add("CLUSTERING_METHOD", parOne); if (parTwo > 0) { myMiningModel.AlgorithmParameters.Add("CLUSTER_COUNT", parTwo); } break; //case MiningModelAlgorithms.MicrosoftTimeSeries: // myMiningModel.AlgorithmParameters.Add("PERIODICITY_HINT", "{12}"); // {12} represents the number of months for prediction // break; case MiningModelAlgorithms.MicrosoftNaiveBayes: break; case MiningModelAlgorithms.MicrosoftDecisionTrees: myMiningModel.AlgorithmParameters.Add("SCORE_METHOD", parOne); myMiningModel.AlgorithmParameters.Add("SPLIT_METHOD", parTwo); break; } /***************** Predict columns *****************/ // add optional predict columns if (lsAtrPredict.Count != 0) { // predict columns for (int i = 0; i < lsAtrPredict.Count; i++) { Microsoft.AnalysisServices.MiningModelColumn modelColumn = myMiningModel.Columns.GetByName(lsAtrPredict[i]); modelColumn.SourceColumnID = lsAtrPredict[i]; if (lbPredictItems[i] == true) { modelColumn.Usage = MiningModelColumnUsages.PredictOnly; } else { modelColumn.Usage = MiningModelColumnUsages.Predict; } } } myMiningModel.Update(); }
/*--------------------------------- * Description: Update Mining Model and process StockPredict DB with new MAX, MIN when MAX,MIN are changed by Technology Analysis (IdentifyTrend) * ----------------------------------- */ public static bool UpdateMMbyAnalysis(string sStockCode) { Server svr = ConnectServer(str_Con_Svr); Database db = svr.Databases.GetByName("StockPredict"); Microsoft.AnalysisServices.MiningStructure ms = db.MiningStructures.FindByName(sStockCode); Microsoft.AnalysisServices.MiningModel mm = ms.MiningModels.FindByName(sStockCode); mm.AlgorithmParameters.Remove("MAXIMUM_SERIES_VALUE"); mm.AlgorithmParameters.Remove("MINIMUM_SERIES_VALUE"); // Max, Min Time Series mm.AlgorithmParameters.Add("MAXIMUM_SERIES_VALUE", MAXIMUM_SERIES_VALUE); mm.AlgorithmParameters.Add("MINIMUM_SERIES_VALUE", MINIMUM_SERIES_VALUE); mm.Update(); mm.Process(ProcessType.ProcessFull); // Update parameters into StockForecastModel try { SqlConnection conn = new SqlConnection(str_Con_SQL); conn.Open(); SqlCommand cmdUpdate = conn.CreateCommand(); cmdUpdate.CommandText = "Update StockForecastModel Set MAXIMUM_SERIES_VALUE=" + MAXIMUM_SERIES_VALUE.ToString().Replace(',', '.') + ", MINIMUM_SERIES_VALUE=" + MINIMUM_SERIES_VALUE.ToString().Replace(',', '.') + " Where StockCode='" + sStockCode + "'"; cmdUpdate.ExecuteNonQuery(); cmdUpdate.Dispose(); conn.Close(); } catch (Exception ex) { MessageBox.Show("Không thể cập nhật StockForecastModel!"); return(false); } return(true); }
public static bool UpdateMMTest(Microsoft.AnalysisServices.MiningModel mm, string strStockCode, DateTime dtTo) { mm.AlgorithmParameters.Clear(); // Default:10; 10:5 -> from 0 to (n-10)/5 //mm.AlgorithmParameters.Add("MINIMUM_SUPPORT", 10); // 0.1:0.05 -> from 0 to (1-0.1)/0.05=18 mm.AlgorithmParameters.Add("COMPLEXITY_PENALTY", COMPLEXITY_PENALTY); // {5,20,60}, 0:0.1 -> from 0 to (1-0.1)/0.05=18 mm.AlgorithmParameters.Add("PERIODICITY_HINT", "{5,20,60}"); mm.AlgorithmParameters.Add("AUTO_DETECT_PERIODICITY", AUTO_DETECT_PERIODICITY); // Defeult: 1, 10 mm.AlgorithmParameters.Add("HISTORIC_MODEL_COUNT", HISTORIC_MODEL_COUNT); mm.AlgorithmParameters.Add("HISTORIC_MODEL_GAP", HISTORIC_MODEL_GAP); // Max, Min Time Series mm.AlgorithmParameters.Add("MAXIMUM_SERIES_VALUE", MAXIMUM_SERIES_VALUE); mm.AlgorithmParameters.Add("MINIMUM_SERIES_VALUE", MINIMUM_SERIES_VALUE); mm.Update(); mm.Process(ProcessType.ProcessFull); return(ADOMDLib.CheckResultTest(strStockCode, dtTo)); }
/*--------------------------------- * Description: Update data for Analysis DB and reprocess it * ----------------------------------- */ public static void UpdateTrainDB(Microsoft.AnalysisServices.Server svr, string sStockCode, bool bAll, DateTime dtFrom, DateTime dtTo, bool bMulti) { Database db = svr.Databases.GetByName("Stock"); CreateDataAccessObjects(db, sStockCode, bAll, dtFrom, dtTo, bMulti, false); Microsoft.AnalysisServices.MiningModel mm = db.MiningStructures.GetByName(sStockCode).MiningModels.GetByName(sStockCode); // Max, Min Time Series mm.AlgorithmParameters.Remove("MAXIMUM_SERIES_VALUE"); mm.AlgorithmParameters.Remove("MINIMUM_SERIES_VALUE"); mm.AlgorithmParameters.Add("MAXIMUM_SERIES_VALUE", MAXIMUM_SERIES_VALUE); mm.AlgorithmParameters.Add("MINIMUM_SERIES_VALUE", MINIMUM_SERIES_VALUE); mm.Update(); //db.MiningStructures.GetByName(sStockCode).Process(ProcessType.ProcessFull); //mm.Process(ProcessType.ProcessFull); db.Process(ProcessType.ProcessFull); }
/*--------------------------------- * Description: Create Mining Model * ----------------------------------- */ public static void CreateMM(Microsoft.AnalysisServices.MiningStructure ms, string strStockCode, bool bMulti) { if (ms.MiningModels.ContainsName(strStockCode)) { ms.MiningModels[strStockCode].Drop(); } Microsoft.AnalysisServices.MiningModel mm = ms.CreateMiningModel(true, strStockCode); mm.Algorithm = MiningModelAlgorithms.MicrosoftTimeSeries; InitialParameters(strStockCode); // 0.1:0.05 -> from 0 to (1-0.1)/0.05=18 mm.AlgorithmParameters.Add("COMPLEXITY_PENALTY", COMPLEXITY_PENALTY); // {5,20,60}, 0:0.1 -> from 0 to (1-0.1)/0.05=18 mm.AlgorithmParameters.Add("PERIODICITY_HINT", "{5,20,60}"); mm.AlgorithmParameters.Add("AUTO_DETECT_PERIODICITY", AUTO_DETECT_PERIODICITY); // Defeult: 1, 10 mm.AlgorithmParameters.Add("HISTORIC_MODEL_COUNT", HISTORIC_MODEL_COUNT); mm.AlgorithmParameters.Add("HISTORIC_MODEL_GAP", HISTORIC_MODEL_GAP); // Max, Min Time Series mm.AlgorithmParameters.Add("MAXIMUM_SERIES_VALUE", MAXIMUM_SERIES_VALUE); mm.AlgorithmParameters.Add("MINIMUM_SERIES_VALUE", MINIMUM_SERIES_VALUE); mm.AllowDrillThrough = true; mm.Columns["ID"].Usage = MiningModelColumnUsages.Key; mm.Columns["ClosePrice"].Usage = MiningModelColumnUsages.Predict; if (strStockCode.ToUpper() != "VNINDEX" && !bMulti) { mm.Columns["OpenPrice"].Usage = MiningModelColumnUsages.Input; mm.Columns["HighPrice"].Usage = MiningModelColumnUsages.Input; mm.Columns["LowPrice"].Usage = MiningModelColumnUsages.Input; //mm.Columns["Volume"].Usage = MiningModelColumnUsages.Input; } mm.Update(); // Update parameters into StockForecastModel UpdateForecastModel(strStockCode); }
/* * Create mining model with custom fields and algorithm */ private void CreateCustomModel(MiningStructure objStructure, string sAlgorithm, string sModelName, string sKeyColumn, List <string> lPredictColumns, List <bool> lbPredictColumns, int parOne, int parTwo) { // drop existing model if (objStructure.MiningModels.ContainsName(sModelName)) { objStructure.MiningModels[sModelName].Drop(); } // Detailed description of the model algorithms is here: // http://msdn.microsoft.com/en-us/library/ms175595.aspx // More customisation for these algorithms can be found here: // http://msdn.microsoft.com/en-us/library/cc280427.aspx // Also a model example can be found here: // http://msdn.microsoft.com/en-us/library/ms345087(v=SQL.100).aspx Microsoft.AnalysisServices.MiningModel myMiningModel = objStructure.CreateMiningModel(true, sModelName); myMiningModel.Algorithm = sAlgorithm; switch (sAlgorithm) { case MiningModelAlgorithms.MicrosoftClustering: myMiningModel.AlgorithmParameters.Add("CLUSTERING_METHOD", parOne); myMiningModel.AlgorithmParameters.Add("CLUSTER_COUNT", parTwo); break; //case MiningModelAlgorithms.MicrosoftTimeSeries: // myMiningModel.AlgorithmParameters.Add("PERIODICITY_HINT", "{12}"); // {12} represents the number of months for prediction // break; case MiningModelAlgorithms.MicrosoftNaiveBayes: break; case MiningModelAlgorithms.MicrosoftDecisionTrees: myMiningModel.AlgorithmParameters.Add("SCORE_METHOD", parOne); myMiningModel.AlgorithmParameters.Add("SPLIT_METHOD", parTwo); break; } /***************** Predict columns *****************/ // add optional predict columns if (lPredictColumns.Count != 0) { // predict columns for (int i = 0; i < lPredictColumns.Count; i++) { Microsoft.AnalysisServices.MiningModelColumn modelColumn = myMiningModel.Columns.GetByName(lPredictColumns[i]); modelColumn.SourceColumnID = lPredictColumns[i]; if (lbPredictColumns[i] == true) { modelColumn.Usage = MiningModelColumnUsages.PredictOnly; } else { modelColumn.Usage = MiningModelColumnUsages.Predict; } } } myMiningModel.Update(); }