Class Vec
- Namespace
- TimeSeriesAnalysis
- Assembly
- TimeSeriesAnalysis.dll
Utility functions and operations for treating arrays as mathematical vectors.
This class considers doubles, methods that require comparisons cannot be easily ported to generic "Vec"/>
public class Vec
- Inheritance
-
Vec
- Inherited Members
Constructors
Vec(double, double)
Constructor
public Vec(double nanValue = -9999, double valuteToReturnElementIsNaN = NaN)
Parameters
nanValuedoubleinputs values matching this value are treated as "NaN" and are excluded from all calculations
valuteToReturnElementIsNaNdoublevalue to return in elementwise calculations to indiate NaN output
Methods
Abs(double[])
Returns an array where each value is the absolute value of array1.
public double[] Abs(double[] array1)
Parameters
array1double[]
Returns
- double[]
Add(double[], double)
Elementwise addition of val2 to array1.
public double[] Add(double[] array1, double val2)
Parameters
Returns
- double[]
Add(double[], double[])
Returns an array which is the elementwise addition of array1 and array2.
public double[] Add(double[] array1, double[] array2)
Parameters
Returns
- double[]
ContainsBadData(double[])
Returns true if array contains a "-9999" or NaN indicating missing data.
public bool ContainsBadData(double[] x)
Parameters
xdouble[]
Returns
Cov(double[], double[], bool)
Returns the co-variance of two arrays (interpreted as "vectors").
public double Cov(double[] array1, double[] array2, bool doNormalize = false)
Parameters
Returns
Deserialize(string)
De-serializes a single vector/array (written by serialize).
public static double[] Deserialize(string fileName)
Parameters
fileNamestring
Returns
- double[]
Diff(double[], List<int>)
Return an array of the differences between the neighboring items in array but ignores indices in the array that are in "indicesToIgnore".
public double[] Diff(double[] vec, List<int> indicesToIgnore = null)
Parameters
Returns
- double[]
Div(double[], double)
Divides a vector by a scalar value.
public double[] Div(double[] vector, double scalar)
Parameters
Returns
- double[]
a vector of values representing the array divided by a scalar. In case of NaN inputs or divide-by-zero, NaN elements are returned.
Div(double[], double[])
Divides two vectors of equal length.
public double[] Div(double[] vector1, double[] vector2)
Parameters
Returns
- double[]
a vector of values representing the array divided by a scalar. In case of NaN inputs or divide-by-zero, NaN elements are returned.
Equal(double[], double[])
Returns true if the two vectors are equal
public static bool Equal(double[] arr1, double[] arr2)
Parameters
Returns
FindValues(double[], double, VectorFindValueType, List<int>)
Return the indices of elements in the array that have a certain relation to value given type (bigger, smaller, equal, etc.). Also capable of finding NaN values.
public List<int> FindValues(double[] vec, double value, VectorFindValueType type, List<int> indicesToIgnore = null)
Parameters
vecdouble[]valuedoublesome types of searches require a value, otherwise can be left as anything
typeVectorFindValueTypeindicesToIgnoreList<int>if given, these indices are ignored when doing the operation
Returns
GetGradient(double[], DateTime[], int, int[])
Gets the gradient of a time-series.
Works by running a regression with time as the "X" variable
public static RegressionResults GetGradient(double[] values, DateTime[] dates, int sampleTime_sec = 1, int[] indicesToIgnore = null)
Parameters
valuesdouble[]values for which the gradient is sought
datesDateTime[]dates corresponding to the values
sampleTime_secintin what unit of time (given in seconds)the gradient shall be presented
indicesToIgnoreint[]optional array of indices that are to be ignored during regression
Returns
- RegressionResults
the gradient will be the "Gain" of the returned object (in units per second by default).
GetValues(double[], List<int>)
Returns all the values of vec, except for those corresponding with indices in "indicesToIngore".
public double[] GetValues(double[] vec, List<int> indicesToIgnore)
Parameters
Returns
- double[]
IsAllNaN(double[])
Returns true if all elements in the array are "-9999" or Double.NaN, or is null.
public bool IsAllNaN(double[] array)
Parameters
arraydouble[]
Returns
IsAllValue(double[], double)
Returns true if all elements in the array are the specific value.
public static bool IsAllValue(double[] array, double value = 0)
Parameters
Returns
Max(double[])
Returns the maximum value of the array.
public double Max(double[] array)
Parameters
arraydouble[]
Returns
Max(double[], List<int>)
Returns the maximum value of the array, ignoring the given indices.
public double Max(double[] array, List<int> indicesToIgnore)
Parameters
Returns
Max(double[], double)
Returns the element-wise maximum of the array element and value.
public double[] Max(double[] array, double value)
Parameters
Returns
- double[]
Max(double[], double[])
Returns the maximum value of two arrays as a new array.
public double[] Max(double[] array1, double[] array2)
Parameters
Returns
- double[]
Max(double[], out int)
Returns the maximum value of the array and the index of the maximum value.
public double Max(double[] array, out int ind)
Parameters
Returns
Mean(double[])
Returns the mean value of array1.
public double? Mean(double[] array1)
Parameters
array1double[]
Returns
Mean(double[], List<int>)
Returns the mean value of array1. while ignoring given indices
public double? Mean(double[] array1, List<int> indToIgnore = null)
Parameters
Returns
Min(double[])
Returns the minimum value of the array.
public double Min(double[] array)
Parameters
arraydouble[]
Returns
Min(double[], List<int>)
Returns the minimum value of the array, ignoring certain indices.
public double Min(double[] array, List<int> indicesToIgnore)
Parameters
Returns
Min(double[], double)
Returns the element-wise minimum of the array elements and value.
public double[] Min(double[] array, double value)
Parameters
Returns
- double[]
Min(double[], double[])
Returns the minimum value of the two arrays as new array.
public static double[] Min(double[] array1, double[] array2)
Parameters
Returns
- double[]
Min(double[], out int)
Returns the minimum value of the array and the index of the maximum value.
public double Min(double[] array, out int ind)
Parameters
Returns
Multiply(double[], double)
Elementwise multiplication of val2 to array1.
public double[] Multiply(double[] array1, double val2)
Parameters
Returns
- double[]
Multiply(double[], double[])
Elementwise multiplication of array1 and array2, assuming they are same size.
public double[] Multiply(double[] array1, double[] array2)
Parameters
Returns
- double[]
NonCausalSmooth(double[], int)
Smooths the array without phase-shifting by using both past and future values (i.e. so called non-causal smoothing).
public double[] NonCausalSmooth(double[] array1, int kernel = 1)
Parameters
Returns
- double[]
Pow(double[], double)
Calculates the power of the array.
public double[] Pow(double[] array, double factor)
Parameters
Returns
- double[]
RSquared(double[], double[], List<int>, int)
R-squared.
R-squared (R2) is a statistical measure that represents the proportion of the variance for a dependent
variable that's explained by an independent variable or variables in a regression model.
Whereas correlation explains the strength of the relationship between an independent and
dependent variable, R-squared explains to what extent the variance of one variable explains the
variance of the second variable. So, if the R2 of a model is 0.50, then approximately
half of the observed variation can be explained by the model's inputs.
public double RSquared(double[] vector1, double[] vector2, List<int> indToIgnoreExt = null, int ymodOffset = -1)
Parameters
vector1double[]first vector
vector2double[]second vector
indToIgnoreExtList<int>optionally: indices to be ignored (for instance bad values)
ymodOffsetintset the offset beteen vector1 and vector2 (for difference equations, ymod is offset by -1 from ymeas). -1 is default.
Returns
- double
R2 squared, a value between
-1and1. If an error occured,Double.PositiveInfinityis returned.
Rand(int, double, double, int?)
Create a vector of random numbers.
public static double[] Rand(int N, double minValue = 0, double maxValue = 1, int? seed = null)
Parameters
Nintthe number of samples of the returned array
minValuedoublelower end of random number range
maxValuedoublehigher end of random number range
seedint?optionally, give in a seed number, this makes random sequence repeatable
Returns
- double[]
an array of size N of random numbers between minValue and maxValue.
Range(double[])
Returns the range of an array; the difference between minimum and maximum element values.
public double Range(double[] array)
Parameters
arraydouble[]
Returns
Regress(double[], double[,], int[])
Robust linear regression.
public RegressionResults Regress(double[] Y, double[,] X, int[] yIndToIgnore = null)
Parameters
Ydouble[]vector of response variable values (to be modelled)
Xdouble[,]2D matrix of manipulated values/independent values/regressors used to explain Y
yIndToIgnoreint[](optional) a list of the indices of values in Y to ignore in regression. By default it is
null
Returns
- RegressionResults
an object of the
RegressionResultclass with the parameters, as well as some statistics on the fit and uncertainty thereof.
RegressRegularized(double[], double[][], int[], List<int>)
Robust linear regression, regularized. To avoid parameters taking on extremely high values in the case of a little excitation in the inputs, two mitigating actions are implemented by the solver to be "robust":
- a "robust" Singular Value Decomposition (SVD) -based solver is used.
- a regularization term is added to the objective function that will bring parameters to zero if (Y,X) does not contain any information to force the parameter away from zero.
public RegressionResults RegressRegularized(double[] Y, double[][] X, int[] yIndToIgnore = null, List<int> XindicesToRegularize = null)
Parameters
Ydouble[]vector of output variable values to be modelled
Xdouble[][]jagged 2D matrix of manipulated values/independent values/regressors used to explain Y
yIndToIgnoreint[](optional) a list of the indices of values in Y to ignore in regression. By default it is
nullXindicesToRegularizeList<int>(optional) only the indices in this list are to be regularized to zero
Returns
- RegressionResults
an object of the
RegressionResultclass with the parameters, as well as some statistics on the fit and uncertainty thereof.
RegressUnRegularized(double[], double[][], int[])
Regression that does not attempt to regualarize inputs toward zero.
public RegressionResults RegressUnRegularized(double[] Y, double[][] X, int[] yIndToIgnore = null)
Parameters
Returns
ReplaceIndWithValue(double[], List<int>, double)
Replace certain values in an array with a new value.
public static double[] ReplaceIndWithValue(double[] array, List<int> indList, double valueToReplaceWith)
Parameters
arraydouble[]the array to be replaced
indListList<int>list of all the indices of all data points in the array to be replaced
valueToReplaceWithdoublethe new value to use in place of old values
Returns
- double[]
a copy of the original array with the values replaced as specified.
ReplaceValuesAbove(double[], double, double)
Replace values below a certain threshold in an array with a new value.
public static double[] ReplaceValuesAbove(double[] array, double threshold, double valueToReplaceWith)
Parameters
Returns
- double[]
ReplaceValuesAboveOrBelow(double[], double, double, double)
Replace values above a higher threshold or below a lower threshold with a new value.
public static double[] ReplaceValuesAboveOrBelow(double[] array, double lowerThreshold, double higherThreshold, double valueToReplaceWith)
Parameters
Returns
- double[]
ReplaceValuesBelow(double[], double, double)
Replace all values above a certain threshold in array with a new value.
public static double[] ReplaceValuesBelow(double[] array, double threshold, double valueToReplaceWith)
Parameters
Returns
- double[]
SelfSumOfAbsErr(double[])
Sum of the absolute errors of the vector compared to itself.
public double SelfSumOfAbsErr(double[] vec)
Parameters
vecdouble[]
Returns
SelfSumOfSquareErr(double[])
Sum of the square errors of the vector compared to itself
public double SelfSumOfSquareErr(double[] vec)
Parameters
vecdouble[]
Returns
Serialize(double[], string)
Serializes a single vector/array to a file for persistent storage to a human-readable text format.
Vector data can then be retreived by companion method Deserialize.
public static bool Serialize(double[] vector, string fileName)
Parameters
vectordouble[]vector to be written to a file
fileNamestringthe file name (or path) of the file to which the vector is to serialized to
Returns
Subtract(double[], double)
Elementwise subtraction of val2 from array1.
public double[] Subtract(double[] array1, double val2)
Parameters
Returns
- double[]
Subtract(double[], double[])
Elementwise subtraction of array1 and array2, assuming they are same size.
public double[] Subtract(double[] array1, double[] array2)
Parameters
Returns
- double[]
Sum(double[])
Returns the elementwise sum of array1.
public double? Sum(double[] array1)
Parameters
array1double[]
Returns
SumOfAbsErr(double[], double[], int)
The sum of absolute errors (|a1-a2|) between array1 and array2.
public double SumOfAbsErr(double[] array1, double[] array2, int indexOffset = -1)
Parameters
Returns
SumOfSquareErr(double[], double, bool, List<int>)
Sum of the square errors of the vector compared to a constant. by default the return value is normalized by dividing by the number of elements; this normalization can be turned off.
public static double SumOfSquareErr(double[] vec, double constant, bool doNormalization = true, List<int> indToIgnore = null)
Parameters
Returns
SumOfSquareErr(double[], double[], int, bool, List<int>)
The sum of square errors (a1-a2)^2 between array1 and array2.
public double SumOfSquareErr(double[] array1, double[] array2, int ymodOffset = -1, bool divByN = true, List<int> indToIgnore = null)
Parameters
array1double[]array2double[]ymodOffsetintdivByNboolif true, the result is normalized by the number of good values
indToIgnoreList<int>optionally a list of indices of
array1to ignore
Returns
ToString(double[], int, string)
Create a compact string of vector with a certain number of significant digits and a chosen divider.
public static string ToString(double[] array, int nSignificantDigits, string dividerStr = ";")
Parameters
Returns
Var(double[], bool)
Returns the variance of the array (always a positive number).
public double Var(double[] array1, bool doNormalize = false)