UNPKG

uapca

Version:

Uncertainty-aware principal component analysis.

3 lines (2 loc) 3.05 kB
import{randomNormal as t}from"d3-random";import{Matrix as e,EigenvalueDecomposition as s,MatrixSubView as r}from"ml-matrix";export{Matrix}from"ml-matrix";import{range as n}from"d3-array";class o{constructor(t,s){this.meanVec=t instanceof e?t:e.rowVector(t),this.covMat=s instanceof e?s:new e(s)}static standard(t){return new o(e.zeros(1,t),e.eye(t,t))}mean(){return this.meanVec}covariance(){return this.covMat}affineTransformation(t,e){const s=this.meanVec.mmul(t).add(e),r=t.transpose().mmul(this.covMat).mmul(t);return new o(s,r)}project(t){return this.affineTransformation(t,e.zeros(1,t.columns))}}class a{constructor(){this.random=t()}}function i(t){const r=new s(t.covariance(),{assumeSymmetric:!0}),n=r.eigenvectorMatrix.transpose();return e.diag(r.realEigenvalues.map(t=>Math.sqrt(t))).mmul(n)}class m{constructor(t){this.mean=t.mean(),this.A=i(t),this.gen=new a}sampleN(t){return e.random(t,this.dims(),this.gen).mmul(this.A).addRowVector(this.mean).to2DArray()}dims(){return this.mean.columns}}function c(t){return t.transpose().mmul(t)}function h(t){const s=t[0].rows,r=t[0].columns,n=t.length;return t.reduce((t,e)=>t.add(e),e.zeros(s,r)).div(n)}class u{constructor(t,e,s){this.lengths=t,this.vectors=e,this.mean=s}static fit(t,r=1){const n=h(t.map(t=>t.mean())),o=c(n),a=h(t.map(t=>c(t.mean()).add(e.mul(t.covariance(),r*r)).sub(o))),i=new s(a,{assumeSymmetric:!0}),m=i.realEigenvalues,l=i.eigenvectorMatrix.transpose().to2DArray(),p=m.map((t,e)=>[t,l[e]]).sort((t,e)=>e[0]-t[0]);return new u(p.map(t=>t[0]),new e(p.map(t=>t[1])),n)}aligned(){const t=this.vectors;for(let e=0;e<t.rows;++e)t.get(e,e)<0&&t.setRow(e,t.getRowVector(e).mul(-1));return new u(this.lengths,t,this.mean)}eigenvalues(t){return t?this.lengths.slice(0,t):this.lengths}projectionMatrix(t){return t?new r(this.vectors,0,t-1,0,this.vectors.columns-1):this.vectors}transform(t,s){const r=this.projectionMatrix(s);return t.map(t=>t.affineTransformation(e.eye(this.mean.columns,this.mean.columns),e.mul(this.mean,-1))).map(t=>t.project(r.transpose()))}}class l{constructor(t){this.samples=t}points(){return this.samples.to2DArray()}pointsFlipped(){return this.samples.mul(-1).to2DArray()}}class p{constructor(t,s,r,o,a){this.N=s||100,this.min=r||0,this.max=o||1e3,this.components=a||2;const i=Math.pow(this.min,2),m=(Math.pow(this.max,2)-i)/this.N,c=n(this.N).map(t=>Math.sqrt(i+t*m)).map(e=>u.fit(t,e).aligned()),h=t[0].mean().columns;this.traceSamples=c.map(t=>{const s=t.projectionMatrix(this.components).transpose();return e.eye(h,h).mmul(s)})}getTrace(t){const s=this.traceSamples[0],r=e.zeros(this.N,this.components);r.setRow(0,s.getRowVector(t));for(let s=1;s<this.traceSamples.length;++s){const n=this.traceSamples[s],o=n.getRowVector(t),a=n.getRowVector(t).mul(-1),i=e.sub(o,r.getRowVector(s-1)),m=e.sub(a,r.getRowVector(s-1));i.dot(i)<m.dot(m)?r.setRow(s,o):r.setRow(s,a)}return new l(r)}}export{p as FactorTraces,o as MultivariateNormal,m as Sampler,l as Trace,u as UaPCA,h as arithmeticMean,i as transformationMatrix}; //# sourceMappingURL=uapca.node.es.js.map