2021-06-28 11:35:52 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public static class LinearFunctionCalculator
|
|
|
|
|
{
|
|
|
|
|
public static double GetAForLinearFormula(IEnumerable<(double value, double timestamp)> rawData)
|
|
|
|
|
{
|
|
|
|
|
var (xSum, ySum, xySum, x2Sum) = GetFormulaVariables(rawData);
|
|
|
|
|
|
|
|
|
|
var result = (xSum * ySum - rawData.Count() * xySum) /
|
|
|
|
|
(xSum * xSum - rawData.Count() * x2Sum);
|
|
|
|
|
|
|
|
|
|
return (double)result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double GetBForLinearFormula(IEnumerable<(double value, double timestamp)> rawData)
|
|
|
|
|
{
|
|
|
|
|
var (xSum, ySum, xySum, x2Sum) = GetFormulaVariables(rawData);
|
|
|
|
|
|
|
|
|
|
var result = (xSum * xySum - x2Sum * ySum) /
|
|
|
|
|
(xSum * xSum - rawData.Count() * x2Sum);
|
|
|
|
|
|
|
|
|
|
return (double)result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsValueNotChanges(double value,
|
|
|
|
|
(double upperBound, double lowerBound) bounds) =>
|
|
|
|
|
value < bounds.upperBound && value > bounds.lowerBound;
|
|
|
|
|
|
|
|
|
|
public static bool IsValueGoesUp(double value, double bound) =>
|
2021-07-19 15:31:50 +05:00
|
|
|
|
value > bound;
|
2021-06-28 11:35:52 +05:00
|
|
|
|
|
|
|
|
|
public static bool IsValueGoesDown(double value, double bound) =>
|
2021-07-19 15:31:50 +05:00
|
|
|
|
value < bound;
|
2021-06-28 11:35:52 +05:00
|
|
|
|
|
|
|
|
|
public static bool IsAverageLessThanBound(IEnumerable<(double Value, double TotalSeconds)> values,
|
|
|
|
|
double bound) =>
|
|
|
|
|
(values.Sum(s => s.Value) / values.Count()) < bound;
|
|
|
|
|
|
|
|
|
|
public static bool IsAverageMoreThanBound(IEnumerable<(double Value, double TotalSeconds)> values,
|
|
|
|
|
double bound) =>
|
|
|
|
|
(values.Sum(s => s.Value) / values.Count()) >= bound;
|
|
|
|
|
|
|
|
|
|
private static (decimal xSum, decimal ySum, decimal xySum, decimal x2Sum) GetFormulaVariables(
|
|
|
|
|
IEnumerable<(double value, double timestamp)> rawData)
|
|
|
|
|
{
|
|
|
|
|
var data = rawData.Select((d) => new
|
|
|
|
|
{
|
|
|
|
|
X = d.timestamp,
|
|
|
|
|
Y = d.value
|
|
|
|
|
});
|
|
|
|
|
var xSum = (decimal)data.Sum(d => d.X);
|
|
|
|
|
var ySum = (decimal)data.Sum(d => d.Y);
|
|
|
|
|
var xySum = (decimal)data.Sum(d => d.X * d.Y);
|
|
|
|
|
var x2Sum = (decimal)data.Sum(d => d.X * d.X);
|
|
|
|
|
|
|
|
|
|
return (xSum, ySum, xySum, x2Sum);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|