Added new probmethods, Made Probmethods static

This commit is contained in:
2025-06-21 14:22:25 +02:00
parent fbbed6f03d
commit bc8ba893e0
3 changed files with 123 additions and 46 deletions

View File

@@ -1,72 +1,149 @@
using System.Numerics.Tensors;
using System.Text.Json;
namespace Server;
public class Probmethods
public static class Probmethods
{
public delegate float probMethodDelegate(List<(string, float)> list);
public Dictionary<string, probMethodDelegate> probMethods;
public static readonly Dictionary<string, probMethodDelegate> probMethods;
public Probmethods(Dictionary<string, probMethodDelegate> probMethods)
static Probmethods()
{
this.probMethods = probMethods;
}
public Probmethods()
{
probMethods = [];
probMethods["wavg"] = WavgList;
probMethods["weighted_average"] = WavgList;
probMethods = new Dictionary<string, probMethodDelegate>
{
["Mean"] = Mean,
["HarmonicMean"] = HarmonicMean,
["QuadraticMean"] = QuadraticMean,
["GeometricMean"] = GeometricMean,
["ExtremeValuesEmphasisWeightedAverage"] = ExtremeValuesEmphasisWeightedAverage,
["EVEWAvg"] = ExtremeValuesEmphasisWeightedAverage,
["HighValueEmphasisWeightedAverage"] = HighValueEmphasisWeightedAverage,
["HVEWAvg"] = HighValueEmphasisWeightedAverage,
["LowValueEmphasisWeightedAverage"] = LowValueEmphasisWeightedAverage,
["LVEWAvg"] = LowValueEmphasisWeightedAverage
};
}
public probMethodDelegate? GetMethod(string name)
public static probMethodDelegate? GetMethod(string name)
{
try
{
return probMethods[name];
} catch (Exception)
}
catch
{
return null;
}
}
public static float Fact(float x)
public static float Mean(List<(string, float)> list)
{
return 1 / (1 - x);
if (list.Count == 0) return 0;
float sum = 0;
foreach ((_, float value) in list)
{
sum += value;
}
return sum / list.Count;
}
public static float WavgList(List<(string, float)> list)
public static float HarmonicMean(List<(string, float)> list)
{
float[] arr = new float[list.Count];
for (int i = 0; i < list.Count; i++)
{
arr[i] = list.ElementAt(i).Item2;
}
return Wavg(arr);
int n_T = list.Count;
float[] nonzeros = [.. list.Select(t => t.Item2).Where(t => t != 0)];
int n_nz = nonzeros.Length;
if (n_nz == 0) return 0;
float nzSum = nonzeros.Sum(x => 1 / x);
return n_nz / nzSum * (n_nz / (float)n_T);
}
public static float Wavg(float[] arr)
public static float QuadraticMean(List<(string, float)> list)
{
if (arr.Contains(1))
float sum = 0;
foreach (var (_, value) in list)
{
return 1;
sum += value * value;
}
float f = 0;
float fm = 0;
for (int i = 0; i < arr.Length; i++)
return (float)Math.Sqrt(sum / list.Count);
}
public static float GeometricMean(List<(string, float)> list)
{
if (list.Count == 0) return 0;
float product = 1;
foreach ((_, float value) in list)
{
float x = arr[i];
f += Fact(x);
fm += x * Fact(x);
product *= value;
}
return (float)Math.Pow(product, 1f / list.Count);
}
public static float ExtremeValuesEmphasisWeightedAverage(List<(string, float)> list)
{
float[] arr = [.. list.Select(x => x.Item2)];
if (arr.Contains(1)) return 1;
if (arr.Contains(0)) return 0;
float f = 0, fm = 0;
foreach (float x in arr)
{
f += x / (x * (1 - x));
fm += 1 / (x * (1 - x));
}
return f / fm;
}
public static float HighValueEmphasisWeightedAverage(List<(string, float)> list)
{
float[] arr = [.. list.Select(x => x.Item2)];
if (arr.Contains(1)) return 1;
float f = 0, fm = 0;
foreach (float x in arr)
{
f += x / (1 - x);
fm += 1 / (1 - x);
}
return f / fm;
}
public static float LowValueEmphasisWeightedAverage(List<(string, float)> list)
{
float[] arr = [.. list.Select(x => x.Item2)];
if (arr.Contains(0)) return 0;
float f = 0, fm = 0;
foreach (float x in arr)
{
f += 1;
fm += 1 / x;
}
return f / fm;
}
public static float DictionaryWeightedAverage(List<(string, float)> list, string jsonValues)
{
var values = JsonSerializer.Deserialize<Dictionary<string, float>>(jsonValues)
?? throw new Exception($"Unable to convert the string to a Dictionary<string,float>: {jsonValues}");
float f = 0, fm = 0;
foreach (var (key, value) in list)
{
float fact = 1;
if (values.TryGetValue(key, out float factor))
{
fact *= factor;
}
f += fact * value;
fm += fact;
}
return f / fm;
}
public static float Similarity(float[] vector1, float[] vector2)
{
return (float) TensorPrimitives.CosineSimilarity(vector1, vector2);
return TensorPrimitives.CosineSimilarity(vector1, vector2);
}
}
}