Table of Contents

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

nanValue double

inputs values matching this value are treated as "NaN" and are excluded from all calculations

valuteToReturnElementIsNaN double

value 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

array1 double[]

Returns

double[]

Add(double[], double)

Elementwise addition of val2 to array1.

public double[] Add(double[] array1, double val2)

Parameters

array1 double[]
val2 double

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

array1 double[]
array2 double[]

Returns

double[]

ContainsBadData(double[])

Returns true if array contains a "-9999" or NaN indicating missing data.

public bool ContainsBadData(double[] x)

Parameters

x double[]

Returns

bool

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

array1 double[]
array2 double[]
doNormalize bool

Returns

double

Deserialize(string)

De-serializes a single vector/array (written by serialize).

public static double[] Deserialize(string fileName)

Parameters

fileName string

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

vec double[]
indicesToIgnore List<int>

Returns

double[]

Div(double[], double)

Divides a vector by a scalar value.

public double[] Div(double[] vector, double scalar)

Parameters

vector double[]
scalar double

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

vector1 double[]
vector2 double[]

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

arr1 double[]
arr2 double[]

Returns

bool

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

vec double[]
value double

some types of searches require a value, otherwise can be left as anything

type VectorFindValueType
indicesToIgnore List<int>

if given, these indices are ignored when doing the operation

Returns

List<int>

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

values double[]

values for which the gradient is sought

dates DateTime[]

dates corresponding to the values

sampleTime_sec int

in what unit of time (given in seconds)the gradient shall be presented

indicesToIgnore int[]

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

vec double[]
indicesToIgnore List<int>

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

array double[]

Returns

bool

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

array double[]
value double

Returns

bool

Max(double[])

Returns the maximum value of the array.

public double Max(double[] array)

Parameters

array double[]

Returns

double

Max(double[], List<int>)

Returns the maximum value of the array, ignoring the given indices.

public double Max(double[] array, List<int> indicesToIgnore)

Parameters

array double[]
indicesToIgnore List<int>

Returns

double

Max(double[], double)

Returns the element-wise maximum of the array element and value.

public double[] Max(double[] array, double value)

Parameters

array double[]
value double

Returns

double[]

Max(double[], double[])

Returns the maximum value of two arrays as a new array.

public double[] Max(double[] array1, double[] array2)

Parameters

array1 double[]
array2 double[]

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

array double[]
ind int

Returns

double

Mean(double[])

Returns the mean value of array1.

public double? Mean(double[] array1)

Parameters

array1 double[]

Returns

double?

Mean(double[], List<int>)

Returns the mean value of array1. while ignoring given indices

public double? Mean(double[] array1, List<int> indToIgnore = null)

Parameters

array1 double[]
indToIgnore List<int>

Returns

double?

Min(double[])

Returns the minimum value of the array.

public double Min(double[] array)

Parameters

array double[]

Returns

double

Min(double[], List<int>)

Returns the minimum value of the array, ignoring certain indices.

public double Min(double[] array, List<int> indicesToIgnore)

Parameters

array double[]
indicesToIgnore List<int>

Returns

double

Min(double[], double)

Returns the element-wise minimum of the array elements and value.

public double[] Min(double[] array, double value)

Parameters

array double[]
value double

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

array1 double[]
array2 double[]

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

array double[]
ind int

Returns

double

Multiply(double[], double)

Elementwise multiplication of val2 to array1.

public double[] Multiply(double[] array1, double val2)

Parameters

array1 double[]
val2 double

Returns

double[]

Multiply(double[], double[])

Elementwise multiplication of array1 and array2, assuming they are same size.

public double[] Multiply(double[] array1, double[] array2)

Parameters

array1 double[]
array2 double[]

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

array1 double[]
kernel int

Returns

double[]

Pow(double[], double)

Calculates the power of the array.

public double[] Pow(double[] array, double factor)

Parameters

array double[]
factor double

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

vector1 double[]

first vector

vector2 double[]

second vector

indToIgnoreExt List<int>

optionally: indices to be ignored (for instance bad values)

ymodOffset int

set 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 -1 and 1. If an error occured, Double.PositiveInfinity is 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

N int

the number of samples of the returned array

minValue double

lower end of random number range

maxValue double

higher end of random number range

seed int?

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

array double[]

Returns

double

Regress(double[], double[,], int[])

Robust linear regression.

public RegressionResults Regress(double[] Y, double[,] X, int[] yIndToIgnore = null)

Parameters

Y double[]

vector of response variable values (to be modelled)

X double[,]

2D matrix of manipulated values/independent values/regressors used to explain Y

yIndToIgnore int[]

(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 RegressionResult class 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

Y double[]

vector of output variable values to be modelled

X double[][]

jagged 2D matrix of manipulated values/independent values/regressors used to explain Y

yIndToIgnore int[]

(optional) a list of the indices of values in Y to ignore in regression. By default it is null

XindicesToRegularize List<int>

(optional) only the indices in this list are to be regularized to zero

Returns

RegressionResults

an object of the RegressionResult class 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

Y double[]
X double[][]
yIndToIgnore int[]

Returns

RegressionResults

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

array double[]

the array to be replaced

indList List<int>

list of all the indices of all data points in the array to be replaced

valueToReplaceWith double

the 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

array double[]
threshold double
valueToReplaceWith double

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

array double[]
lowerThreshold double
higherThreshold double
valueToReplaceWith double

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

array double[]
threshold double
valueToReplaceWith double

Returns

double[]

SelfSumOfAbsErr(double[])

Sum of the absolute errors of the vector compared to itself.

public double SelfSumOfAbsErr(double[] vec)

Parameters

vec double[]

Returns

double

SelfSumOfSquareErr(double[])

Sum of the square errors of the vector compared to itself

public double SelfSumOfSquareErr(double[] vec)

Parameters

vec double[]

Returns

double

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

vector double[]

vector to be written to a file

fileName string

the file name (or path) of the file to which the vector is to serialized to

Returns

bool

Subtract(double[], double)

Elementwise subtraction of val2 from array1.

public double[] Subtract(double[] array1, double val2)

Parameters

array1 double[]
val2 double

Returns

double[]

Subtract(double[], double[])

Elementwise subtraction of array1 and array2, assuming they are same size.

public double[] Subtract(double[] array1, double[] array2)

Parameters

array1 double[]
array2 double[]

Returns

double[]

Sum(double[])

Returns the elementwise sum of array1.

public double? Sum(double[] array1)

Parameters

array1 double[]

Returns

double?

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

array1 double[]
array2 double[]
indexOffset int

Returns

double

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

vec double[]
constant double
doNormalization bool
indToIgnore List<int>

Returns

double

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

array1 double[]
array2 double[]
ymodOffset int
divByN bool

if true, the result is normalized by the number of good values

indToIgnore List<int>

optionally a list of indices of array1 to ignore

Returns

double

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

array double[]
nSignificantDigits int
dividerStr string

Returns

string

Var(double[], bool)

Returns the variance of the array (always a positive number).

public double Var(double[] array1, bool doNormalize = false)

Parameters

array1 double[]
doNormalize bool

Returns

double