Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/AiDotNet.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>0.0.1-preview</Version>
<Title>Ai for .Net</Title>
<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>
<Company>Ooples Finance</Company>
<Authors>ooples</Authors>
<Copyright>Ooples Finance LLC 2023</Copyright>
<PackageProjectUrl>https://github.com/ooples/AiDotNet</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ooples/AiDotNet</RepositoryUrl>
<PackageTags>ai; regression; machine learning; artificial; intelligence; machine; chatgpt; learning; algorithm; algo; chatgpt-4</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageIcon>Favicon.jpg</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>0.0.1-preview</Version>
<Title>Ai for .Net</Title>
<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>
<Company>Ooples Finance</Company>
<Authors>ooples</Authors>
<Copyright>Ooples Finance LLC 2023</Copyright>
<PackageProjectUrl>https://github.com/ooples/AiDotNet</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ooples/AiDotNet</RepositoryUrl>
<PackageTags>ai; regression; machine learning; artificial; intelligence; machine; chatgpt; learning; algorithm; algo; chatgpt-4</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<LangVersion>latest</LangVersion>
<PackageIcon>Favicon.jpg</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
14 changes: 14 additions & 0 deletions src/Interfaces/IMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ public abstract class IMetrics
internal abstract double CalculateMeanSquaredError();

internal abstract double CalculateRootMeanSquaredError();

internal abstract double CalculateR2();

internal abstract double CalculateAdjustedR2(double r2);

internal abstract double CalculateAverageStandardError();

internal abstract double CalculatePredictionStandardError();

internal abstract double CalculateAverageStandardDeviation();

internal abstract double CalculatePredictionStandardDeviation();

internal abstract int CalculateDegreesOfFreedom();
}
93 changes: 83 additions & 10 deletions src/Metrics/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,109 @@

namespace AiDotNet;

public class Metrics : IMetrics
public sealed class Metrics : IMetrics
{
public double MeanSquaredError { get; private set; }
public double RootMeanSquaredError { get; private set; }

public double AdjustedR2 { get; private set; }
public double R2 { get; private set; }
public double PredictionsStandardError { get; private set; }
public double PredictionsStandardDeviation { get; private set; }
public double AverageStandardError { get; private set; }
public double AverageStandardDeviation { get; private set; }
public int DegreesOfFreedom { get; private set; }

private double[] _oosPredictions { get; }
private double _oosPredictionsAvg { get; }
private double[] _oosActualValues { get; }
private double _oosActualValuesAvg { get; }
private int _paramsCount { get; }
private int _sampleSize { get; }
private double _residualSumOfSquares { get; set; }
private double _totalSumOfSquares { get; set; }

public Metrics(double[] OosPredictions, double[] OosActualValues)
public Metrics(double[] OosPredictions, double[] OosActualValues, int paramCount)
{
_oosPredictions = OosPredictions;
_oosPredictionsAvg = _oosPredictions.Average();
_oosActualValues = OosActualValues;
_oosActualValuesAvg = _oosActualValues.Average();
_paramsCount = paramCount;
_sampleSize = _oosPredictions.Length;

DegreesOfFreedom = CalculateDegreesOfFreedom();
R2 = CalculateR2();
AdjustedR2 = CalculateAdjustedR2(R2);
AverageStandardDeviation = CalculateAverageStandardDeviation();
PredictionsStandardDeviation = CalculatePredictionStandardDeviation();
AverageStandardError = CalculateAverageStandardError();
PredictionsStandardError = CalculatePredictionStandardError();
MeanSquaredError = CalculateMeanSquaredError();
RootMeanSquaredError = CalculateRootMeanSquaredError();
}

internal sealed override double CalculateMeanSquaredError()
internal override double CalculateMeanSquaredError()
{
return _residualSumOfSquares / _sampleSize;
}

internal override double CalculateRootMeanSquaredError()
{
return MeanSquaredError >= 0 ? Math.Sqrt(MeanSquaredError) : 0;
}

internal override double CalculateR2()
{
double sum = 0;
for (var i = 0; i < _oosPredictions.Length; i++)
double residualSumSquares = 0, totalSumSquares = 0;
for (int i = 0; i < _sampleSize; i++)
{
sum += Math.Pow(_oosActualValues[i] - _oosPredictions[i], 2);
residualSumSquares += Math.Pow(_oosActualValues[i] - _oosPredictions[i], 2);
totalSumSquares += Math.Pow(_oosActualValues[i] - _oosActualValuesAvg, 2);
}

return sum / _oosPredictions.Length;
// We are saving these values for later reuse
_residualSumOfSquares = residualSumSquares;
_totalSumOfSquares = totalSumSquares;

return _totalSumOfSquares != 0 ? 1 - (_residualSumOfSquares / _totalSumOfSquares) : 0;
}

internal sealed override double CalculateRootMeanSquaredError()
internal override double CalculateAdjustedR2(double r2)
{
return MeanSquaredError >= 0 ? Math.Sqrt(MeanSquaredError) : 0;
return _sampleSize != 1 && DegreesOfFreedom != 1 ? 1 - (1 - Math.Pow(r2, 2)) * (_sampleSize - 1) / (DegreesOfFreedom - 1) : 0;
}

internal override double CalculateAverageStandardError()
{
return AverageStandardDeviation / Math.Sqrt(_sampleSize);
}

internal override double CalculatePredictionStandardError()
{
return PredictionsStandardDeviation / Math.Sqrt(_sampleSize);
}

private static double CalculateStandardDeviation(double avgSumSquares)
{
return avgSumSquares >= 0 ? Math.Sqrt(avgSumSquares) : 0;
}

internal override double CalculateAverageStandardDeviation()
{
var avgSumSquares = _totalSumOfSquares / _sampleSize;

return CalculateStandardDeviation(avgSumSquares);
}

internal override double CalculatePredictionStandardDeviation()
{
var avgSumSquares = _residualSumOfSquares / _sampleSize;

return CalculateStandardDeviation(avgSumSquares);
}

internal override int CalculateDegreesOfFreedom()
{
return _sampleSize - _paramsCount;
}
}
2 changes: 1 addition & 1 deletion src/Regression/SimpleRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SimpleRegression(double[] inputs, double[] outputs, int trainingPctSize =

Fit(TrainingInputs, TrainingOutputs);
Predictions = Transform(OutOfSampleInputs);
Metrics = new Metrics(Predictions, OutOfSampleOutputs);
Metrics = new Metrics(Predictions, OutOfSampleOutputs, inputs.Rank);
}

internal sealed override void Fit(double[] x, double[] y)
Expand Down
3 changes: 2 additions & 1 deletion testconsole/AiDotNetTestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions tests/AiDotNetUnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;netstandard2.1;net48</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down