Skip to content

Commit 22f33b5

Browse files
committed
Added new time series regression models with more to come
1 parent ab4aa70 commit 22f33b5

34 files changed

+3705
-207
lines changed

src/Enums/DistanceMetricType.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum DistanceMetricType
4+
{
5+
Euclidean,
6+
Manhattan,
7+
Cosine,
8+
Jaccard,
9+
Hamming,
10+
Mahalanobis
11+
}

src/Enums/GradientType.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum GradientType
4+
{
5+
Omega,
6+
Alpha,
7+
Beta
8+
}

src/Enums/MetricType.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ public enum MetricType
1111
Precision,
1212
Recall,
1313
F1Score,
14-
PredictionIntervalCoverage
14+
PredictionIntervalCoverage,
15+
PearsonCorrelation,
16+
SpearmanCorrelation,
17+
KendallTau
1518
}

src/Helpers/MathHelper.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,23 @@ public static T Clamp<T>(T value, T min, T max)
4343
return value;
4444
}
4545

46-
public static bool IsInteger<T>(T value, INumericOperations<T> numOps)
46+
public static bool IsInteger<T>(T value)
4747
{
4848
// If the value is equal to its rounded value, it's an integer
49+
var numOps = GetNumericOperations<T>();
4950
return numOps.Equals(value, numOps.Round(value));
5051
}
5152

52-
public static bool AlmostEqual<T>(T a, T b, T tolerance, INumericOperations<T> numOps)
53+
public static bool AlmostEqual<T>(T a, T b, T tolerance)
5354
{
55+
var numOps = GetNumericOperations<T>();
5456
return numOps.LessThan(numOps.Abs(numOps.Subtract(a, b)), tolerance);
5557
}
5658

57-
public static bool AlmostEqual<T>(T a, T b, INumericOperations<T> numOps)
59+
public static bool AlmostEqual<T>(T a, T b)
5860
{
59-
return AlmostEqual(a, b, numOps.FromDouble(1e-8), numOps);
61+
var numOps = GetNumericOperations<T>();
62+
return AlmostEqual(a, b, numOps.FromDouble(1e-8));
6063
}
6164

6265
public static T Factorial<T>(int n)

src/Helpers/SerializationHelper.cs

+111-89
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Serialization.Formatters.Binary;
2+
13
namespace AiDotNet.Helpers;
24

35
public static class SerializationHelper<T>
@@ -55,78 +57,6 @@ public static void SerializeNode(DecisionTreeNode<T>? node, BinaryWriter writer)
5557
return node;
5658
}
5759

58-
public static void SerializeVector(BinaryWriter writer, Vector<T> vector)
59-
{
60-
writer.Write(vector.Length);
61-
62-
foreach (var value in vector)
63-
{
64-
writer.Write(Convert.ToDouble(value));
65-
}
66-
}
67-
68-
public static Vector<T> DeserializeVector(BinaryReader reader)
69-
{
70-
int count = reader.ReadInt32();
71-
T[] values = new T[count];
72-
for (int i = 0; i < count; i++)
73-
{
74-
values[i] = (T)Convert.ChangeType(reader.ReadDouble(), typeof(T));
75-
}
76-
77-
return new Vector<T>(values);
78-
}
79-
80-
public static void WriteMatrix(BinaryWriter writer, Matrix<T> matrix)
81-
{
82-
writer.Write(matrix.Rows);
83-
writer.Write(matrix.Columns);
84-
for (int i = 0; i < matrix.Rows; i++)
85-
{
86-
for (int j = 0; j < matrix.Columns; j++)
87-
{
88-
writer.Write(Convert.ToDouble(matrix[i, j]));
89-
}
90-
}
91-
}
92-
93-
public static Matrix<T> ReadMatrix(BinaryReader reader)
94-
{
95-
int rows = reader.ReadInt32();
96-
int cols = reader.ReadInt32();
97-
Matrix<T> matrix = new Matrix<T>(rows, cols, NumOps);
98-
for (int i = 0; i < rows; i++)
99-
{
100-
for (int j = 0; j < cols; j++)
101-
{
102-
matrix[i, j] = NumOps.FromDouble(reader.ReadDouble());
103-
}
104-
}
105-
106-
return matrix;
107-
}
108-
109-
public static void WriteVector(BinaryWriter writer, Vector<T> vector)
110-
{
111-
writer.Write(vector.Length);
112-
for (int i = 0; i < vector.Length; i++)
113-
{
114-
writer.Write(Convert.ToDouble(vector[i]));
115-
}
116-
}
117-
118-
public static Vector<T> ReadVector(BinaryReader reader)
119-
{
120-
int length = reader.ReadInt32();
121-
Vector<T> vector = new Vector<T>(length, NumOps);
122-
for (int i = 0; i < length; i++)
123-
{
124-
vector[i] = NumOps.FromDouble(reader.ReadDouble());
125-
}
126-
127-
return vector;
128-
}
129-
13060
public static void WriteValue(BinaryWriter writer, T value)
13161
{
13262
if (typeof(T) == typeof(double))
@@ -247,10 +177,8 @@ public static T ReadValue(BinaryReader reader)
247177
}
248178
}
249179

250-
public static byte[] SerializeMatrix(Matrix<T> matrix)
180+
public static void SerializeMatrix(BinaryWriter writer, Matrix<T> matrix)
251181
{
252-
using MemoryStream ms = new();
253-
using BinaryWriter writer = new(ms);
254182
writer.Write(matrix.Rows);
255183
writer.Write(matrix.Columns);
256184
for (int i = 0; i < matrix.Rows; i++)
@@ -260,54 +188,148 @@ public static byte[] SerializeMatrix(Matrix<T> matrix)
260188
writer.Write(Convert.ToDouble(matrix[i, j]));
261189
}
262190
}
191+
}
192+
193+
public static Matrix<T> DeserializeMatrix(BinaryReader reader, int rows, int columns)
194+
{
195+
int storedRows = reader.ReadInt32();
196+
int storedColumns = reader.ReadInt32();
197+
198+
if (storedRows != rows || storedColumns != columns)
199+
{
200+
throw new InvalidOperationException("Stored matrix dimensions do not match expected dimensions.");
201+
}
202+
203+
Matrix<T> matrix = new Matrix<T>(rows, columns);
204+
for (int i = 0; i < rows; i++)
205+
{
206+
for (int j = 0; j < columns; j++)
207+
{
208+
matrix[i, j] = NumOps.FromDouble(reader.ReadDouble());
209+
}
210+
}
211+
212+
return matrix;
213+
}
214+
215+
public static byte[] SerializeMatrix(Matrix<T> matrix)
216+
{
217+
using MemoryStream ms = new MemoryStream();
218+
using BinaryWriter writer = new BinaryWriter(ms);
219+
220+
writer.Write(matrix.Rows);
221+
writer.Write(matrix.Columns);
222+
for (int i = 0; i < matrix.Rows; i++)
223+
{
224+
for (int j = 0; j < matrix.Columns; j++)
225+
{
226+
WriteValue(writer, matrix[i, j]);
227+
}
228+
}
263229

264230
return ms.ToArray();
265231
}
266232

233+
public static Matrix<T> DeserializeMatrix(BinaryReader reader)
234+
{
235+
int rows = reader.ReadInt32();
236+
int columns = reader.ReadInt32();
237+
Matrix<T> matrix = new Matrix<T>(rows, columns);
238+
for (int i = 0; i < rows; i++)
239+
{
240+
for (int j = 0; j < columns; j++)
241+
{
242+
matrix[i, j] = ReadValue(reader);
243+
}
244+
}
245+
246+
return matrix;
247+
}
248+
267249
public static Matrix<T> DeserializeMatrix(byte[] data)
268250
{
269-
using MemoryStream ms = new(data);
270-
using BinaryReader reader = new(ms);
251+
using MemoryStream ms = new MemoryStream(data);
252+
using BinaryReader reader = new BinaryReader(ms);
253+
271254
int rows = reader.ReadInt32();
272255
int columns = reader.ReadInt32();
273-
Matrix<T> matrix = new(rows, columns);
256+
Matrix<T> matrix = new Matrix<T>(rows, columns);
274257
for (int i = 0; i < rows; i++)
275258
{
276259
for (int j = 0; j < columns; j++)
277260
{
278-
double value = reader.ReadDouble();
279-
matrix[i, j] = NumOps.FromDouble(value);
261+
matrix[i, j] = ReadValue(reader);
280262
}
281263
}
282264

283265
return matrix;
284266
}
285267

286-
public static byte[] SerializeVector(Vector<T> vector)
268+
public static void SerializeVector(BinaryWriter writer, Vector<T> vector)
287269
{
288-
using MemoryStream ms = new();
289-
using BinaryWriter writer = new(ms);
290270
writer.Write(vector.Length);
291271
for (int i = 0; i < vector.Length; i++)
292272
{
293273
writer.Write(Convert.ToDouble(vector[i]));
294274
}
275+
}
276+
277+
public static Vector<T> DeserializeVector(BinaryReader reader, int length)
278+
{
279+
int storedLength = reader.ReadInt32();
280+
281+
if (storedLength != length)
282+
{
283+
throw new InvalidOperationException("Stored vector length does not match expected length.");
284+
}
285+
286+
Vector<T> vector = new Vector<T>(length);
287+
for (int i = 0; i < length; i++)
288+
{
289+
vector[i] = NumOps.FromDouble(reader.ReadDouble());
290+
}
291+
292+
return vector;
293+
}
294+
295+
public static Vector<T> DeserializeVector(BinaryReader reader)
296+
{
297+
int length = reader.ReadInt32();
298+
T[] array = new T[length];
299+
for (int i = 0; i < length; i++)
300+
{
301+
array[i] = ReadValue(reader);
302+
}
303+
304+
return new Vector<T>(array);
305+
}
306+
307+
public static byte[] SerializeVector(Vector<T> vector)
308+
{
309+
using MemoryStream ms = new MemoryStream();
310+
using BinaryWriter writer = new BinaryWriter(ms);
311+
312+
writer.Write(vector.Length);
313+
for (int i = 0; i < vector.Length; i++)
314+
{
315+
WriteValue(writer, vector[i]);
316+
}
295317

296318
return ms.ToArray();
297319
}
298320

299321
public static Vector<T> DeserializeVector(byte[] data)
300322
{
301-
using MemoryStream ms = new(data);
302-
using BinaryReader reader = new(ms);
323+
using MemoryStream ms = new MemoryStream(data);
324+
using BinaryReader reader = new BinaryReader(ms);
325+
303326
int length = reader.ReadInt32();
304-
Vector<T> vector = new(length);
327+
T[] array = new T[length];
305328
for (int i = 0; i < length; i++)
306329
{
307-
double value = reader.ReadDouble();
308-
vector[i] = NumOps.FromDouble(value);
330+
array[i] = ReadValue(reader);
309331
}
310332

311-
return vector;
333+
return new Vector<T>(array);
312334
}
313335
}

0 commit comments

Comments
 (0)