public void Run() { long percentComplete = 0; _exhuastiveCache = new Cache(10, 0.0); if ((_fitnessCrit == FitnessCriteria.R2) || (_fitnessCrit == FitnessCriteria.AdjustedR2)) { _exhuastiveCache.Comparer = new DescendSort(); } else if ((_fitnessCrit == FitnessCriteria.Sensitivity) || (_fitnessCrit == FitnessCriteria.Specificity) || (_fitnessCrit == FitnessCriteria.Accuracy)) { _exhuastiveCache.Comparer = new DescendSort(); } else { _exhuastiveCache.Comparer = new AscendSort(); } IIndividual indiv = null; List<short> combList = new List<short>(); short tmp = 0; ; for (int i = 0; i < _numVars; i++) { //ListItem li = (ListItem)lbIndVariables.Items[i]; tmp = (short)(i + 1); //tmp += Convert.ToInt16(li.ValueItem); combList.Add(tmp); } long totalComb = 0; long totalComplete = 0; Combinations<short> combinations = null; List<Combinations<short>> listAllComb = new List<Combinations<short>>(); for (short i = 1; i <= _maxVarsInModel; i++) { combinations = new Combinations<short>(combList.ToArray(), i, GenerateOption.WithoutRepetition); listAllComb.Add(combinations); totalComb += combinations.Count; } for (short i = 1; i <= _maxVarsInModel; i++) { if (Cancel) break; //combinations = new Combinations<short>(combList.ToArray(), i, GenerateOption.WithoutRepetition); combinations = listAllComb[i - 1]; foreach (IList<short> comb in combinations) { if ((!Double.IsNaN(_decisionThreshold)) && (!Double.IsNaN(_mandateThreshold)) && (_maxVIF != Int32.MaxValue)) indiv = new MLRIndividual(i, i, _fitnessCrit, _maxVIF, _decisionThreshold, _mandateThreshold); else if (_maxVIF != Int32.MaxValue) indiv = new MLRIndividual(i, i, _fitnessCrit, _maxVIF); else indiv = new MLRIndividual(i, i, _fitnessCrit); for (int j = 0; j < comb.Count; j++) indiv.Chromosome[j] = comb[j]; if (Cancel) break; indiv.Evaluate(); if (indiv.IsViable()) { //_exhuastiveCache.SortCache(); //_exhuastiveCache.ReplaceMinimum(indiv); _exhuastiveCache.Add(indiv); } //else //throw new Exception("Invalid individual."); totalComplete++; if ((totalComplete % 10) == 0) { percentComplete = totalComplete * 100 / totalComb; ESProgress(percentComplete, _exhuastiveCache.MaximumFitness); //VBLogger.getLogger().logEvent(percentComplete.ToString(), VBLogger.messageIntent.UserOnly, VBLogger.targetSStrip.ProgressBar); //lblProgress.Text = "Progress: " + (Convert.ToDouble(totalComplete) / Convert.ToDouble(totalComb)) * 100; //Console.WriteLine("Progress: " + (Convert.ToDouble(totalComplete) / Convert.ToDouble(totalComb)) * 100); //lblProgress.Refresh(); //Application.DoEvents(); } } } _exhuastiveCache.Sort(); //list = _exhuastiveCache.CacheList; //lbModels.Items.Clear(); //UpdateFitnessListBox(); ESComplete(this); }
private void RunManual() { ResetGraphs(); ResetOutputFields(); if (chkAllCombinations.Checked) { SearchExhaustiveModelList(); return; } DataTable dt = _projMgr.ModelDataTable; int numVars = lbIndVariables.Items.Count; FitnessCriteria fitnessCrit = GetFitnessCriteria(); MLRIndividual indiv = null; indiv = new MLRIndividual(numVars, numVars, fitnessCrit, _maxVIF, _decisionThreshold, _mandateThreshold); for (int i = 0; i < numVars; i++) indiv.Chromosome[i] = (short)(i + 1); indiv.Evaluate(); _list.Clear(); _list.Add(indiv); btnRun.Text = "Run"; changeControlStatus(true); UpdateFitnessListBox(); return; }
/// <summary> /// Fires when the app opens a project file /// </summary> /// <param name="projMgr"></param> /// public void UnpackState(object objPackedStates) { ModelingInfo formPackedState = (ModelingInfo)objPackedStates; //If either of these are null, then no project to open if (formPackedState == null) return; if (formPackedState.AvailableVariables == null) return; //Unpack the saved state of the PLS modeling control. this.Show(); tabControl1.SelectedTab = tabControl1.TabPages["PLS"]; ipyPLSControl.UnpackProjectState(formPackedState.PlsProject); //Return to the tab that was active when the project was saved. tabControl1.SelectedIndex = formPackedState.ActiveModelingTab; //Console.WriteLine("\n*** Modeling: project opened.***\n"); //Save dependent variable transform if (formPackedState.DependentVariableTransform == Globals.DependentVariableTransforms.none) mlrPredObs1.Transform = Globals.DependentVariableTransforms.none; else if (formPackedState.DependentVariableTransform == Globals.DependentVariableTransforms.Log10) mlrPredObs1.Transform = Globals.DependentVariableTransforms.Log10; else if (formPackedState.DependentVariableTransform == Globals.DependentVariableTransforms.Ln) mlrPredObs1.Transform = Globals.DependentVariableTransforms.Ln; else if (formPackedState.DependentVariableTransform == Globals.DependentVariableTransforms.Power) mlrPredObs1.Transform = Globals.DependentVariableTransforms.Power; _projMgr.Model = formPackedState.Model; //Save available and independent variables lbAvailableVariables.Items.Clear(); for (int i = 0; i < formPackedState.AvailableVariables.Count; i++) lbAvailableVariables.Items.Add(formPackedState.AvailableVariables[i]); lbIndVariables.Items.Clear(); for (int i = 0; i < formPackedState.IndependentVariables.Count; i++) lbIndVariables.Items.Add(formPackedState.IndependentVariables[i]); lblAvailVars.Text = lbAvailableVariables.Items.Count.ToString(); lblDepVars.Text = lbIndVariables.Items.Count.ToString(); //Save the chromosomes if (_list != null) _list.Clear(); else _list = new List<IIndividual>(); _projMgr.ModelDataTable = CreateModelDataTable(); MLRIndividual indiv = null; ModelingInfo modInfo = null; if (formPackedState.Chromosomes != null) { for (int i = 0; i < formPackedState.Chromosomes.Count; i++) { modInfo = formPackedState; int numGenes = formPackedState.Chromosomes[i].Count; indiv = new MLRIndividual(numGenes, modInfo.MaxGeneValue, (FitnessCriteria)modInfo.FitnessCriteria, modInfo.MaxVIF, modInfo.DecisionThreshold, modInfo.MandatedThreshold); indiv.Chromosome = formPackedState.Chromosomes[i]; indiv.Evaluate(); _list.Add((IIndividual)indiv); } UpdateFitnessListBox(); if (formPackedState.SelectedModel > -1 && indiv.Parameters != null) listBox1.SelectedIndex = formPackedState.SelectedModel; } _numObs = _projMgr.CorrelationDataTable.Rows.Count; lblNumObs.Text = "Number of Observations: " + _numObs.ToString(); int maxVar = _numObs / 5; //int recVar = Math.Min(((_numObs / 10) + 1), availVar); int recVar = Math.Min(((_numObs / 10) + 1), (lbIndVariables.Items.Count)); lblMaxAndRecommended.Text = "Recommended: " + recVar.ToString() + ", Max: " + maxVar.ToString(); txtMaxVars.Text = recVar.ToString(); this.Show(); if (listBox1.Items.Count < 1 && ipyPLSControl.VirginState == true) { _projMgr._comms.sendMessage("Hide", this); } }
/// <summary> /// given a mlr model, vary the threshold by increments to find model sensitivity and specificity /// for each increment - these become roc plotting points for the model. also, save the roc trace /// data for passing to the caller for table display. plotting points need to be sorted and aggregated /// (weedppl(ppl)) for calculating auc (area-under-curve via integration) /// </summary> /// <param name="model">given mlr model</param> /// <param name="rocTableVals"></param> /// <returns>null if number of pts lt 10, otherwise a pointpair list fro plotting</returns> private PointPairList ROCpoints(MLRIndividual model, out List<object> rocTableVals) { const int interations = 50; //vary the decision threshold by increments //calculate the ROC point for the decision threshold increment //accumulate points for all increments and return pointpairlist PointPairList ppl = new PointPairList(); PointPair pp = new PointPair(); ROCParameters rocParameters = null; List<object> rocTableVal = new List<object>(); double maxPred = model.PredictedValues.Max(); double minPred = model.PredictedValues.Min(); double inc = (maxPred - minPred) / (double)interations; double threshold = minPred; while (threshold < maxPred) { threshold += inc; pp = ROCpoint(model, threshold, out rocParameters); if (!pp.IsInvalid) { ppl.Add(pp); rocTableVal.Add(rocParameters.ROCPars); } } rocTableVals = rocTableVal; //how many points is the minimum??? if (ppl.Count > 10) { //sort for integral calc ppl.Sort(); //get rid of multiple X datapoints ppl = weedppl(ppl); return ppl; } else { return null; } }
/// <summary> /// given a model and decision threshold calculate (and store) information for a roc curve /// </summary> /// <param name="model">mlr model</param> /// <param name="decisionThreshold">threshold value</param> /// <param name="rocPars">roc curve information </param> /// <returns>point pair for plotting</returns> private PointPair ROCpoint(MLRIndividual model, double decisionThreshold, out ROCParameters rocPars) { //for a given threshold and model, categorize predictions relative to observations //and calculate the sensitivity and specificity evaluation criteria, return theses //values as a zedgraph point. PointPair pp = new PointPair(); double[] measurement = model.ObservedValues; double[] est = model.PredictedValues; int truePos = 0; int trueNeg = 0; int falsePos = 0; int falseNeg = 0; double pred = double.NaN; double obs = double.NaN; double specificity = double.NaN; double sensitivity = double.NaN; for (int i = 0; i < est.Length; i++) { pred = est[i]; obs = measurement[i]; if ((pred > decisionThreshold) && (obs > _mandateThreshold)) truePos++; else if ((pred > decisionThreshold) && (obs < _mandateThreshold)) falsePos++; else if ((pred < decisionThreshold) && (obs > _mandateThreshold)) falseNeg++; else if ((pred < decisionThreshold) && (obs < _mandateThreshold)) trueNeg++; } sensitivity = (double)truePos / (double)(truePos + falseNeg); specificity = (double)trueNeg / (double)(trueNeg + falsePos); if (!sensitivity.Equals(double.NaN) && !specificity.Equals(double.NaN)) { pp.X = (1.0d - specificity); pp.Y = sensitivity; } ROCParameters rocpars = new ROCParameters(decisionThreshold, sensitivity, specificity, falsePos, falseNeg); rocPars = rocpars; return pp; }
public void Run() { long percentComplete = 0; _exhuastiveCache = new Cache(10, 0.0); if ((_fitnessCrit == FitnessCriteria.R2) || (_fitnessCrit == FitnessCriteria.AdjustedR2)) { _exhuastiveCache.Comparer = new DescendSort(); } else if ((_fitnessCrit == FitnessCriteria.Sensitivity) || (_fitnessCrit == FitnessCriteria.Specificity) || (_fitnessCrit == FitnessCriteria.Accuracy)) { _exhuastiveCache.Comparer = new DescendSort(); } else { _exhuastiveCache.Comparer = new AscendSort(); } IIndividual indiv = null; List <short> combList = new List <short>(); short tmp = 0;; for (int i = 0; i < _numVars; i++) { //ListItem li = (ListItem)lbIndVariables.Items[i]; tmp = (short)(i + 1); //tmp += Convert.ToInt16(li.ValueItem); combList.Add(tmp); } long totalComb = 0; long totalComplete = 0; Combinations <short> combinations = null; List <Combinations <short> > listAllComb = new List <Combinations <short> >(); for (short i = 1; i <= _maxVarsInModel; i++) { combinations = new Combinations <short>(combList.ToArray(), i, GenerateOption.WithoutRepetition); listAllComb.Add(combinations); totalComb += combinations.Count; } for (short i = 1; i <= _maxVarsInModel; i++) { if (Cancel) { break; } //combinations = new Combinations<short>(combList.ToArray(), i, GenerateOption.WithoutRepetition); combinations = listAllComb[i - 1]; foreach (IList <short> comb in combinations) { if ((!Double.IsNaN(_decisionThreshold)) && (!Double.IsNaN(_mandateThreshold)) && (_maxVIF != Int32.MaxValue)) { indiv = new MLRIndividual(i, i, _fitnessCrit, _maxVIF, _decisionThreshold, _mandateThreshold); } else if (_maxVIF != Int32.MaxValue) { indiv = new MLRIndividual(i, i, _fitnessCrit, _maxVIF); } else { indiv = new MLRIndividual(i, i, _fitnessCrit); } for (int j = 0; j < comb.Count; j++) { indiv.Chromosome[j] = comb[j]; } if (Cancel) { break; } indiv.Evaluate(); if (indiv.IsViable()) { //_exhuastiveCache.SortCache(); //_exhuastiveCache.ReplaceMinimum(indiv); _exhuastiveCache.Add(indiv); } //else //throw new Exception("Invalid individual."); totalComplete++; if ((totalComplete % 10) == 0) { percentComplete = totalComplete * 100 / totalComb; ESProgress(percentComplete, _exhuastiveCache.MaximumFitness); //VBLogger.getLogger().logEvent(percentComplete.ToString(), VBLogger.messageIntent.UserOnly, VBLogger.targetSStrip.ProgressBar); //lblProgress.Text = "Progress: " + (Convert.ToDouble(totalComplete) / Convert.ToDouble(totalComb)) * 100; //Console.WriteLine("Progress: " + (Convert.ToDouble(totalComplete) / Convert.ToDouble(totalComb)) * 100); //lblProgress.Refresh(); //Application.DoEvents(); } } } _exhuastiveCache.Sort(); //list = _exhuastiveCache.CacheList; //lbModels.Items.Clear(); //UpdateFitnessListBox(); ESComplete(this); }