Linear Regression library in pure Javascript

Overview

Lyric

Linear Regression library in pure Javascript

Lyric can help you analyze any set of x,y series data by building a model that can be used to:

  1. Create trendlines on charts
  2. Predict future values based on an existing set of data

Typical applications would include charting libraries and machine learning applications.

Lyric depends on the great Javascript Matrix library Sylvester by James Coglan available here: https://github.com/jcoglan/sylvester and the JQuery library available here: http://jquery.com/

Usage

Download the lyric.js, jquery-X.X.X.js and sylvester.js files and include them in your web application.

Include both the lyric.js and the sylvester.js files as shown below:

">



First, make sure your data is represented in the form of a 2xN Array comprised of elements with an 'x' and 'y' value. The x value should be the explanatory and the y the dependent variables.

var input = new Array();
input['x'] = new Array();
input['y'] = new Array();
input['x'][0] = 1;		input['y'][0] = 0.5;
input['x'][1] = 2;		input['y'][1] = 1.6;	
input['x'][2] = 3;		input['y'][2] = 4.5;
input['x'][3] = 4;		input['y'][3] = 7.6;
input['x'][4] = 5;		input['y'][4] = 10.1;

Then you need to have Lyric build the model for you from your data:

var model = buildModel(input);

Now that you have your model, you will likely want to apply it to a set of inputs. The newInput should be a 1xN array containing only the explanatory variable values you would like to calculate the dependent values. This will result in a new 2xN array which will include the resulting series.

var data = applyModel(model, estimationInput);

The following is a complete example which, given some values for the explanatory values 1 through 5, estimates the values of 6 through 8:

var input = new Array();
input['x'] = new Array();
input['y'] = new Array();
input['x'][0] = 1;		input['y'][0] = 0.5;
input['x'][1] = 2;		input['y'][1] = 1.6;	
input['x'][2] = 3;		input['y'][2] = 4.5;
input['x'][3] = 4;		input['y'][3] = 7.6;
input['x'][4] = 5;		input['y'][4] = 10.1;

var estimationInput = new Array();
estimationInput['x'] = new Array();
estimationInput['x'][0] = 6;
estimationInput['x'][1] = 7;
estimationInput['x'][2] = 8;

var estimateData = applyModel(estimationInput, buildModel(input));

/** 
 * Resulting data: 
 * estimateData['x'][0] = 6; 	estimateData['y'][0] = 13.4;
 * estimateData['x'][1] = 7; 	estimateData['y'][0] = 15.7;
 * estimateData['x'][2] = 8; 	estimateData['y'][0] = 18.1;
 */	

By default Lyric will attempt to use a 2nd degree polynomial to model the data. If you would like to use a higher order polynomial for the model, just pass in the degree you would like to use in the buildModel() and applyModel() functions. For example, to model using a 4-th degree polynomial you would modify the above example as follows:

var estimateData = applyModel(estimationInput, buildModel(input, 4), 4);

Timeseries

For timeseries data using regular intervals, it is typically more efficient to use the ordinality as the explanatory value than the timestamp. For example, given the following data series:

var input = new Array();
input['x'][0] = '2012-03-01';		input['y'][0] = 0.5;
input['x'][1] = '2012-03-02';		input['y'][1] = 1.6;	
input['x'][2] = '2012-03-03';		input['y'][2] = 4.5;
input['x'][3] = '2012-03-04';		input['y'][3] = 7.6;
input['x'][4] = '2012-03-05';		input['y'][4] = 10.1;

You can turn the dates in the input[0] series into timestamps for use in modelling, but since each data point represents a single day the easier and simpler calculation is to ignore the particular days and use the ordinality. Lyric provides a convenience function for manipulating this kind of data called ordinalize() which is used as shown below:

var ordinalInput = ordinalize(input);

The resulting ordinalInput will be equivalent to having created the following input:

var input = new Array();
input['label'][0] = '2012-03-01';		input['x'][0] = 1;		input['y'][0] = 0.5;
input['label'][1] = '2012-03-01';		input['x'][1] = 2;		input['y'][1] = 1.6;	
input['label'][2] = '2012-03-01';		input['x'][2] = 3;		input['y'][2] = 4.5;
input['label'][3] = '2012-03-01';		input['x'][3] = 4;		input['y'][3] = 7.6;
input['label'][4] = '2012-03-01';		input['x'][4] = 5;		input['y'][4] = 10.1;

Lyric can then use the ordinal x values to more efficiently compute the regression. Note that if you do use this, you need to ordinalize both the input provided to build the model AND the input the model is applied.

Implementation

Lyric uses the Normal Equation (closed form) to build the linear model. You can read more about the Normal Equation here: http://mathworld.wolfram.com/NormalEquation.html

This does introduce the limitation that Lyric will not work on input data that produces a non-invertible matrix when multiplied by its transpose.

A full breakdown on Lyric is available here: http://tech.flurry.com/lyric-linear-regression-in-pure-javascript

License

Copyright 2012 Flurry, Inc. (http://flurry.com)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

You might also like...

Support Vector Machine (SVM) library for nodejs

Support Vector Machine (SVM) library for nodejs

node-svm Support Vector Machine (SVM) library for nodejs. Support Vector Machines Wikipedia : Support vector machines are supervised learning models t

Nov 6, 2022

machinelearn.js is a Machine Learning library written in Typescript

machinelearn.js is a Machine Learning library written in Typescript

machinelearn.js is a Machine Learning library written in Typescript. It solves Machine Learning problems and teaches users how Machine Learning algorithms work.

Jan 2, 2023

FANN (Fast Artificial Neural Network Library) bindings for Node.js

node-fann node-fann is a FANN bindings for Node.js. FANN (Fast Artificial Neural Network Library) is a free open source neural network library, which

Oct 31, 2022

An NLP library for building bots, with entity extraction, sentiment analysis, automatic language identify, and so more

An NLP library for building bots, with entity extraction, sentiment analysis, automatic language identify, and so more

NLP.js If you're looking for the version 3 docs, you can find them here Version 3 "NLP.js" is a general natural language utility for nodejs. Currently

Dec 29, 2022

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.

ConvNetJS ConvNetJS is a Javascript implementation of Neural networks, together with nice browser-based demos. It currently supports: Common Neural Ne

Dec 31, 2022

[UNMAINTAINED] Simple feed-forward neural network in JavaScript

This project has reached the end of its development as a simple neural network library. Feel free to browse the code, but please use other JavaScript

Dec 26, 2022

Differential Programming in JavaScript.

April 19, 2018 TensorFlow.js was recently released. It is well engineered, provides an autograd-style interface to backprop, and has committed to supp

Dec 29, 2022

Machine learning tools in JavaScript

ml.js - Machine learning tools in JavaScript Introduction This library is a compilation of the tools developed in the mljs organization. It is mainly

Jan 1, 2023

Deep Neural Network Sandbox for JavaScript.

Deep Neural Network Sandbox for JavaScript.

Deep Neural Network Sandbox for Javascript Train a neural network with your data & save it's trained state! Demo • Installation • Getting started • Do

Jan 4, 2023
Comments
  • Error in estimates?

    Error in estimates?

    I might be off on this but I can't get it to calculate using the correct example you provided. Im comparing it against Excel TREND function. Here's the data array, 12 periods, equidistant:

    0, 734000 1, 769900 2, 822450 3, 849950 4, 849950 5, 872000 6, 855000 7, 889500 8, 895000 9, 897000 10, 894500 11, 890000

    image

    If I estimate for x=24 I get 437977.98 while TREND in Excel gives me a plausible number of 922492.67.

    var estimationInput = new Array(); estimationInput['x'] = new Array(); estimationInput['x'][0] = 24; var estimateData = lyric.applyModel(estimationInput, lyric.buildModel(input));

    What am I missing here?

    opened by bibanul 0
Owner
Flurry, Inc.
Flurry, Inc.
Pure Javascript OCR for more than 100 Languages 📖🎉🖥

Version 2 is now available and under development in the master branch, read a story about v2: Why I refactor tesseract.js v2? Check the support/1.x br

Project Naptha 29.2k Dec 31, 2022
A neural network library built in JavaScript

A flexible neural network library for Node.js and the browser. Check out a live demo of a movie recommendation engine built with Mind. Features Vector

Steven Miller 1.5k Dec 31, 2022
A JavaScript deep learning and reinforcement learning library.

neurojs is a JavaScript framework for deep learning in the browser. It mainly focuses on reinforcement learning, but can be used for any neural networ

Jan 4.4k Jan 4, 2023
A WebGL accelerated JavaScript library for training and deploying ML models.

TensorFlow.js TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models. ⚠️ We recent

null 16.9k Jan 4, 2023
architecture-free neural network library for node.js and the browser

Synaptic Important: Synaptic 2.x is in stage of discussion now! Feel free to participate Synaptic is a javascript neural network library for node.js a

Juan Cazala 6.9k Dec 27, 2022
A lightweight library for neural networks that runs anywhere

Synapses A lightweight library for neural networks that runs anywhere! Getting Started Why Sypapses? It's easy Add one dependency to your project. Wri

Dimos Michailidis 65 Nov 9, 2022
A library for prototyping realtime hand detection (bounding box), directly in the browser.

Handtrack.js View a live demo in your browser here. Handtrack.js is a library for prototyping realtime hand detection (bounding box), directly in the

Victor Dibia 2.7k Jan 3, 2023
A speech recognition library running in the browser thanks to a WebAssembly build of Vosk

A speech recognition library running in the browser thanks to a WebAssembly build of Vosk

Ciaran O'Reilly 207 Jan 3, 2023
This is a JS/TS library for accelerated tensor computation intended to be run in the browser.

TensorJS TensorJS How to use Tensors Tensor operations Reading values Data types Converting between backends Onnx model support Optimizations Running

Frithjof Winkelmann 32 Jun 26, 2022
Machine Learning library for node.js

shaman Machine Learning library for node.js Linear Regression shaman supports both simple linear regression and multiple linear regression. It support

Luc Castera 108 Feb 26, 2021