1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.cpdp.dataprocessing;
|
---|
16 |
|
---|
17 | import java.security.InvalidParameterException;
|
---|
18 | import java.util.Random;
|
---|
19 |
|
---|
20 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
21 | import org.apache.commons.math3.util.MathArrays;
|
---|
22 |
|
---|
23 | import weka.core.Instance;
|
---|
24 | import weka.core.Instances;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Implements the MORPH data privatization.
|
---|
28 | *
|
---|
29 | *
|
---|
30 | * @author Steffen Herbold
|
---|
31 | */
|
---|
32 | public class MORPH implements ISetWiseProcessingStrategy, IProcessesingStrategy {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * random number generator for MORPH
|
---|
36 | */
|
---|
37 | Random rand = new Random();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * parameter alpha for MORPH, default is 0.15
|
---|
41 | */
|
---|
42 | double alpha = 0.15;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * parameter beta for MORPH, default is 0.35
|
---|
46 | */
|
---|
47 | double beta = 0.35;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Does not have parameters. String is ignored.
|
---|
51 | *
|
---|
52 | * @param parameters
|
---|
53 | * ignored
|
---|
54 | */
|
---|
55 | @Override
|
---|
56 | public void setParameter(String parameters) {
|
---|
57 | if (parameters != null && !parameters.equals("")) {
|
---|
58 | String[] values = parameters.split(" ");
|
---|
59 | if( values.length!=2 ) {
|
---|
60 | throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values");
|
---|
61 | }
|
---|
62 | try {
|
---|
63 | alpha = Double.parseDouble(values[0]);
|
---|
64 | beta = Double.parseDouble(values[1]);
|
---|
65 | } catch(NumberFormatException e) {
|
---|
66 | throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values");
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances,
|
---|
73 | * org.apache.commons.collections4.list.SetUniqueList)
|
---|
74 | */
|
---|
75 | @Override
|
---|
76 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
77 | for( Instances traindata : traindataSet ) {
|
---|
78 | applyMORPH(traindata);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances,
|
---|
84 | * weka.core.Instances)
|
---|
85 | */
|
---|
86 | @Override
|
---|
87 | public void apply(Instances testdata, Instances traindata) {
|
---|
88 | applyMORPH(traindata);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | *
|
---|
93 | * <p>
|
---|
94 | * Applies MORPH to the data
|
---|
95 | * </p>
|
---|
96 | *
|
---|
97 | * @param data data to which the processor is applied
|
---|
98 | */
|
---|
99 | public void applyMORPH(Instances data) {
|
---|
100 | for (int i=0; i<data.numInstances(); i++ ) {
|
---|
101 | morphInstance(data.get(i), data);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * <p>
|
---|
107 | * Applies MORPH to a single instance
|
---|
108 | * </p>
|
---|
109 | *
|
---|
110 | * @param instance instance that is morphed
|
---|
111 | * @param data data based on which the instance is morphed
|
---|
112 | */
|
---|
113 | public void morphInstance(Instance instance, Instances data) {
|
---|
114 | Instance nearestUnlikeNeighbor = getNearestUnlikeNeighbor(instance, data);
|
---|
115 | if( nearestUnlikeNeighbor==null ) {
|
---|
116 | throw new RuntimeException("could not find nearest unlike neighbor within the data: " + data.relationName());
|
---|
117 | }
|
---|
118 | for( int j=0; j<data.numAttributes() ; j++ ) {
|
---|
119 | if( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) {
|
---|
120 | double randVal = rand.nextDouble()*(beta-alpha)+alpha;
|
---|
121 | instance.setValue(j, instance.value(j) + randVal*(instance.value(j)-nearestUnlikeNeighbor.value(j)) );
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * <p>
|
---|
128 | * Determines the nearest unlike neighbor of an instance.
|
---|
129 | * </p>
|
---|
130 | *
|
---|
131 | * @param instance instance to which the nearest unlike neighbor is determined
|
---|
132 | * @param data data where the nearest unlike neighbor is determined from
|
---|
133 | * @return nearest unlike instance
|
---|
134 | */
|
---|
135 | public Instance getNearestUnlikeNeighbor(Instance instance, Instances data) {
|
---|
136 | Instance nearestUnlikeNeighbor = null;
|
---|
137 |
|
---|
138 | double[] instanceVector = new double[data.numAttributes()-1];
|
---|
139 | int tmp = 0;
|
---|
140 | for( int j=0; j<data.numAttributes(); j++ ) {
|
---|
141 | if( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) {
|
---|
142 | instanceVector[tmp] = instance.value(j);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | double minDistance = Double.MAX_VALUE;
|
---|
147 | for( int i=0 ; i<data.numInstances() ; i++ ) {
|
---|
148 | if( instance.classValue() != data.instance(i).classValue() ) {
|
---|
149 | double[] otherVector = new double[data.numAttributes() - 1];
|
---|
150 | tmp = 0;
|
---|
151 | for (int j = 0; j < data.numAttributes(); j++) {
|
---|
152 | if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) {
|
---|
153 | otherVector[tmp++] = data.instance(i).value(j);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if( MathArrays.distance(instanceVector, otherVector)<minDistance) {
|
---|
157 | minDistance = MathArrays.distance(instanceVector, otherVector);
|
---|
158 | nearestUnlikeNeighbor = data.instance(i);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return nearestUnlikeNeighbor;
|
---|
163 | }
|
---|
164 | }
|
---|