1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /** |
---|
18 | * UniformDataGenerator.java |
---|
19 | * Copyright (C) 2008 K.Hempstalk, University of Waikato, Hamilton, New Zealand. |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.classifiers.meta.generators; |
---|
23 | |
---|
24 | /** |
---|
25 | <!-- globalinfo-start --> |
---|
26 | * A uniform artificial data generator.<br/> |
---|
27 | * <br/> |
---|
28 | * This generator uses a uniform data model - all values have the same probability, and generated values must fall within the range given to the generator. |
---|
29 | * <p/> |
---|
30 | <!-- globalinfo-end --> |
---|
31 | * |
---|
32 | <!-- options-start --> |
---|
33 | * Valid options are: <p/> |
---|
34 | * |
---|
35 | * <pre> -D |
---|
36 | * If set, generator is run in debug mode and |
---|
37 | * may output additional info to the console</pre> |
---|
38 | * |
---|
39 | * <pre> -S <seed> |
---|
40 | * Sets the seed of the random number generator of the generator (default: 1)</pre> |
---|
41 | * |
---|
42 | * <pre> -L <num> |
---|
43 | * Sets the lower range of the generator |
---|
44 | * (default: 0)</pre> |
---|
45 | * |
---|
46 | * <pre> -U <num> |
---|
47 | * Sets the upper range of the generator |
---|
48 | * (default: 1)</pre> |
---|
49 | * |
---|
50 | <!-- options-end --> |
---|
51 | * |
---|
52 | * @author Kathryn Hempstalk (kah18 at cs.waikato.ac.nz) |
---|
53 | * @version $Revision: 5861 $ |
---|
54 | */ |
---|
55 | public class UniformDataGenerator |
---|
56 | extends RandomizableRangedGenerator |
---|
57 | implements NumericAttributeGenerator { |
---|
58 | |
---|
59 | /** for serialization. */ |
---|
60 | private static final long serialVersionUID = -6390354660638644832L; |
---|
61 | |
---|
62 | /** |
---|
63 | * Returns a string describing this class' ability. |
---|
64 | * |
---|
65 | * @return A description of the class. |
---|
66 | */ |
---|
67 | public String globalInfo() { |
---|
68 | return |
---|
69 | "A uniform artificial data generator.\n" |
---|
70 | + "\n" |
---|
71 | + "This generator uses a uniform data model - all values have " |
---|
72 | + "the same probability, and generated values must fall within " |
---|
73 | + "the range given to the generator."; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Generates a value that falls under this distribution. |
---|
78 | * |
---|
79 | * @return A generated value. |
---|
80 | */ |
---|
81 | public double generate() { |
---|
82 | double range = (m_UpperRange - m_LowerRange); |
---|
83 | return (m_Random.nextDouble() * range) + m_LowerRange; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Gets the probability that a value falls under |
---|
88 | * this distribution. |
---|
89 | * |
---|
90 | * |
---|
91 | * @param somedata The value to get the probability of. |
---|
92 | * @return The probability of the given value. |
---|
93 | */ |
---|
94 | public double getProbabilityOf(double somedata) { |
---|
95 | double range = (m_UpperRange - m_LowerRange); |
---|
96 | if (range <= 0 || somedata > m_UpperRange || somedata < m_LowerRange) { |
---|
97 | return Double.MIN_VALUE; |
---|
98 | } |
---|
99 | |
---|
100 | return 1 / (range); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Gets the (natural) log of the probability of a given value. |
---|
105 | * |
---|
106 | * @param somedata The value to get the log probability of. |
---|
107 | * @return The (natural) log of the probability. |
---|
108 | */ |
---|
109 | public double getLogProbabilityOf(double somedata) { |
---|
110 | double range = (m_UpperRange - m_LowerRange); |
---|
111 | if ((range <= 0) || (((somedata < m_LowerRange) || (somedata > m_UpperRange)))) { |
---|
112 | return Math.log(Double.MIN_VALUE); |
---|
113 | } |
---|
114 | return -Math.log(range); |
---|
115 | } |
---|
116 | } |
---|