Skip to content

Commit

Permalink
[UPDATE] move summary stats to separate files. AIC, AICc, BIC.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Mar 29, 2015
1 parent 9f8a59c commit 481c4a0
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 9 deletions.
3 changes: 0 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ TODO
- [jarque-bera test](http://en.wikipedia.org/wiki/Jarque%E2%80%93Bera_test)
- [condition number](http://en.wikipedia.org/wiki/Condition_number)
- p-value
- log likelihood
- estimate std error
- t-value
- t-prob
- aic
- bic
- skew
- kurt
- influential observations
Expand Down
55 changes: 55 additions & 0 deletions lib/adjustedrsquared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
*
* ADJUSTED COEFFICIENT OF DETERMINATION
*
*
* DESCRIPTION:
* - Computes the adjusted coefficient of determination (adjusted R squared).
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

// MODULES //

var rsq = require( './rsquared.js' );


// ADJUSTED R SQUARED //

/**
* FUNCTION: arsq( mss, tss, mdf, rdf )
* Computes the adjusted coefficient of determination.
*
* @param {Number} mss - model sum of squares
* @param {Number} tss - total sum of squares
* @param {Number} mdf - model degrees of freedom
* @param {Number} rdf - residual degrees of freedom
* @returns {Number} adjusted coefficient of determination
*/
function arsq( mss, tss, mdf, rdf ) {
var r2 = rsq( mss, tss );
return r2 - (1-r2)*(mdf/rdf);
} // end FUNCTION arsq()


// EXPORTS //

module.exports = arsq;
45 changes: 45 additions & 0 deletions lib/aic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
*
* AIC
*
*
* DESCRIPTION:
* - Computes the Akaike information criterion.
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

/**
* FUNCTION: aic( ll, k )
* Computes the Akaike information criterion.
*
* @param {Number} ll - log likelihood
* @param {Number} k - number of parameters
* @returns {Number} Akaike information criterion
*/
function aic( ll, k ) {
return 2 * ( k - ll );
} // end FUNCTION aic()


// EXPORTS //

module.exports = aic;
53 changes: 53 additions & 0 deletions lib/aicc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
*
* AIC
*
*
* DESCRIPTION:
* - Computes the corrected Akaike information criterion for finite sample sizes.
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

// MODULES //

var aic = require( './aic.js' );


// CORRECTED AIC //

/**
* FUNCTION: aicc( ll, N, k )
* Computes the corrected Akaike information criterion for finite sample sizes.
*
* @param {Number} ll - log likelihood
* @param {Number} N - sample size
* @param {Number} k - number of parameters
* @returns {Number} Akaike information criterion
*/
function aicc( ll, N, k ) {
return aic( ll, k ) + (2*k*(k+1)) / (N-k-1);
} // end FUNCTION aicc()


// EXPORTS //

module.exports = aicc;
46 changes: 46 additions & 0 deletions lib/bic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
*
* BIC
*
*
* DESCRIPTION:
* - Computes the Bayesian information criterion.
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

/**
* FUNCTION: bic( ll, N, k )
* Computes the Bayesian information criterion.
*
* @param {Number} ll - log likelihood
* @param {Number} N - number of observations
* @param {Number} k - number of parameters
* @returns {Number} Bayesian information criterion
*/
function bic( ll, N, k ) {
return -2*ll + k*Math.log( N );
} // end FUNCTION bic()


// EXPORTS //

module.exports = bic;
45 changes: 45 additions & 0 deletions lib/loglikelihood.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
*
* LOG LIKELIHOOD
*
*
* DESCRIPTION:
* - Computes the log likelihood.
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

/**
* FUNCTION: loglikelihood( N, rss )
* Computes the log likelihood.
*
* @param {Number} N - number of observations
* @param {Number} rss - residual sum of squares (sum of squared errors)
* @returns {Number} log likelihood
*/
function loglikelihood( N, rss ) {
return -N / 2 * ( Math.log( (rss/N) * 2 * Math.PI ) + 1 );
} // end FUNCTION loglikelihood()


// EXPORTS //

module.exports = loglikelihood;
31 changes: 25 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ var isArray = require( 'validate.io-array' ),
isFunction = require( 'validate.io-function' ),
isNumber = require( 'validate.io-number' ),
isBoolean = require( 'validate.io-boolean-primitive' ),
copy = require( 'utils-copy' ),
rss = require( './rss.js' ),
copy = require( 'utils-copy' );


// FUNCTIONS //

var rss = require( './rss.js' ),
mss = require( './mss.js' ),
tss = require( './tss.js' );
tss = require( './tss.js' ),
aic = require( './aic.js' ),
aicc = require( './aicc.js' ),
bic = require( './bic.js' ),
rsq = require( './rsquared.js' ),
arsq = require( './adjustedrsquared.js' ),
ll = require( './loglikelihood.js' );


// MODEL //
Expand Down Expand Up @@ -341,10 +351,10 @@ function createModel( x, y, xmu, ymu, slope, yint ) {
summary.tss = tss( y, ymu );

// Coefficient of Determination:
summary.rsq = summary.mss / summary.tss;
summary.rsq = rsq( summary.mss, summary.tss );

// Adjusted coefficient of determination:
summary.arsq = summary.rsq - (1-summary.rsq)*(summary.mdf / summary.rdf);
summary.arsq = arsq( summary.mss, summary.tss, summary.mdf, summary.rdf );

// Sample variance:
summary.variance = summary.tss / summary.df;
Expand All @@ -365,7 +375,16 @@ function createModel( x, y, xmu, ymu, slope, yint ) {
summary.rstdev = Math.sqrt( summary.rvariance );

// Log likelihood:
summary.loglikelihood = -N / 2 * ( Math.log( (summary.rss/N) * 2 * Math.PI ) + 1 );
summary.loglikelihood = ll( N, summary.rss );

// Akaike information criterion:
summary.aic = aic( summary.loglikelihood, m );

// Corrected Akaike information criterion for finite sample sizes:
summary.aicc = aicc( summary.loglikelihood, N, m );

// Bayesian information criterion:
summary.bic = bic( summary.loglikelihood, N, m );

// F statistic:
summary.fstat = summary.mvariance / summary.rvariance;
Expand Down
45 changes: 45 additions & 0 deletions lib/rsquared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
*
* COEFFICIENT OF DETERMINATION
*
*
* DESCRIPTION:
* - Computes the coefficient of determination (R squared).
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/

'use strict';

/**
* FUNCTION: rsq( mss, tss )
* Computes the coefficient of determination.
*
* @param {Number} mss - model sum of squares
* @param {Number} tss - total sum of squares
* @returns {Number} coefficient of determination
*/
function rsq( mss, tss ) {
return mss / tss;
} // end FUNCTION rsq()


// EXPORTS //

module.exports = rsq;

0 comments on commit 481c4a0

Please sign in to comment.