1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package de.lmu.ifi.dbs.elki.distance.similarityfunction.kernel;
/*
This file is part of ELKI:
Environment for Developing KDD-Applications Supported by Index-Structures
Copyright (C) 2012
Ludwig-Maximilians-Universität München
Lehr- und Forschungseinheit für Datenbanksysteme
ELKI Development Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
import de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation;
import de.lmu.ifi.dbs.elki.database.query.DistanceSimilarityQuery;
import de.lmu.ifi.dbs.elki.database.query.distance.PrimitiveDistanceSimilarityQuery;
import de.lmu.ifi.dbs.elki.database.relation.Relation;
import de.lmu.ifi.dbs.elki.distance.distancefunction.AbstractPrimitiveDistanceFunction;
import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance;
import de.lmu.ifi.dbs.elki.distance.similarityfunction.PrimitiveSimilarityFunction;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.DoubleParameter;
/**
* Provides a polynomial Kernel function that computes a similarity between the
* two feature vectors V1 and V2 defined by (V1^T*V2)^degree.
*
* @author Simon Paradies
*/
public class PolynomialKernelFunction extends AbstractPrimitiveDistanceFunction<NumberVector<?, ?>, DoubleDistance> implements PrimitiveSimilarityFunction<NumberVector<?, ?>, DoubleDistance> {
/**
* The default degree.
*/
public static final double DEFAULT_DEGREE = 2.0;
/**
* Degree parameter
*/
public static final OptionID DEGREE_ID = OptionID.getOrCreateOptionID("kernel.degree", "The degree of the polynomial kernel function. Default: " + DEFAULT_DEGREE);
/**
* Degree of the polynomial kernel function
*/
private double degree = 0.0;
/**
* Constructor.
*
* @param degree Kernel degree
*/
public PolynomialKernelFunction(double degree) {
super();
this.degree = degree;
}
/**
* Provides the linear kernel similarity between the given two vectors.
*
* @param o1 first vector
* @param o2 second vector
* @return the linear kernel similarity between the given two vectors as an
* instance of {@link DoubleDistance DoubleDistance}.
*/
@Override
public DoubleDistance similarity(NumberVector<?, ?> o1, NumberVector<?, ?> o2) {
if(o1.getDimensionality() != o2.getDimensionality()) {
throw new IllegalArgumentException("Different dimensionality of Feature-Vectors" + "\n first argument: " + o1.toString() + "\n second argument: " + o2.toString());
}
double sim = 0;
for(int i = 1; i <= o1.getDimensionality(); i++) {
sim += o1.doubleValue(i) * o2.doubleValue(i);
}
return new DoubleDistance(Math.pow(sim, degree));
}
@Override
public DoubleDistance distance(final NumberVector<?, ?> fv1, final NumberVector<?, ?> fv2) {
return new DoubleDistance(Math.sqrt(similarity(fv1, fv1).doubleValue() + similarity(fv2, fv2).doubleValue() - 2 * similarity(fv1, fv2).doubleValue()));
}
@Override
public VectorFieldTypeInformation<? super NumberVector<?, ?>> getInputTypeRestriction() {
return TypeUtil.NUMBER_VECTOR_FIELD;
}
@Override
public DoubleDistance getDistanceFactory() {
return DoubleDistance.FACTORY;
}
@Override
public <T extends NumberVector<?, ?>> DistanceSimilarityQuery<T, DoubleDistance> instantiate(Relation<T> database) {
return new PrimitiveDistanceSimilarityQuery<T, DoubleDistance>(database, this, this);
}
/**
* Parameterization class.
*
* @author Erich Schubert
*
* @apiviz.exclude
*/
public static class Parameterizer extends AbstractParameterizer {
protected double degree = 0;
@Override
protected void makeOptions(Parameterization config) {
super.makeOptions(config);
final DoubleParameter degreeP = new DoubleParameter(DEGREE_ID, DEFAULT_DEGREE);
if(config.grab(degreeP)) {
degree = degreeP.getValue();
}
}
@Override
protected PolynomialKernelFunction makeInstance() {
return new PolynomialKernelFunction(degree);
}
}
}
|