@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 2.82 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import r from"./../../assert/is-positive-integer.js";import e from"./../../assert/is-square-matrix.js";import t from"./../../assert/is-vector-like.js";import a from"./../../array/float64.js";import n from"./../../math/base/special/sqrt.js";import o from"./../../ndarray/ctor.js";import i from"./../../ndarray/base/ctor.js";import s from"./../../ndarray/base/numel.js";var m=r.isPrimitive,u=e,l=t,f=a,d=n,c=o,h=i,p=s,g={codegen:!1};function w(r,e){return(e?h:c)("float64",2,g)(new f(r*r),[r,r],[r,1],0,"row-major")}function v(r,e){var t,a=r.shape[0];for(t=0;t<a;t++)r.set(t,t,e);return r}function y(r){return h("float64",1,g)(new f(r),[r],[1],0,"row-major")}function j(r,e){var t,a,n,o,i,s,c,h;if(h=0,m(r))a=w(t=r,!1);else{if(!u(r))throw new TypeError("invalid argument. First argument must either specify the order of the correlation matrix or be a square 2-dimensional ndarray for storing the correlation matrix. Value: `"+r+"`.");t=r.shape[0],a=r}if(a=v(a,1),c=new f(t),n=new f(t),o=new f(t),s=w(t,!0),arguments.length>1){if(!l(e))throw new TypeError("invalid argument. Second argument must be a 1-dimensional ndarray. Value: `"+e+"`.");if(p(e.shape)!==t)throw new Error("invalid argument. The number of elements (means) in the second argument must match correlation matrix dimensions. Expected: "+t+". Actual: "+p(e.shape)+".");return i=e,j}return i=y(t),g;function g(r){var e,m,u,f,p,g,w,v,y,j,x,E;if(0===arguments.length)return 0===h?null:a;if(!l(r))throw new TypeError("invalid argument. Must provide a 1-dimensional ndarray. Value: `"+r+"`.");if(r.shape[0]!==t)throw new Error("invalid argument. Vector length must match correlation matrix dimensions. Expected: "+t+". Actual: "+r.shape[0]+".");if(j=(y=h)/(h+=1),e=y||1,1===h)for(x=0;x<t;x++)for(w=r.get(x),v=i.get(x),v+=(g=w-v)/h,i.set(x,v),c[x]=g,n[x]+=g*(w-v),o[x]=d(n[x]/e),m=j*c[x],E=0;E<x;E++)u=s.get(x,E)+m*c[E],s.set(x,E,u),s.set(E,x,u);else for(x=0;x<t;x++)for(w=r.get(x),v=i.get(x),v+=(g=w-v)/h,i.set(x,v),c[x]=g,n[x]+=g*(w-v),o[x]=d(n[x]/e),m=j*c[x],p=o[x],E=0;E<x;E++)u=s.get(x,E)+m*c[E],s.set(x,E,u),s.set(E,x,u),f=u/e/(p*o[E]),a.set(x,E,f),a.set(E,x,f);return a}function j(r){var e,m,u,f,p,g;if(0===arguments.length)return 0===h?null:a;if(!l(r))throw new TypeError("invalid argument. Must provide a 1-dimensional ndarray. Value: `"+r+"`.");if(r.shape[0]!==t)throw new Error("invalid argument. Vector length must match correlation matrix dimensions. Expected: "+t+". Actual: "+r.shape[0]+".");for(h+=1,p=0;p<t;p++)for(f=r.get(p)-i.get(p),c[p]=f,n[p]+=f*f,o[p]=d(n[p]/h),u=o[p],g=0;g<p;g++)m=s.get(p,g)+f*c[g],s.set(p,g,m),s.set(g,p,m),e=m/h/(u*o[g]),a.set(p,g,e),a.set(g,p,e);return a}}var x=j;export default x;
//# sourceMappingURL=pcorrmat.js.map