1 | package tudelft.mentalhealth.perfectfit;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Objects;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Normalized characteristics of a person. Use
|
---|
9 | * {@link #normalize(double, double, double, double, double, double, double, double, double, double)}
|
---|
10 | * is you have non-normalized characterists eg from the form
|
---|
11 | */
|
---|
12 | public class Characteristics {
|
---|
13 |
|
---|
14 | private final double godin_activity;
|
---|
15 | private final double exercise_se;
|
---|
16 | private final double exercise_identity;
|
---|
17 | private final double extraversion;
|
---|
18 | private final double ttm_pa;
|
---|
19 | private final double openness;
|
---|
20 | private final double sitting_weekend;
|
---|
21 | private final double age;
|
---|
22 | private final double household_income;
|
---|
23 | private final double household_size;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | *
|
---|
27 | * @param godin_activity normalized godin value from the questionnaire
|
---|
28 | * form
|
---|
29 | * @param exercise_se normalized exercise_se value from the
|
---|
30 | * questionnaire form
|
---|
31 | * @param exercise_identity normalized exercise_identity value from the
|
---|
32 | * questionnaire form
|
---|
33 | * @param extraversion normalized extraversion value from the
|
---|
34 | * questionnaire form
|
---|
35 | * @param ttm_pa normalized ttm_pa value from the questionnaire
|
---|
36 | * form
|
---|
37 | * @param openness normalized openness value from the questionnaire
|
---|
38 | * form
|
---|
39 | * @param sitting_weekend normalized sitting_weekend value from the
|
---|
40 | * questionnaire form
|
---|
41 | * @param age normalized age value from the questionnaire form
|
---|
42 | * @param household_income normalized household_income value from the
|
---|
43 | * questionnaire form
|
---|
44 | * @param household_size normalized household_size value from the
|
---|
45 | * questionnaire form
|
---|
46 | */
|
---|
47 | public Characteristics(double godin_activity, double exercise_se,
|
---|
48 | double exercise_identity, double extraversion, double ttm_pa,
|
---|
49 | double openness, double sitting_weekend, double age,
|
---|
50 | double household_income, double household_size) throws IOException {
|
---|
51 | if (notnormal(godin_activity) || notnormal(exercise_se)
|
---|
52 | || notnormal(exercise_identity) || notnormal(extraversion)
|
---|
53 | || notnormal(ttm_pa) || notnormal(openness)
|
---|
54 | || notnormal(sitting_weekend) || notnormal(age)
|
---|
55 | || notnormal(household_income) || notnormal(household_size))
|
---|
56 | throw new IllegalArgumentException("values are not normalized");
|
---|
57 | this.godin_activity = godin_activity;
|
---|
58 | this.exercise_se = exercise_se;
|
---|
59 | this.exercise_identity = exercise_identity;
|
---|
60 | this.extraversion = extraversion;
|
---|
61 | this.ttm_pa = ttm_pa;
|
---|
62 | this.openness = openness;
|
---|
63 | this.sitting_weekend = sitting_weekend;
|
---|
64 | this.age = age;
|
---|
65 | this.household_income = household_income;
|
---|
66 | this.household_size = household_size;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | *
|
---|
71 | * @param godin_activity non-normalized godin value from the
|
---|
72 | * questionnaire form
|
---|
73 | * @param exercise_se non-normalized exercise_se value from the
|
---|
74 | * questionnaire form
|
---|
75 | * @param exercise_identity non-normalized exercise_identity value from the
|
---|
76 | * questionnaire form
|
---|
77 | * @param extraversion non-normalized extraversion value from the
|
---|
78 | * questionnaire form
|
---|
79 | * @param ttm_pa non-normalized ttm_pa value from the
|
---|
80 | * questionnaire form
|
---|
81 | * @param openness non-normalized openness value from the
|
---|
82 | * questionnaire form
|
---|
83 | * @param sitting_weekend non-normalized sitting_weekend value from the
|
---|
84 | * questionnaire form
|
---|
85 | * @param age non-normalized age value from the questionnaire
|
---|
86 | * form
|
---|
87 | * @param household_income non-normalized household_income value from the
|
---|
88 | * questionnaire form
|
---|
89 | * @param household_size non-normalized household_size value from the
|
---|
90 | * questionnaire form
|
---|
91 | * @throws IOException if bad values are given
|
---|
92 | */
|
---|
93 | public static Characteristics normalize(double godin_activity,
|
---|
94 | double exercise_se, double exercise_identity, double extraversion,
|
---|
95 | double ttm_pa, double openness, double sitting_weekend, double age,
|
---|
96 | double household_income, double household_size) throws IOException {
|
---|
97 | return new Characteristics(n(godin_activity, 0, 2),
|
---|
98 | n(exercise_se, 0, 100),
|
---|
99 | n(exercise_identity, 0, 3.7000000000000002),
|
---|
100 | n(extraversion, 0, 5.5), n(ttm_pa, 0, 4), n(openness, 0, 4),
|
---|
101 | n(sitting_weekend, 0, 22), n(age, 0, 52),
|
---|
102 | n(household_income, 0, 12), n(household_size, 0, 6));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @param v the value to be normalized.
|
---|
107 | * @param min the min expected value
|
---|
108 | * @param max the max expected value
|
---|
109 | * @return normalized value of v: (v-min)/(max-min), or 0 if v<min or 1 if
|
---|
110 | * v>max.
|
---|
111 | */
|
---|
112 | private static double n(double v, double min, double max) {
|
---|
113 | if (min >= max)
|
---|
114 | throw new IllegalArgumentException("min must be <max");
|
---|
115 | if (v < min)
|
---|
116 | return 0;
|
---|
117 | if (v > max)
|
---|
118 | return 1;
|
---|
119 | return (v - min) / (max - min);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public String toString() {
|
---|
124 | return Arrays.asList(godin_activity, exercise_se, exercise_identity,
|
---|
125 | extraversion, ttm_pa, openness, sitting_weekend, age,
|
---|
126 | household_income, household_size).toString();
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | *
|
---|
131 | * @param other another {@link Characteristics}
|
---|
132 | * @return the Nele similarity to the other. Higher value means more
|
---|
133 | * similar. The Nele similarity is based on the regression formula
|
---|
134 | * reported by Nele. Note that the function is symmetrical:
|
---|
135 | * distance(a,b) = distance(b,a).
|
---|
136 | */
|
---|
137 | public double similarity(Characteristics other) {
|
---|
138 | double dage = Math.abs(age - other.age);
|
---|
139 | double dhousehold_income = Math
|
---|
140 | .abs(household_income - other.household_income);
|
---|
141 | double dhousehold_size = Math
|
---|
142 | .abs(household_size - other.household_size);
|
---|
143 | double dextraversion = Math.abs(extraversion - other.extraversion);
|
---|
144 | double dopenness = Math.abs(openness - other.openness);
|
---|
145 | double dttm = Math.abs(ttm_pa - other.ttm_pa);
|
---|
146 | double dexerciseid = Math
|
---|
147 | .abs(exercise_identity - other.exercise_identity);
|
---|
148 | double dexercisese = Math.abs(exercise_se - other.exercise_se);
|
---|
149 | double dsittingw = Math.abs(sitting_weekend - other.sitting_weekend);
|
---|
150 | double dgodin = Math.abs(godin_activity - other.godin_activity);
|
---|
151 |
|
---|
152 | return 0.13887 - 0.64921 * dage + 0.76815 * dhousehold_income
|
---|
153 | + 0.60581 * dhousehold_size - 0.73115 * dextraversion
|
---|
154 | - 0.78798 * dopenness - 0.53480 * dttm - 0.44290 * dexerciseid
|
---|
155 | - 0.82005 * dexercisese + 0.56707 * dsittingw
|
---|
156 | - 0.88615 * dgodin;
|
---|
157 | }
|
---|
158 |
|
---|
159 | private boolean notnormal(double val) {
|
---|
160 | return val < 0 || val > 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public int hashCode() {
|
---|
165 | return Objects.hash(age, exercise_identity, exercise_se, extraversion,
|
---|
166 | godin_activity, household_income, household_size, openness,
|
---|
167 | sitting_weekend, ttm_pa);
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public boolean equals(Object obj) {
|
---|
172 | if (this == obj)
|
---|
173 | return true;
|
---|
174 | if (obj == null)
|
---|
175 | return false;
|
---|
176 | if (getClass() != obj.getClass())
|
---|
177 | return false;
|
---|
178 | Characteristics other = (Characteristics) obj;
|
---|
179 | return Double.doubleToLongBits(age) == Double
|
---|
180 | .doubleToLongBits(other.age)
|
---|
181 | && Double.doubleToLongBits(exercise_identity) == Double
|
---|
182 | .doubleToLongBits(other.exercise_identity)
|
---|
183 | && Double.doubleToLongBits(exercise_se) == Double
|
---|
184 | .doubleToLongBits(other.exercise_se)
|
---|
185 | && Double.doubleToLongBits(extraversion) == Double
|
---|
186 | .doubleToLongBits(other.extraversion)
|
---|
187 | && Double.doubleToLongBits(godin_activity) == Double
|
---|
188 | .doubleToLongBits(other.godin_activity)
|
---|
189 | && Double.doubleToLongBits(household_income) == Double
|
---|
190 | .doubleToLongBits(other.household_income)
|
---|
191 | && Double.doubleToLongBits(household_size) == Double
|
---|
192 | .doubleToLongBits(other.household_size)
|
---|
193 | && Double.doubleToLongBits(openness) == Double
|
---|
194 | .doubleToLongBits(other.openness)
|
---|
195 | && Double.doubleToLongBits(sitting_weekend) == Double
|
---|
196 | .doubleToLongBits(other.sitting_weekend)
|
---|
197 | && Double.doubleToLongBits(ttm_pa) == Double
|
---|
198 | .doubleToLongBits(other.ttm_pa);
|
---|
199 | }
|
---|
200 |
|
---|
201 | }
|
---|