Class Vec
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"/>
Assembly: TimeSeriesAnalysis.dll
Syntax
Constructors
|
Edit this page
View Source
Vec(double, double)
Declaration
public Vec(double nanValue = -9999, double valuteToReturnElementIsNaN = NaN)
Parameters
Type |
Name |
Description |
double |
nanValue |
inputs values matching this value are treated as "NaN"
and are excluded from all calculations
|
double |
valuteToReturnElementIsNaN |
value to return in elementwise calculations to indiate NaN output
|
Methods
|
Edit this page
View Source
Abs(double[])
Returns an array where each value is the absolute value of array1.
Declaration
public double[] Abs(double[] array1)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
Returns
|
Edit this page
View Source
Add(double[], double)
Elementwise addition of val2 to array1.
Declaration
public double[] Add(double[] array1, double val2)
Parameters
Returns
|
Edit this page
View Source
Add(double[], double[])
Returns an array which is the elementwise addition of array1 and array2.
Declaration
public double[] Add(double[] array1, double[] array2)
Parameters
Returns
|
Edit this page
View Source
ContainsBadData(double[])
Returns true if array contains a "-9999" or NaN indicating missing data.
Declaration
public bool ContainsBadData(double[] x)
Parameters
Type |
Name |
Description |
double[] |
x |
|
Returns
|
Edit this page
View Source
Cov(double[], double[], bool)
Returns the co-variance of two arrays (interpreted as "vectors").
Declaration
public double Cov(double[] array1, double[] array2, bool doNormalize = false)
Parameters
Returns
|
Edit this page
View Source
Deserialize(string)
De-serializes a single vector/array (written by serialize).
Declaration
public static double[] Deserialize(string fileName)
Parameters
Type |
Name |
Description |
string |
fileName |
|
Returns
|
Edit this page
View Source
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".
Declaration
public double[] Diff(double[] vec, List<int> indicesToIgnore = null)
Parameters
Returns
|
Edit this page
View Source
Div(double[], double)
Divides a vector by a scalar value.
Declaration
public double[] Div(double[] vector, double scalar)
Parameters
Returns
Type |
Description |
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.
|
|
Edit this page
View Source
Div(double[], double[])
Divides two vectors of equal length.
Declaration
public double[] Div(double[] vector1, double[] vector2)
Parameters
Returns
Type |
Description |
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.
|
|
Edit this page
View Source
Equal(double[], double[])
Returns true if the two vectors are equal
Declaration
public static bool Equal(double[] arr1, double[] arr2)
Parameters
Returns
|
Edit this page
View Source
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.
Declaration
public List<int> FindValues(double[] vec, double value, VectorFindValueType type, List<int> indicesToIgnore = null)
Parameters
Returns
|
Edit this page
View Source
GetGradient(double[], DateTime[], int, int[])
Gets the gradient of a time-series.
Works by running a regression with time as the "X" variable
Declaration
public static RegressionResults GetGradient(double[] values, DateTime[] dates, int sampleTime_sec = 1, int[] indicesToIgnore = null)
Parameters
Type |
Name |
Description |
double[] |
values |
values for which the gradient is sought
|
DateTime[] |
dates |
dates corresponding to the values
|
int |
sampleTime_sec |
in what unit of time (given in seconds)the gradient shall be presented
|
int[] |
indicesToIgnore |
optional array of indices that are to be ignored during regression
|
Returns
Type |
Description |
RegressionResults |
the gradient will be the "Gain" of the returned object (in units per second by default).
|
|
Edit this page
View Source
GetValues(double[], List<int>)
Returns all the values of vec, except for those corresponding with indices in "indicesToIngore".
Declaration
public double[] GetValues(double[] vec, List<int> indicesToIgnore)
Parameters
Returns
|
Edit this page
View Source
IsAllNaN(double[])
Returns true if all elements in the array are "-9999" or Double.NaN, or is null.
Declaration
public bool IsAllNaN(double[] array)
Parameters
Type |
Name |
Description |
double[] |
array |
|
Returns
|
Edit this page
View Source
IsAllValue(double[], double)
Returns true if all elements in the array are the specific value.
Declaration
public static bool IsAllValue(double[] array, double value = 0)
Parameters
Returns
|
Edit this page
View Source
Max(double[])
Returns the maximum value of the array.
Declaration
public double Max(double[] array)
Parameters
Type |
Name |
Description |
double[] |
array |
|
Returns
|
Edit this page
View Source
Max(double[], List<int>)
Returns the maximum value of the array, ignoring the given indices.
Declaration
public double Max(double[] array, List<int> indicesToIgnore)
Parameters
Returns
|
Edit this page
View Source
Max(double[], double)
Returns the element-wise maximum of the array element and value.
Declaration
public double[] Max(double[] array, double value)
Parameters
Returns
|
Edit this page
View Source
Max(double[], double[])
Returns the maximum value of two arrays as a new array.
Declaration
public double[] Max(double[] array1, double[] array2)
Parameters
Returns
|
Edit this page
View Source
Max(double[], out int)
Returns the maximum value of the array and the index of the maximum value.
Declaration
public double Max(double[] array, out int ind)
Parameters
Returns
|
Edit this page
View Source
Mean(double[])
Returns the mean value of array1.
Declaration
public double? Mean(double[] array1)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
Returns
|
Edit this page
View Source
Min(double[])
Returns the minimum value of the array.
Declaration
public double Min(double[] array)
Parameters
Type |
Name |
Description |
double[] |
array |
|
Returns
|
Edit this page
View Source
Min(double[], List<int>)
Returns the minimum value of the array, ignoring certain indices.
Declaration
public double Min(double[] array, List<int> indicesToIgnore)
Parameters
Returns
|
Edit this page
View Source
Min(double[], double)
Returns the element-wise minimum of the array elements and value.
Declaration
public double[] Min(double[] array, double value)
Parameters
Returns
|
Edit this page
View Source
Min(double[], double[])
Returns the minimum value of the two arrays as new array.
Declaration
public static double[] Min(double[] array1, double[] array2)
Parameters
Returns
|
Edit this page
View Source
Min(double[], out int)
Returns the minimum value of the array and the index of the maximum value.
Declaration
public double Min(double[] array, out int ind)
Parameters
Returns
|
Edit this page
View Source
Multiply(double[], double)
Elementwise multiplication of val2 to array1.
Declaration
public double[] Multiply(double[] array1, double val2)
Parameters
Returns
|
Edit this page
View Source
Multiply(double[], double[])
Elementwise multiplication of array1 and array2, assuming they are same size.
Declaration
public double[] Multiply(double[] array1, double[] array2)
Parameters
Returns
|
Edit this page
View Source
NonCausalSmooth(double[], int)
Smooths the array without phase-shifting by using both past and future values
(i.e. so called non-causal smoothing).
Declaration
public double[] NonCausalSmooth(double[] array1, int kernel = 1)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
int |
kernel |
|
Returns
|
Edit this page
View Source
Pow(double[], double)
Calculates the power of the array.
Declaration
public double[] Pow(double[] array, double factor)
Parameters
Returns
|
Edit this page
View Source
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.
Declaration
public double RSquared(double[] vector1, double[] vector2, List<int> indToIgnoreExt = null, int ymodOffset = -1)
Parameters
Type |
Name |
Description |
double[] |
vector1 |
first vector
|
double[] |
vector2 |
second vector
|
List<int> |
indToIgnoreExt |
optionally: indices to be ignored (for instance bad values)
|
int |
ymodOffset |
set the offset beteen vector1 and vector2 (for difference equations, ymod is offset by -1 from ymeas). -1 is default.
|
Returns
Type |
Description |
double |
R2 squared, a value between -1 and 1 . If an error occured,
Double.PositiveInfinity is returned.
|
|
Edit this page
View Source
Rand(int, double, double, int?)
Create a vector of random numbers.
Declaration
public static double[] Rand(int N, double minValue = 0, double maxValue = 1, int? seed = null)
Parameters
Type |
Name |
Description |
int |
N |
the number of samples of the returned array
|
double |
minValue |
lower end of random number range
|
double |
maxValue |
higher end of random number range
|
int? |
seed |
optionally, give in a seed number, this makes random sequence repeatable
|
Returns
Type |
Description |
double[] |
an array of size N of random numbers between minValue and maxValue.
|
|
Edit this page
View Source
Range(double[])
Returns the range of an array; the difference between minimum and maximum element values.
Declaration
public double Range(double[] array)
Parameters
Type |
Name |
Description |
double[] |
array |
|
Returns
|
Edit this page
View Source
Regress(double[], double[,], int[])
Robust linear regression.
Declaration
public RegressionResults Regress(double[] Y, double[,] X, int[] yIndToIgnore = null)
Parameters
Type |
Name |
Description |
double[] |
Y |
vector of response variable values (to be modelled)
|
double[,] |
X |
2D matrix of manipulated values/independent values/regressors used to explain Y
|
int[] |
yIndToIgnore |
(optional) a list of the indices of values in Y to ignore in regression. By default it is null
|
Returns
Type |
Description |
RegressionResults |
an object of the RegressionResult class with the parameters, as well as
some statistics on the fit and uncertainty thereof.
|
|
Edit this page
View Source
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.
Declaration
public RegressionResults RegressRegularized(double[] Y, double[][] X, int[] yIndToIgnore = null, List<int> XindicesToRegularize = null)
Parameters
Type |
Name |
Description |
double[] |
Y |
vector of output variable values to be modelled
|
double[][] |
X |
jagged 2D matrix of manipulated values/independent values/regressors used to explain Y
|
int[] |
yIndToIgnore |
(optional) a list of the indices of values in Y to ignore in regression. By default it is null
|
List<int> |
XindicesToRegularize |
(optional) only the indices in this list are to be regularized to zero
|
Returns
Type |
Description |
RegressionResults |
an object of the RegressionResult class with the parameters, as well as
some statistics on the fit and uncertainty thereof.
|
|
Edit this page
View Source
RegressUnRegularized(double[], double[][], int[])
Regression that does not attempt to regualarize inputs toward zero.
Declaration
public RegressionResults RegressUnRegularized(double[] Y, double[][] X, int[] yIndToIgnore = null)
Parameters
Returns
|
Edit this page
View Source
ReplaceIndWithValue(double[], List<int>, double)
Replace certain values in an array with a new value.
Declaration
public static double[] ReplaceIndWithValue(double[] array, List<int> indList, double valueToReplaceWith)
Parameters
Type |
Name |
Description |
double[] |
array |
the array to be replaced
|
List<int> |
indList |
list of all the indices of all data points in the array to be replaced
|
double |
valueToReplaceWith |
the new value to use in place of old values
|
Returns
Type |
Description |
double[] |
a copy of the original array with the values replaced as specified.
|
|
Edit this page
View Source
ReplaceValuesAbove(double[], double, double)
Replace values below a certain threshold in an array with a new value.
Declaration
public static double[] ReplaceValuesAbove(double[] array, double threshold, double valueToReplaceWith)
Parameters
Returns
|
Edit this page
View Source
ReplaceValuesAboveOrBelow(double[], double, double, double)
Replace values above a higher threshold or below a lower threshold with a new value.
Declaration
public static double[] ReplaceValuesAboveOrBelow(double[] array, double lowerThreshold, double higherThreshold, double valueToReplaceWith)
Parameters
Returns
|
Edit this page
View Source
ReplaceValuesBelow(double[], double, double)
Replace all values above a certain threshold in array with a new value.
Declaration
public static double[] ReplaceValuesBelow(double[] array, double threshold, double valueToReplaceWith)
Parameters
Returns
|
Edit this page
View Source
SelfSumOfAbsErr(double[])
Sum of the absolute errors of the vector compared to itself.
Declaration
public double SelfSumOfAbsErr(double[] vec)
Parameters
Type |
Name |
Description |
double[] |
vec |
|
Returns
|
Edit this page
View Source
SelfSumOfSquareErr(double[])
Sum of the square errors of the vector compared to itself
Declaration
public double SelfSumOfSquareErr(double[] vec)
Parameters
Type |
Name |
Description |
double[] |
vec |
|
Returns
|
Edit this page
View Source
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
.
Declaration
public static bool Serialize(double[] vector, string fileName)
Parameters
Type |
Name |
Description |
double[] |
vector |
vector to be written to a file
|
string |
fileName |
the file name (or path) of the file to which the vector is to serialized to
|
Returns
|
Edit this page
View Source
Subtract(double[], double)
Elementwise subtraction of val2 from array1.
Declaration
public double[] Subtract(double[] array1, double val2)
Parameters
Returns
|
Edit this page
View Source
Subtract(double[], double[])
Elementwise subtraction of array1 and array2, assuming they are same size.
Declaration
public double[] Subtract(double[] array1, double[] array2)
Parameters
Returns
|
Edit this page
View Source
Sum(double[])
Returns the elementwise sum of array1.
Declaration
public double? Sum(double[] array1)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
Returns
|
Edit this page
View Source
SumOfAbsErr(double[], double[], int)
The sum of absolute errors (|a1-a2|)
between array1
and array2
.
Declaration
public double SumOfAbsErr(double[] array1, double[] array2, int indexOffset = -1)
Parameters
Returns
|
Edit this page
View Source
SumOfSquareErr(double[], double, bool)
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.
Declaration
public static double SumOfSquareErr(double[] vec, double constant, bool doNormalization = true)
Parameters
Returns
|
Edit this page
View Source
SumOfSquareErr(double[], double[], int, bool, List<int>)
The sum of square errors (a1-a2)^2
between array1
and array2
.
Declaration
public double SumOfSquareErr(double[] array1, double[] array2, int ymodOffset = -1, bool divByN = true, List<int> indToIgnore = null)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
double[] |
array2 |
|
int |
ymodOffset |
|
bool |
divByN |
if true, the result is normalized by the number of good values
|
List<int> |
indToIgnore |
optionally a list of indices of array1 to ignore
|
Returns
|
Edit this page
View Source
ToString(double[], int, string)
Create a compact string of vector with a certain number of significant digits and a chosen divider.
Declaration
public static string ToString(double[] array, int nSignificantDigits, string dividerStr = ";")
Parameters
Type |
Name |
Description |
double[] |
array |
|
int |
nSignificantDigits |
|
string |
dividerStr |
|
Returns
|
Edit this page
View Source
Var(double[], bool)
Returns the variance of the array (always a positive number).
Declaration
public double Var(double[] array1, bool doNormalize = false)
Parameters
Type |
Name |
Description |
double[] |
array1 |
|
bool |
doNormalize |
|
Returns