Skip to content

Commit 99e062c

Browse files
authored
Updated language version to use latest (#1)
Updated target frameworks to widen the range Added a bunch of new metrics such as R2, Std Deviation, Std Error, etc
1 parent 3479eee commit 99e062c

File tree

6 files changed

+123
-34
lines changed

6 files changed

+123
-34
lines changed

src/AiDotNet.csproj

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8-
<Version>0.0.1-preview</Version>
9-
<Title>Ai for .Net</Title>
10-
<Description>This is a preview library that will eventually showcase the latest and greatest in ai breakthroughs and bring them to the .net community</Description>
11-
<Company>Ooples Finance</Company>
12-
<Authors>ooples</Authors>
13-
<Copyright>Ooples Finance LLC 2023</Copyright>
14-
<PackageProjectUrl>https://github.com/ooples/AiDotNet</PackageProjectUrl>
15-
<RepositoryType>git</RepositoryType>
16-
<RepositoryUrl>https://github.com/ooples/AiDotNet</RepositoryUrl>
17-
<PackageTags>ai; regression; machine learning; artificial; intelligence; machine; chatgpt; learning; algorithm; algo; chatgpt-4</PackageTags>
18-
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
19-
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
20-
<PackageIcon>Favicon.jpg</PackageIcon>
21-
<PackageReadmeFile>README.md</PackageReadmeFile>
4+
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8+
<Version>0.0.1-preview</Version>
9+
<Title>Ai for .Net</Title>
10+
<Description>This is a preview library that will eventually showcase the latest and greatest in ai breakthroughs and bring them to the .net community</Description>
11+
<Company>Ooples Finance</Company>
12+
<Authors>ooples</Authors>
13+
<Copyright>Ooples Finance LLC 2023</Copyright>
14+
<PackageProjectUrl>https://github.com/ooples/AiDotNet</PackageProjectUrl>
15+
<RepositoryType>git</RepositoryType>
16+
<RepositoryUrl>https://github.com/ooples/AiDotNet</RepositoryUrl>
17+
<PackageTags>ai; regression; machine learning; artificial; intelligence; machine; chatgpt; learning; algorithm; algo; chatgpt-4</PackageTags>
18+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
19+
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
20+
<LangVersion>latest</LangVersion>
21+
<PackageIcon>Favicon.jpg</PackageIcon>
22+
<PackageReadmeFile>README.md</PackageReadmeFile>
2223
</PropertyGroup>
2324

2425
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

src/Interfaces/IMetrics.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@ public abstract class IMetrics
55
internal abstract double CalculateMeanSquaredError();
66

77
internal abstract double CalculateRootMeanSquaredError();
8+
9+
internal abstract double CalculateR2();
10+
11+
internal abstract double CalculateAdjustedR2(double r2);
12+
13+
internal abstract double CalculateAverageStandardError();
14+
15+
internal abstract double CalculatePredictionStandardError();
16+
17+
internal abstract double CalculateAverageStandardDeviation();
18+
19+
internal abstract double CalculatePredictionStandardDeviation();
20+
21+
internal abstract int CalculateDegreesOfFreedom();
822
}

src/Metrics/Metrics.cs

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,109 @@
22

33
namespace AiDotNet;
44

5-
public class Metrics : IMetrics
5+
public sealed class Metrics : IMetrics
66
{
77
public double MeanSquaredError { get; private set; }
88
public double RootMeanSquaredError { get; private set; }
9-
9+
public double AdjustedR2 { get; private set; }
10+
public double R2 { get; private set; }
11+
public double PredictionsStandardError { get; private set; }
12+
public double PredictionsStandardDeviation { get; private set; }
13+
public double AverageStandardError { get; private set; }
14+
public double AverageStandardDeviation { get; private set; }
15+
public int DegreesOfFreedom { get; private set; }
16+
1017
private double[] _oosPredictions { get; }
18+
private double _oosPredictionsAvg { get; }
1119
private double[] _oosActualValues { get; }
20+
private double _oosActualValuesAvg { get; }
21+
private int _paramsCount { get; }
22+
private int _sampleSize { get; }
23+
private double _residualSumOfSquares { get; set; }
24+
private double _totalSumOfSquares { get; set; }
1225

13-
public Metrics(double[] OosPredictions, double[] OosActualValues)
26+
public Metrics(double[] OosPredictions, double[] OosActualValues, int paramCount)
1427
{
1528
_oosPredictions = OosPredictions;
29+
_oosPredictionsAvg = _oosPredictions.Average();
1630
_oosActualValues = OosActualValues;
31+
_oosActualValuesAvg = _oosActualValues.Average();
32+
_paramsCount = paramCount;
33+
_sampleSize = _oosPredictions.Length;
1734

35+
DegreesOfFreedom = CalculateDegreesOfFreedom();
36+
R2 = CalculateR2();
37+
AdjustedR2 = CalculateAdjustedR2(R2);
38+
AverageStandardDeviation = CalculateAverageStandardDeviation();
39+
PredictionsStandardDeviation = CalculatePredictionStandardDeviation();
40+
AverageStandardError = CalculateAverageStandardError();
41+
PredictionsStandardError = CalculatePredictionStandardError();
1842
MeanSquaredError = CalculateMeanSquaredError();
1943
RootMeanSquaredError = CalculateRootMeanSquaredError();
2044
}
2145

22-
internal sealed override double CalculateMeanSquaredError()
46+
internal override double CalculateMeanSquaredError()
47+
{
48+
return _residualSumOfSquares / _sampleSize;
49+
}
50+
51+
internal override double CalculateRootMeanSquaredError()
52+
{
53+
return MeanSquaredError >= 0 ? Math.Sqrt(MeanSquaredError) : 0;
54+
}
55+
56+
internal override double CalculateR2()
2357
{
24-
double sum = 0;
25-
for (var i = 0; i < _oosPredictions.Length; i++)
58+
double residualSumSquares = 0, totalSumSquares = 0;
59+
for (int i = 0; i < _sampleSize; i++)
2660
{
27-
sum += Math.Pow(_oosActualValues[i] - _oosPredictions[i], 2);
61+
residualSumSquares += Math.Pow(_oosActualValues[i] - _oosPredictions[i], 2);
62+
totalSumSquares += Math.Pow(_oosActualValues[i] - _oosActualValuesAvg, 2);
2863
}
2964

30-
return sum / _oosPredictions.Length;
65+
// We are saving these values for later reuse
66+
_residualSumOfSquares = residualSumSquares;
67+
_totalSumOfSquares = totalSumSquares;
68+
69+
return _totalSumOfSquares != 0 ? 1 - (_residualSumOfSquares / _totalSumOfSquares) : 0;
3170
}
3271

33-
internal sealed override double CalculateRootMeanSquaredError()
72+
internal override double CalculateAdjustedR2(double r2)
3473
{
35-
return MeanSquaredError >= 0 ? Math.Sqrt(MeanSquaredError) : 0;
74+
return _sampleSize != 1 && DegreesOfFreedom != 1 ? 1 - (1 - Math.Pow(r2, 2)) * (_sampleSize - 1) / (DegreesOfFreedom - 1) : 0;
75+
}
76+
77+
internal override double CalculateAverageStandardError()
78+
{
79+
return AverageStandardDeviation / Math.Sqrt(_sampleSize);
80+
}
81+
82+
internal override double CalculatePredictionStandardError()
83+
{
84+
return PredictionsStandardDeviation / Math.Sqrt(_sampleSize);
85+
}
86+
87+
private static double CalculateStandardDeviation(double avgSumSquares)
88+
{
89+
return avgSumSquares >= 0 ? Math.Sqrt(avgSumSquares) : 0;
90+
}
91+
92+
internal override double CalculateAverageStandardDeviation()
93+
{
94+
var avgSumSquares = _totalSumOfSquares / _sampleSize;
95+
96+
return CalculateStandardDeviation(avgSumSquares);
97+
}
98+
99+
internal override double CalculatePredictionStandardDeviation()
100+
{
101+
var avgSumSquares = _residualSumOfSquares / _sampleSize;
102+
103+
return CalculateStandardDeviation(avgSumSquares);
104+
}
105+
106+
internal override int CalculateDegreesOfFreedom()
107+
{
108+
return _sampleSize - _paramsCount;
36109
}
37110
}

src/Regression/SimpleRegression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public SimpleRegression(double[] inputs, double[] outputs, int trainingPctSize =
5959

6060
Fit(TrainingInputs, TrainingOutputs);
6161
Predictions = Transform(OutOfSampleInputs);
62-
Metrics = new Metrics(Predictions, OutOfSampleOutputs);
62+
Metrics = new Metrics(Predictions, OutOfSampleOutputs, inputs.Rank);
6363
}
6464

6565
internal sealed override void Fit(double[] x, double[] y)

testconsole/AiDotNetTestConsole.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
89
</PropertyGroup>
910

1011
<ItemGroup>

tests/AiDotNetUnitTests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
8-
<IsPackable>false</IsPackable>
7+
<IsPackable>false</IsPackable>
98
<IsTestProject>true</IsTestProject>
9+
<LangVersion>latest</LangVersion>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

0 commit comments

Comments
 (0)