Khiva

Khiva - high-performance time series algorithms - for Ruby

:fire: Runs on GPUs and CPUs

Build Status

Installation

First, install Khiva. For Homebrew, use:

brew install khiva

Add this line to your application’s Gemfile:

gem 'khiva'

Getting Started

Calculate a matrix profile

a = Khiva::Array.new([11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11])
b = Khiva::Array.new([9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9])
profile, index = Khiva::Matrix.stomp(a, b, 3)

Find discords

distances, indices, subsequences = Khiva::Matrix.find_best_n_discords(profile, index, 2, 2)

Find motifs

distances, indices, subsequences = Khiva::Matrix.find_best_n_motifs(profile, index, 2, 2)

Modules

Array

Create an array from a Ruby array

Khiva::Array.new([1, 2, 3])

Specify the type - :b8, :f32, :f64, :s16, :s32, :s64, :u8, :u16, :u32, :u64

Khiva::Array.new([1, 2, 3], type: :s64)

Get the type and dimensions

a.type
a.dims

Perform operations on arrays

a + b
a - b
a * b
a / b
a % b
a ** b

Compare arrays

a.eq(b)
a.ne(b)
a.lt(b)
a.gt(b)
a.le(b)
a.ge(b)

Clustering

k-means algorithm

centroids, labels = Khiva::Clustering.k_means(tss, k)

k-Shape algorithm

centroids, labels = Khiva::Clustering.k_shape(tss, k)

Dimensionality

Piecewise aggregate approximate (PAA)

Khiva::Dimensionality.paa(a, bins)

Distances

Dynamic time warping (DTW) distance

Khiva::Distances.dtw(tss)

Euclidean distance

Khiva::Distances.euclidean(tss)

Hamming distance

Khiva::Distances.hamming(tss)

Manhattan distance

Khiva::Distances.manhattan(tss)

Shape-based distance (SBD)

Khiva::Distances.sbd(tss)

Squared Euclidean distance

Khiva::Distances.squared_euclidean(tss)

Features

Sum of square values

Khiva::Features.abs_energy(tss)

Absolute sum of changes

Khiva::Features.absolute_sum_of_changes(tss)

Number of values above the mean

Khiva::Features.count_above_mean(tss)

Number of values below the mean

Khiva::Features.count_below_mean(tss)

The spectral centroid (mean), variance, skew, and kurtosis of the absolute fourier transform spectrum

Khiva::Features.fft_aggregated(tss)

First location of the maximum value

Khiva::Features.first_location_of_maximum(tss)

First location of the minimum value

Khiva::Features.first_location_of_minimum(tss)

Last location of the maximum value

Khiva::Features.last_location_of_maximum(tss)

Last location of the minimum value

Khiva::Features.last_location_of_minimum(tss)

Length of the series

Khiva::Features.length(tss)

Local maximals

Khiva::Features.local_maximals(tss)

Length of the longest consecutive subsequence above the mean

Khiva::Features.longest_strike_above_mean(tss)

Length of the longest consecutive subsequence below the mean

Khiva::Features.longest_strike_below_mean(tss)

Maximum

Khiva::Features.maximum(tss)

Mean

Khiva::Features.mean(tss)

Mean absolute change

Khiva::Features.mean_absolute_change(tss)

Mean change

Khiva::Features.mean_change(tss)

Mean of a central approximation of the second derivative

Khiva::Features.mean_second_derivative_central(tss)

Median

Khiva::Features.median(tss)

Minimum

Khiva::Features.minimum(tss)

Ratio of unique values

Khiva::Features.ratio_value_number_to_time_series_length(tss)

Sample entropy

Khiva::Features.sample_entropy(tss)

Skewness

Khiva::Features.skewness(tss)

Standard deviation

Khiva::Features.standard_deviation(tss)

Sum of values

Khiva::Features.sum_values(tss)

Number of occurrences of a value

Khiva::Features.value_count(tss, v)

Variance

Khiva::Features.variance(tss)

If variance is larger than one

Khiva::Features.variance_larger_than_standard_deviation(tss)

Library

Get backend info

Khiva::Library.backend_info

Get current backend

Khiva::Library.backend

Get available backends

Khiva::Library.backends

Set backend - :default, :cpu, :cuda, :opencl

Khiva::Library.set_backend(:cpu)

Set device

Khiva::Library.set_device(device_id)

Get device id

Khiva::Library.device_id

Get device count

Khiva::Library.device_count

Set device memory in GB

Khiva::Library.set_device_memory_in_gb(1.5)

Get version

Khiva::Library.version

Linalg

Khiva::Linalg.lls(a, b)

Matrix

Calculate the matrix profile between ta and tb using a subsequence length of m with the STOMP algorithm

profile, index = Khiva::Matrix.stomp(ta, tb, m)

Calculate the matrix profile between t and itself using a subsequence length of m with the STOMP algorithm

profile, index = Khiva::Matrix.stomp_self_join(t, m)

Calculate the matrix profile between ta and tb using a subsequence length of m

profile, index = Khiva::Matrix.matrix_profile(ta, tb, m)

Calculate the matrix profile between t and itself using a subsequence length of m

profile, index = Khiva::Matrix.matrix_profile_self_join(t, m)

Normalization

Decimal scaling

Khiva::Normalization.decimal_scaling_norm(tss)
Khiva::Normalization.decimal_scaling_norm!(tss)

Max min

Khiva::Normalization.max_min_norm(tss)
Khiva::Normalization.max_min_norm!(tss)

Mean

Khiva::Normalization.mean_norm(tss)
Khiva::Normalization.mean_norm!(tss)

Znorm

Khiva::Normalization.znorm(tss)
Khiva::Normalization.znorm!(tss)

Polynomial

Least squares polynomial fit

Khiva::Polynomial.polyfit(x, y, deg)

Regression

Linear least squares regression

slope, intercept, rvalue, pvalue, stderrest = Khiva::Regression.linear(xss, yss)

Regularization

Khiva::Regularization.group_by(tss, aggregation_function, columns_key: 1, n_columns_value: 1)

Statistics

Covariance

Khiva::Statistics.covariance(tss, unbiased: false)

Kurtosis

Khiva::Statistics.kurtosis(tss)

Ljung-Box

Khiva::Statistics.ljung_box(tss, lags)

Moment

Khiva::Statistics.moment(tss, k)

Quantile

Khiva::Statistics.quantile(tss, q, precision: 1e-8)

Quantiles cut

Khiva::Statistics.quantiles_cut(tss, quantiles, precision: 1e-8)

Standard deviation

Khiva::Statistics.sample_stdev(tss)

Skewness

Khiva::Statistics.skewness(tss)

Khiva Installation

Linux

Install Boost and Eigen. On Ubuntu, use:

sudo apt-get install libboost-all-dev libeigen3-dev

Install ArrayFire:

wget -q https://arrayfire.s3.amazonaws.com/3.7.3/ArrayFire-v3.7.3_Linux_x86_64.sh
chmod +x ./ArrayFire-v3.7.3_Linux_x86_64.sh
./ArrayFire-v3.7.3_Linux_x86_64.sh --include-subdir --prefix=/opt
echo /opt/arrayfire/lib64 | sudo tee /etc/ld.so.conf.d/arrayfire.conf
sudo ldconfig

And install Khiva:

git clone --recursive --branch v0.5.0 https://github.com/shapelets/khiva
cd khiva
mkdir build
cd build
cmake .. -DKHIVA_USE_CONAN=OFF -DKHIVA_BUILD_TESTS=OFF -DKHIVA_BUILD_BENCHMARKS=OFF -DKHIVA_BUILD_JNI_BINDINGS=OFF
make -j4
sudo make install

Mac

Run:

brew install khiva

Windows

See instructions.

Credits

This library is modeled after the Khiva-Python API.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/khiva-ruby.git
cd khiva-ruby
bundle install
bundle exec rake test