Skip to content

Commit

Permalink
Issue 49131: Always calculate the values for the first N-1 samples, b…
Browse files Browse the repository at this point in the history
…ased on their sequence (#5039)
  • Loading branch information
ankurjuneja authored Dec 14, 2023
1 parent 0c0a116 commit cc7e46e
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions api/src/org/labkey/api/visualization/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,22 @@ public static Double[] getMovingRanges(Double[] values, boolean forcePositiveRes
*/
public static Double[] getTrailingMeans(Double[] values, int N)
{
if (values == null || values.length <= 1 || values.length <= N)
if (values == null || values.length <= 1)
return new Double[0];

int numOfTrailingValues = values.length - N + 1;
Double[] trailingMeans = new Double[numOfTrailingValues];
Double[] trailingMeans = new Double[values.length];
int start = 0;
int end = N;
for (int i = 0; i < numOfTrailingValues; i++)
int end = 1;
trailingMeans[0] = values[0];
for (int i = 1; i < values.length; i++)
{
// Issue 49131: At the very beginning of the folder's date range, always calculate the values for the first N-1 samples,
// based on their sequence. And so on until you hit N, when we start using the moving window.
if (i >= N)
{
start++;
}
trailingMeans[i] = getMean(getValuesFromRange(values, start, end));
start++;
end++;
}

Expand All @@ -192,37 +197,47 @@ public static Double[] getTrailingMeans(Double[] values, int N)
*/
public static Double[] getTrailingCVs(Double[] values, int N)
{
if (values == null || values.length <= 1 || values.length <= N)
if (values == null || values.length <= 1)
return new Double[0];

int numOfTrailingValues = values.length - N + 1;
Double[] trailingCVs = new Double[numOfTrailingValues];
Double[] trailingCVs = new Double[values.length];
int start = 0;
int end = N;
for (int i = 0; i < numOfTrailingValues; i++)
int end = 1;
trailingCVs[0] = 0.0;
for (int i = 1; i < values.length; i++)
{
Double[] vals = getValuesFromRange(values, start, end);
double sd = getStdDev(vals, false);
double mean = getMean(vals);
if (mean == 0)
{
trailingCVs[i] = null;
}
else
// Issue 49131: At the very beginning of the folder's date range, always calculate the values for the first N-1 samples,
// based on their sequence. And so on until you hit N, when we start using the moving window.
if (i >= N)
{
trailingCVs[i] = sd / mean * 100;
start++;
}
start++;
calcCV(values, trailingCVs, start, end, i);
end++;
}

return trailingCVs;
}

private static void calcCV(Double[] values, Double[] trailingCVs, int start, int end, int i)
{
Double[] vals = getValuesFromRange(values, start, end);
double sd = getStdDev(vals, false);
double mean = getMean(vals);
if (mean == 0)
{
trailingCVs[i] = null;
}
else
{
trailingCVs[i] = sd / mean * 100;
}
}

private static Double[] getValuesFromRange(Double[] values, int start, int end)
{
Double[] valuesFromRange = new Double[end-start];
if (end - start >= 0) System.arraycopy(values, start, valuesFromRange, 0, end - start);
Double[] valuesFromRange = new Double[end-start+1];
if (end - start >= 0) System.arraycopy(values, start, valuesFromRange, 0, end - start + 1);
return valuesFromRange;
}
}

0 comments on commit cc7e46e

Please sign in to comment.