MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * DDM.java 00003 * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand 00004 * @author Manuel Baena ([email protected]) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 */ 00020 package moa.classifiers.core.driftdetection; 00021 00022 import moa.core.ObjectRepository; 00023 import moa.options.AbstractOptionHandler; 00024 import moa.options.IntOption; 00025 import moa.tasks.TaskMonitor; 00026 00036 public class DDM extends AbstractOptionHandler implements DriftDetectionMethod { 00037 00038 private static final long serialVersionUID = -3518369648142099719L; 00039 00040 public IntOption minNumInstancesOption = new IntOption( 00041 "minNumInstances", 00042 'n', 00043 "The minium number of instances before permitting detecting change.", 00044 30, 0, Integer.MAX_VALUE); 00045 00046 private int m_n; 00047 00048 private double m_p; 00049 00050 private double m_s; 00051 00052 private double m_psmin; 00053 00054 private double m_pmin; 00055 00056 private double m_smin; 00057 00058 public DDM() { 00059 initialize(); 00060 } 00061 00062 private void initialize() { 00063 m_n = 1; 00064 m_p = 1; 00065 m_s = 0; 00066 m_psmin = Double.MAX_VALUE; 00067 m_pmin = Double.MAX_VALUE; 00068 m_smin = Double.MAX_VALUE; 00069 } 00070 00071 @Override 00072 public int computeNextVal(boolean prediction) { 00073 if (prediction == false) { 00074 m_p = m_p + (1.0 - m_p) / (double) m_n; 00075 } else { 00076 m_p = m_p - (m_p) / (double) m_n; 00077 } 00078 m_s = Math.sqrt(m_p * (1 - m_p) / (double) m_n); 00079 00080 m_n++; 00081 00082 // System.out.print(prediction + " " + m_n + " " + (m_p+m_s) + " "); 00083 00084 if (m_n < minNumInstancesOption.getValue()) { 00085 return DDM_INCONTROL_LEVEL; 00086 } 00087 00088 if (m_p + m_s <= m_psmin) { 00089 m_pmin = m_p; 00090 m_smin = m_s; 00091 m_psmin = m_p + m_s; 00092 } 00093 00094 00095 00096 if (m_n > minNumInstancesOption.getValue() && m_p + m_s > m_pmin + 3 * m_smin) { 00097 System.out.println(m_p + ",D"); 00098 initialize(); 00099 return DDM_OUTCONTROL_LEVEL; 00100 } else if (m_p + m_s > m_pmin + 2 * m_smin) { 00101 System.out.println(m_p + ",W"); 00102 return DDM_WARNING_LEVEL; 00103 } else { 00104 System.out.println(m_p + ",N"); 00105 return DDM_INCONTROL_LEVEL; 00106 } 00107 } 00108 00109 @Override 00110 public void getDescription(StringBuilder sb, int indent) { 00111 // TODO Auto-generated method stub 00112 } 00113 00114 @Override 00115 protected void prepareForUseImpl(TaskMonitor monitor, 00116 ObjectRepository repository) { 00117 // TODO Auto-generated method stub 00118 } 00119 00120 @Override 00121 public DriftDetectionMethod copy() { 00122 return (DriftDetectionMethod) super.copy(); 00123 } 00124 }