@@ -5741,6 +5741,9 @@ return /******/ (function(modules) { // webpackBootstrap
57415741 this.r = y.r;
57425742 this.r2 = y.r2;
57435743 }
5744+ if(y.chi2){
5745+ this.chi2 = y.chi2;
5746+ }
57445747 }
57455748 else{
57465749 var n = x.length;
@@ -5769,9 +5772,8 @@ return /******/ (function(modules) { // webpackBootstrap
57695772 this.slope = numerator / (n * xSquared - xSum * xSum);
57705773 this.intercept = (1 / n) * ySum - this.slope * (1 / n) * xSum;
57715774 this.coefficients = [this.intercept, this.slope];
5772- if(options.computeCoefficient){
5773- this.r = this.rCoefficient(x,y);
5774- this.r2 = this.r*this.r;
5775+ if(options.computeQuality){
5776+ this.quality = this.modelQuality(x,y);
57755777 }
57765778 }
57775779
@@ -5783,9 +5785,8 @@ return /******/ (function(modules) { // webpackBootstrap
57835785 slope: this.slope,
57845786 intercept: this.intercept
57855787 }
5786- if (this.r) {
5787- out. r=this.r;
5788- out.r2 = this.r2;
5788+ if(this.quality){
5789+ out.quality = this.quality;
57895790 }
57905791
57915792 return out;
@@ -5882,20 +5883,21 @@ return /******/ (function(modules) { // webpackBootstrap
58825883 }
58835884
58845885 /**
5885- * Return the correlation coefficient of determination (r).
5886+ * Return the correlation coefficient of determination (r) and chi-square .
58865887 * @param x
58875888 * @param y
58885889 * @returns {number}
58895890 */
5890- rCoefficient (x, y) {
5891+ modelQuality (x, y) {
58915892 let n = x.length;
58925893 var y2 = new Array(n);
58935894 for (var i = 0; i < n; i++) {
58945895 y2[i]=this._predict(x[i]);
58955896 }
58965897 var xSum = 0;
58975898 var ySum = 0;
5898-
5899+ var chi2 = 0;
5900+ var rmsd = 0;
58995901 var xSquared = 0;
59005902 var ySquared = 0;
59015903 var xY = 0;
@@ -5906,10 +5908,16 @@ return /******/ (function(modules) { // webpackBootstrap
59065908 xSquared += y2[i] * y2[i];
59075909 ySquared += y[i] * y[i];
59085910 xY += y2[i] * y[i];
5911+ if(y[i]!=0)
5912+ chi2 += (y[i]-y2[i])*(y[i]-y2[i])/y[i];
5913+ rmsd = (y[i]-y2[i])*(y[i]-y2[i]);
59095914 }
59105915
5911- return (n*xY-xSum*ySum)/Math.sqrt((n*xSquared-xSum*xSum)*(n*ySquared-ySum*ySum));
5916+ var r = (n*xY-xSum*ySum)/Math.sqrt((n*xSquared-xSum*xSum)*(n*ySquared-ySum*ySum));
5917+
5918+ return {r:r, r2:r*r, chi2:chi2, rmsd:rmsd*rmsd/n};
59125919 }
5920+
59135921 }
59145922
59155923 module.exports = BaseRegression;
@@ -5953,9 +5961,8 @@ return /******/ (function(modules) { // webpackBootstrap
59535961 this.coefficients = outputs.coefficients;
59545962 this.powers = outputs.powers;
59555963 this.M = outputs.M;
5956- if(y.r){
5957- this.r = y.r;
5958- this.r2 = y.r2;
5964+ if(y.quality){
5965+ this.quality = y.quality;
59595966 }
59605967 } else {
59615968 var n = x.length;
@@ -5993,9 +6000,8 @@ return /******/ (function(modules) { // webpackBootstrap
59936000 this.coefficients = A.solve(B).to1DArray();
59946001 this.powers = powers;
59956002 this.M = M-1;
5996- if(opt.computeCoefficient){
5997- this.r = this.rCoefficient(x,y);
5998- this.r2 = this.r*this.r;
6003+ if(opt.computeQuality){
6004+ this.quality = this.modelQuality(x,y);
59996005 }
60006006 }
60016007 }
@@ -6015,9 +6021,8 @@ return /******/ (function(modules) { // webpackBootstrap
60156021 M: this.M
60166022 };
60176023
6018- if(this.r){
6019- out.r = this.r;
6020- out.r2=this.r2;
6024+ if(this.quality){
6025+ out.quality = this.quality;
60216026 }
60226027 return out;
60236028 }
@@ -6109,9 +6114,8 @@ return /******/ (function(modules) { // webpackBootstrap
61096114 if (x === true) { // reloading model
61106115 this.A = outputs.A;
61116116 this.M = outputs.M;
6112- if(y.r){
6113- this.r = y.r;
6114- this.r2 = y.r2;
6117+ if(y.quality){
6118+ this.quality = y.quality;
61156119 }
61166120 } else {
61176121 var n = x.length;
@@ -6122,9 +6126,8 @@ return /******/ (function(modules) { // webpackBootstrap
61226126 var linear = new PolynomialRegression(x, y, [M] ,{computeCoefficient:true});
61236127 this.A = linear.coefficients[0];
61246128 this.M = M;
6125- if(opt.computeCoefficient){
6126- this.r = this.rCoefficient(x,y);
6127- this.r2 = this.r*this.r;
6129+ if(opt.computeQuality){
6130+ this.quality = this.modelQuality(x,y);
61286131 }
61296132 }
61306133 }
@@ -6135,9 +6138,8 @@ return /******/ (function(modules) { // webpackBootstrap
61356138
61366139 toJSON() {
61376140 var out = {name: 'potentialRegression', A: this.A, M: this.M};
6138- if(this.r){
6139- out.r = this.r;
6140- out.r2 = this.r2;
6141+ if(this.quality){
6142+ out.quality = this.quality;
61416143 }
61426144 return out;
61436145 }
@@ -6147,7 +6149,11 @@ return /******/ (function(modules) { // webpackBootstrap
61476149 }
61486150
61496151 toLaTeX(precision){
6150- return "y = "+maybeToPrecision(this.A, precision)+"x^{"+this.M+"}";
6152+
6153+ if (this.M >= 0)
6154+ return "y = "+maybeToPrecision(this.A, precision)+"x^{"+this.M+"}";
6155+ else
6156+ return "y = \\frac{"+maybeToPrecision(this.A, precision)+"}{x^{"+(-this.M)+"}}";
61516157 }
61526158
61536159 static load(json) {
@@ -6194,9 +6200,8 @@ return /******/ (function(modules) { // webpackBootstrap
61946200 if (x === true) { // reloading model
61956201 this.A = outputs.A;
61966202 this.C = outputs.C;
6197- if(y.r){
6198- this.r = y.r;
6199- this.r2 = y.r2;
6203+ if(y.quality){
6204+ this.quality = y.quality;
62006205 }
62016206 } else {
62026207 var n = x.length;
@@ -6211,9 +6216,8 @@ return /******/ (function(modules) { // webpackBootstrap
62116216 var linear = new SimpleLinearRegression(x, yl, {computeCoefficient:false});
62126217 this.A = linear.slope;
62136218 this.C = Math.exp(linear.intercept);
6214- if(opt.computeCoefficient){
6215- this.r = this.rCoefficient(x,y);
6216- this.r2 = this.r*this.r;
6219+ if(opt.computeQuality){
6220+ this.quality = this.modelQuality(x,y);
62176221 }
62186222 }
62196223 }
@@ -6224,9 +6228,8 @@ return /******/ (function(modules) { // webpackBootstrap
62246228
62256229 toJSON() {
62266230 var out = {name: 'expRegression', A: this.A, C: this.C};
6227- if(this.r){
6228- out.r = this.r;
6229- out.r2=this.r2;
6231+ if(this.quality){
6232+ out.quality = this.quality;
62306233 }
62316234 return out;
62326235 }
@@ -6236,7 +6239,11 @@ return /******/ (function(modules) { // webpackBootstrap
62366239 }
62376240
62386241 toLaTeX(precision){
6239- return "y = "+maybeToPrecision(this.C, precision)+"e^{"+maybeToPrecision(this.A, precision)+"x}";
6242+ if(this.A>=0)
6243+ return "y = "+maybeToPrecision(this.C, precision)+"e^{"+maybeToPrecision(this.A, precision)+"x}";
6244+ else
6245+ return "y = \\frac{"+maybeToPrecision(this.C, precision)+"}{e^{"+maybeToPrecision(-this.A, precision)+"x}}";
6246+
62406247 }
62416248
62426249 static load(json) {
@@ -6283,6 +6290,9 @@ return /******/ (function(modules) { // webpackBootstrap
62836290 this.r = y.r;
62846291 this.r2 = y.r2;
62856292 }
6293+ if(y.chi2){
6294+ this.chi2 = y.chi2;
6295+ }
62866296 } else {
62876297 var n = x.length;
62886298 if (n !== y.length) {
@@ -6297,9 +6307,8 @@ return /******/ (function(modules) { // webpackBootstrap
62976307 var linear = new SimpleLinearRegression(xl, yl, {computeCoefficient:false});
62986308 this.A = Math.exp(linear.intercept);
62996309 this.B = linear.slope;
6300- if(opt.computeCoefficient){
6301- this.r = this.rCoefficient(x,y);
6302- this.r2 = this.r*this.r;
6310+ if(opt.computeQuality){
6311+ this.quality = this.modelQuality(x,y);
63036312 }
63046313 }
63056314 }
@@ -6310,9 +6319,8 @@ return /******/ (function(modules) { // webpackBootstrap
63106319
63116320 toJSON() {
63126321 var out = {name: 'powerRegression', A: this.A, B: this.B};
6313- if(this.r){
6314- out.r = this.r;
6315- out.r2=this.r2;
6322+ if(this.quality){
6323+ out.quality = this.quality;
63166324 }
63176325 return out;
63186326 }
@@ -6321,8 +6329,11 @@ return /******/ (function(modules) { // webpackBootstrap
63216329 return "y = "+maybeToPrecision(this.A, precision)+"*x^"+maybeToPrecision(this.B, precision);
63226330 }
63236331
6324- toLaTeX(precision){
6325- return "y = "+maybeToPrecision(this.A, precision)+"x^{"+maybeToPrecision(this.B, precision)+"}";
6332+ toLaTeX(precision) {
6333+ if (this.B >= 0)
6334+ return "y = " + maybeToPrecision(this.A, precision) + "x^{" + maybeToPrecision(this.B, precision) + "}";
6335+ else
6336+ return "y = \\frac{" + maybeToPrecision(this.A, precision) + "}{x^{" + maybeToPrecision(-this.B, precision) + "}}";
63266337 }
63276338
63286339 static load(json) {
@@ -6364,9 +6375,9 @@ return /******/ (function(modules) { // webpackBootstrap
63646375 this.kernelType = outputs.kernelType;
63656376 this.kernelOptions = outputs.kernelOptions;
63666377 this.kernel = new Kernel(outputs.kernelType, outputs.kernelOptions);
6367- if(outputs.r){
6368- this.r = outputs.r;
6369- this.r2 = outputs.r2 ;
6378+
6379+ if( outputs.quality){
6380+ this.quality = outputs.quality ;
63706381 }
63716382 } else {
63726383 options = Object.assign({}, defaultOptions, options);
@@ -6382,9 +6393,8 @@ return /******/ (function(modules) { // webpackBootstrap
63826393 this.kernelOptions = options.kernelOptions;
63836394 this.kernel = kernelFunction;
63846395
6385- if(options.computeCoefficient){
6386- this.r = this.rCoefficient(inputs, outputs);
6387- this.r2 = this.r*this.r;
6396+ if(options.computeQuality){
6397+ this.quality=this.modelQuality(inputs,outputs);
63886398 }
63896399 }
63906400 }
@@ -6394,13 +6404,17 @@ return /******/ (function(modules) { // webpackBootstrap
63946404 }
63956405
63966406 toJSON() {
6397- return {
6407+ var out = {
63986408 name: 'kernelRidgeRegression',
63996409 alpha: this.alpha,
64006410 inputs: this.inputs,
64016411 kernelType: this.kernelType,
64026412 kernelOptions: this.kernelOptions
64036413 };
6414+ if(this.quality){
6415+ out.quality = this.quality;
6416+ }
6417+ return out;
64046418 }
64056419
64066420 static load(json) {
@@ -6588,6 +6602,9 @@ return /******/ (function(modules) { // webpackBootstrap
65886602 this.r = outputs.r;
65896603 this.r2 = outputs.r2;
65906604 }
6605+ if(outputs.chi2){
6606+ this.chi2 = outputs.chi2;
6607+ }
65916608 } else {
65926609 options = Object.assign({}, defaultOptions, options);
65936610 this.order = options.order;
@@ -6596,9 +6613,9 @@ return /******/ (function(modules) { // webpackBootstrap
65966613 this.y = outputs;
65976614
65986615 this.train(this.X,this.y,options);
6599- if(options.computeCoefficient){
6600- this.r = this.rCoefficient(inputs, outputs);
6601- this.r2 = this.r*this.r ;
6616+
6617+ if(options.computeQuality){
6618+ this.quality = this.modelQuality(inputs,outputs) ;
66026619 }
66036620 }
66046621 }
@@ -6695,9 +6712,7 @@ return /******/ (function(modules) { // webpackBootstrap
66956712
66966713 for(var i = 0; i <= this.order; i++) {
66976714 for(var j = 0; j <= this.order - i; j++) {
6698- var value = Math.pow(x1, i)*(Math.pow(x2, j));
6699- value*=this.coefficients[column][0];
6700- y+=value;
6715+ y+= Math.pow(x1, i)*(Math.pow(x2, j))*this.coefficients[column][0];
67016716 column++;
67026717 }
67036718 }
@@ -6706,11 +6721,15 @@ return /******/ (function(modules) { // webpackBootstrap
67066721 }
67076722
67086723 toJSON() {
6709- return {
6724+ var out = {
67106725 name: "polyfit2D",
67116726 order: this.order,
67126727 coefficients: this.coefficients
67136728 };
6729+ if(this.quality){
6730+ out.quality = this.quality;
6731+ }
6732+ return out;
67146733 };
67156734
67166735 static load(json) {
0 commit comments