DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/LinearFunctionCalculator.cs

67 lines
2.4 KiB
C#

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 IsValueChanges(double value,
(double upperBound, double lowerBound) bounds) =>
value >= bounds.upperBound || value <= bounds.lowerBound;
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) =>
value >= bound;
public static bool IsValueGoesDown(double value, double bound) =>
value <= bound;
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);
}
}
}