Skip to content

Commit fc74c1b

Browse files
committed
Simple BS average replaced with weighted average
1 parent 83927d1 commit fc74c1b

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

portable/comp-android/diacomp/src/main/java/org/bosik/diacomp/android/frontend/fragments/FragmentTabCharts.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.bosik.diacomp.core.entities.business.diary.records.MealRecord;
4747
import org.bosik.diacomp.core.services.analyze.RateService;
4848
import org.bosik.diacomp.core.services.analyze.entities.Rate;
49+
import org.bosik.diacomp.core.services.analyze.entities.WeightedValue;
4950
import org.bosik.diacomp.core.services.diary.DiaryService;
5051
import org.bosik.diacomp.core.utils.Utils;
5152
import org.bosik.merklesync.Versioned;
@@ -176,10 +177,25 @@ public Collection<Series<?>> load(Context context)
176177

177178
if (!items.isEmpty())
178179
{
179-
Collection<Double> values = items.values();
180+
final List<Date> keys = new ArrayList<>(items.keySet());
181+
final List<WeightedValue> points = new ArrayList<>();
182+
183+
if (keys.size() == 1)
184+
{
185+
points.add(new WeightedValue(items.get(keys.get(0)), 1.0));
186+
}
187+
else
188+
{
189+
for (int j = 0; j < keys.size() - 1; j++)
190+
{
191+
final double value = (items.get(keys.get(j)) + items.get(keys.get(j + 1))) / 2;
192+
final double weight = keys.get(j + 1).getTime() - keys.get(j).getTime();
193+
points.add(new WeightedValue(value, weight));
194+
}
195+
}
180196

181-
double mean = Utils.getMean(values);
182-
double deviation = Utils.getDeviation(values, mean);
197+
double mean = Utils.getWeightedMean(points);
198+
double deviation = Utils.getWeightedDeviation(points, mean);
183199
dataAvg.add(new DataPoint(windowMiddle, mean));
184200
dataMin.add(new DataPoint(windowMiddle, mean - deviation));
185201
dataMax.add(new DataPoint(windowMiddle, mean + deviation));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Diacomp - Diabetes analysis & management system
3+
* Copyright (C) 2025 Nikita Bosik
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.bosik.diacomp.core.services.analyze.entities;
19+
20+
public class WeightedValue
21+
{
22+
private double value;
23+
private double weight;
24+
25+
public WeightedValue()
26+
{
27+
}
28+
29+
public WeightedValue(double value, double weight)
30+
{
31+
this.value = value;
32+
this.weight = weight;
33+
}
34+
35+
public double getValue()
36+
{
37+
return value;
38+
}
39+
40+
public void setValue(double value)
41+
{
42+
this.value = value;
43+
}
44+
45+
public double getWeight()
46+
{
47+
return weight;
48+
}
49+
50+
public void setWeight(double weight)
51+
{
52+
this.weight = weight;
53+
}
54+
}

portable/comp-transfer/src/main/java/org/bosik/diacomp/core/utils/Utils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.bosik.diacomp.core.entities.business.Units;
2121
import org.bosik.diacomp.core.services.analyze.entities.Rate;
22+
import org.bosik.diacomp.core.services.analyze.entities.WeightedValue;
2223
import org.bosik.diacomp.core.services.preferences.PreferenceID;
2324
import org.json.JSONArray;
2425

@@ -764,6 +765,27 @@ public static double getMean(Collection<Double> values)
764765
}
765766
}
766767

768+
public static double getWeightedMean(Collection<WeightedValue> values)
769+
{
770+
double totalValue = 0.0;
771+
double totalWeight = 0.0;
772+
773+
for (WeightedValue x : values)
774+
{
775+
totalValue += x.getValue() * x.getWeight();
776+
totalWeight += x.getWeight();
777+
}
778+
779+
if (totalWeight > 0)
780+
{
781+
return totalValue / totalWeight;
782+
}
783+
else
784+
{
785+
return 0.0;
786+
}
787+
}
788+
767789
/**
768790
* Calculates standard deviation using pre-calculated mean
769791
*
@@ -787,6 +809,28 @@ public static double getDeviation(Collection<Double> values, double mean)
787809
return Math.sqrt(s);
788810
}
789811

812+
public static double getWeightedDeviation(Collection<WeightedValue> values, double mean)
813+
{
814+
double totalValue = 0.0;
815+
double totalWeight = 0.0;
816+
817+
for (WeightedValue x : values)
818+
{
819+
final double centered = x.getValue() - mean;
820+
totalValue += centered * centered * x.getWeight();
821+
totalWeight += x.getWeight();
822+
}
823+
824+
if (totalWeight > 0)
825+
{
826+
return Math.sqrt(totalValue / totalWeight);
827+
}
828+
else
829+
{
830+
return 0.0;
831+
}
832+
}
833+
790834
/* =========================================================================================
791835
* RANDOM
792836
* =========================================================================================/

0 commit comments

Comments
 (0)