public double computeAnomalyUnsupervised(RuleClassification rl, int ruleIndex, Instance inst) { //Unsupervised
		ArrayList<Integer> caseAnomalyTemp = new ArrayList<Integer>();
	    ArrayList<ArrayList<Double>> AttribAnomalyStatisticTemp2 = new ArrayList<ArrayList<Double>>();
	    double D = 0.0;
		double N = 0.0;
		
		
		if (rl.instancesSeen > this.anomalyNumInstThresholdOption.getValue() && this.anomalyDetectionOption.isSet()) { 
			for (int x = 0; x < inst.numAttributes() - 1; x++) {
				if(!inst.isMissing(x)){
					ArrayList<Double> AttribAnomalyStatisticTemp = new ArrayList<Double>();
					if (inst.attribute(x).isNumeric()) { //Numeric Attributes
						if((rl.instancesSeen - rl.attributeMissingValues.getValue(x)) > 30){
						double mean = computeMean(rl.attributeStatistics.getValue(x), rl.instancesSeen);
						double sd = computeSD(rl.squaredAttributeStatistics.getValue(x),rl.attributeStatistics.getValue(x), rl.instancesSeen);
						double probability = computeProbability(mean, sd, inst.value(x));
						if(probability!=0.0) {
							D = D + Math.log(probability);
							if(probability < this.probabilityThresholdOption.getValue()){  //0.10
								N = N + Math.log(probability);
			            	 	AttribAnomalyStatisticTemp.add((double)x);
			            	    AttribAnomalyStatisticTemp.add(inst.value(x));
				                AttribAnomalyStatisticTemp.add(mean);
				            	AttribAnomalyStatisticTemp.add(sd);
				            	AttribAnomalyStatisticTemp.add(probability);
				            	AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp);
				            	}
							}
						}
					}else { //Nominal Attributes
							AutoExpandVector<DoubleVector> attribNominal = ((NominalAttributeClassObserver)rl.observers.get(x)).attValDistPerClass; //Attributes values distribution per class
							double numbAttribValue = 0.0;
							double attribVal = inst.value(x); //Attribute value
							for(int i = 0; i < attribNominal.size(); i++){
								if(attribNominal.get(i) != null){
									numbAttribValue = numbAttribValue + attribNominal.get(i).getValue((int)attribVal); 
								}
							}
							double probability = numbAttribValue / rl.instancesSeen;
							if(probability!= 0.0) {
								D = D + Math.log(probability);
								if(probability < this.probabilityThresholdOption.getValue()){  //0.10
									N = N + Math.log(probability);
				            	 	AttribAnomalyStatisticTemp.add((double)x);
				            	    AttribAnomalyStatisticTemp.add(inst.value(x));
					            	AttribAnomalyStatisticTemp.add(probability);
					            	AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp);
					            	}
								}
							}
					}
				}
			}
		double anomaly=0.0;
		if(D !=0){
		 anomaly = Math.abs(N/D);
		}
		if(anomaly >= this.anomalyProbabilityThresholdOption.getValue()){
			caseAnomalyTemp.add(this.numInstance);
			double val = anomaly * 100;
			caseAnomalyTemp.add((int)val);
			this.caseAnomaly.add(caseAnomalyTemp);
			RuleClassification x = new RuleClassification(this.ruleSet.get(ruleIndex));
			this.ruleSetAnomalies.add(x);
			this.ruleAnomaliesIndex.add(ruleIndex + 1);
			this.ruleAttribAnomalyStatistics.add(AttribAnomalyStatisticTemp2);
			
			
		}
		return anomaly;
	}
public double computeAnomalyUnsupervised ( RuleClassification rl , int ruleIndex , Instance inst ) { ArrayList < Integer > caseAnomalyTemp = new ArrayList < Integer > ( ) ; ArrayList < ArrayList < Double > > AttribAnomalyStatisticTemp2 = new ArrayList < ArrayList < Double > > ( ) ; double D = 0.0 ; double N = 0.0 ; if ( rl . instancesSeen > this . anomalyNumInstThresholdOption . getValue ( ) && this . anomalyDetectionOption . isSet ( ) ) { for ( int x = 0 ; x < inst . numAttributes ( ) - 1 ; x ++ ) { if ( ! inst . isMissing ( x ) ) { ArrayList < Double > AttribAnomalyStatisticTemp = new ArrayList < Double > ( ) ; if ( inst . attribute ( x ) . isNumeric ( ) ) { if ( ( rl . instancesSeen - rl . attributeMissingValues . getValue ( x ) ) > 30 ) { double mean = computeMean ( rl . attributeStatistics . getValue ( x ) , rl . instancesSeen ) ; double sd = computeSD ( rl . squaredAttributeStatistics . getValue ( x ) , rl . attributeStatistics . getValue ( x ) , rl . instancesSeen ) ; double probability = computeProbability ( mean , sd , inst . value ( x ) ) ; if ( probability != 0.0 ) { D = D + Math . log ( probability ) ; if ( probability < this . probabilityThresholdOption . getValue ( ) ) { N = N + Math . log ( probability ) ; AttribAnomalyStatisticTemp . add ( ( double ) x ) ; AttribAnomalyStatisticTemp . add ( inst . value ( x ) ) ; AttribAnomalyStatisticTemp . add ( mean ) ; AttribAnomalyStatisticTemp . add ( sd ) ; AttribAnomalyStatisticTemp . add ( probability ) ; AttribAnomalyStatisticTemp2 . add ( AttribAnomalyStatisticTemp ) ; } } } } else { AutoExpandVector < DoubleVector > attribNominal = ( ( NominalAttributeClassObserver ) rl . observers . get ( x ) ) . attValDistPerClass ; double numbAttribValue = 0.0 ; double attribVal = inst . value ( x ) ; for ( int i = 0 ; i < attribNominal . size ( ) ; i ++ ) { if ( attribNominal . get ( i ) != null ) { numbAttribValue = numbAttribValue + attribNominal . get ( i ) . getValue ( ( int ) attribVal ) ; } } double probability = numbAttribValue / rl . instancesSeen ; if ( probability != 0.0 ) { D = D + Math . log ( probability ) ; if ( probability < this . probabilityThresholdOption . getValue ( ) ) { N = N + Math . log ( probability ) ; AttribAnomalyStatisticTemp . add ( ( double ) x ) ; AttribAnomalyStatisticTemp . add ( inst . value ( x ) ) ; AttribAnomalyStatisticTemp . add ( probability ) ; AttribAnomalyStatisticTemp2 . add ( AttribAnomalyStatisticTemp ) ; } } } } } } double anomaly = 0.0 ; if ( D != 0 ) { anomaly = Math . abs ( N / D ) ; } if ( anomaly >= this . anomalyProbabilityThresholdOption . getValue ( ) ) { caseAnomalyTemp . add ( this . numInstance ) ; double val = anomaly * 100 ; caseAnomalyTemp . add ( ( int ) val ) ; this . caseAnomaly . add ( caseAnomalyTemp ) ; RuleClassification x = new RuleClassification ( this . ruleSet . get ( ruleIndex ) ) ; this . ruleSetAnomalies . add ( x ) ; this . ruleAnomaliesIndex . add ( ruleIndex + 1 ) ; this . ruleAttribAnomalyStatistics . add ( AttribAnomalyStatisticTemp2 ) ; } return anomaly ; }
Compute anomalies unsupervised