@veyforma/charts 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ /*! @veyforma/charts v1.0.0 · MIT License · Copyright (c) 2026 Veyforma · https://github.com/veyforma/veyforma-charts */
2
+ (function(K){"use strict";var mt="1.0.0",Lt="http://www.w3.org/2000/svg",Tt=0,nt={hpl:"#111827",rtpl:"#c2410c",unexplained:"#6b7280",contribution:"#a63d16",offset:"#2f6fed",standalone:"#111827",zones:{green:"#15803d",amber:"#b45309",red:"#b91c1c"},outside:"#d1d5db",grid:"#ececec",axis:"#9ca3af",text:"#111827",mutedText:"#4b5563",axisText:"#4b5563",cardBackground:"#f9fafb",background:"transparent",tooltipBackground:"#111827",tooltipText:"#ffffff",focus:"#c2410c",accent:"#c2410c",fontFamily:"inherit",fontSize:12},pt={veyforma:{},mono:{hpl:"#111111",rtpl:"#6b6b6b",unexplained:"#8a8a8a",contribution:"#111111",offset:"#8a8a8a",zones:{green:"#111111",amber:"#4b4b4b",red:"#111111"},accent:"#111111",focus:"#111111"},"colour-safe":{hpl:"#1f2937",rtpl:"#d55e00",unexplained:"#6b7280",contribution:"#cc3311",offset:"#0072b2",zones:{green:"#007a5e",amber:"#a35c00",red:"#b2182b"},accent:"#0072b2"},ocean:{hpl:"#0c3b5e",rtpl:"#0e7490",contribution:"#0c3b5e",offset:"#c2410c",cardBackground:"#eef5f8",accent:"#0e7490"},forest:{hpl:"#22311a",rtpl:"#4d6b31",contribution:"#22311a",offset:"#2f6fed",cardBackground:"#f2f4ee",accent:"#4d6b31"},corporate:{hpl:"#1e293b",rtpl:"#1d4ed8",contribution:"#1e293b",offset:"#c2410c",cardBackground:"#f1f4f8",accent:"#1d4ed8"},midnight:{background:"#111418",hpl:"#f3f4f6",rtpl:"#f08a5d",unexplained:"#8b93a1",contribution:"#f08a5d",offset:"#93c5fd",standalone:"#f3f4f6",zones:{green:"#4ade80",amber:"#fbbf24",red:"#fb7185"},outside:"#374151",grid:"#252a31",axis:"#4b5563",text:"#f3f4f6",mutedText:"#a3abb8",axisText:"#a3abb8",cardBackground:"#1b2027",tooltipBackground:"#f3f4f6",tooltipText:"#111418",focus:"#93c5fd",accent:"#f08a5d"}},dt={spearmanGreen:.8,spearmanRed:.7,ksGreen:.09,ksRed:.12},vt={data:null,view:"factors",window:250,fix:null,thresholds:null,top:15,pathSteps:5,theme:"veyforma",locale:void 0,animate:!0,summary:!0,legend:!0,labels:null,ariaLabel:void 0,caption:!0,annotations:null,onAnnotationsChange:null,onItemClick:null,exportDays:2e4,toolbar:!0},ut={factors:"Unexplained P&L by risk factor",daily:"HPL and RTPL by day",distribution:"Distribution of HPL and RTPL",hpl:"HPL (front office)",rtpl:"RTPL (risk model)",unexplained:"Unexplained (HPL \u2212 RTPL)",adds:"Adds to unexplained volatility",offsets:"Offsets other gaps",standalone:"Gap on its own",test:"PLA test",unexplainedCard:"Unexplained P&L",largest:"Largest driver",path:"Path to green",green:"Green",amber:"Amber",red:"Red",na:"n/a",other:"Other {n} factors",unattributed:"Unattributed",notModelled:"not in the risk model",ifFixed:"If fixed",outside:"Outside the test window"};function Y(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];if(a)for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(t[r]=a[r])}return t}var gt={};function Q(t){gt[t]||(gt[t]=1,K.console&&console.warn&&console.warn("VeyPlaAttribution: "+t))}function j(t){throw new Error("VeyPlaAttribution: "+t)}function at(t,e,a){return Math.min(a,Math.max(e,t))}function xt(t,e){return String(t).replace(/\{(\w+)\}/g,function(a,r){return e[r]!=null?e[r]:a})}function Gt(t){return t=String(t),/^[A-Z0-9]{2}/.test(t)?t:t.charAt(0).toLowerCase()+t.slice(1)}function Mt(t){return typeof t=="number"?t:t==null||t===""?NaN:+t}function G(t,e,a){var r=document.createElementNS(Lt,t);if(e)for(var n in e)e[n]!=null&&r.setAttribute(n,e[n]);return a&&a.appendChild(r),r}function P(t,e,a){var r=document.createElement(t);return e&&(r.className=e),a&&a.appendChild(r),r}var tt=null;function B(t,e,a,r){if(!tt){var n=document.createElement("canvas");tt=n.getContext&&n.getContext("2d")}return tt?(tt.font=(a||400)+" "+e+"px "+r,tt.measureText(String(t)).width):String(t).length*e*.6}function rt(t,e,a,r,n){if(t=String(t),B(t,a,r,n)<=e)return t;for(;t.length>2&&B(t+"\u2026",a,r,n)>e;)t=t.slice(0,-1);return t.replace(/\s+$/,"")+"\u2026"}function it(t){var e=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(String(t||"").trim());if(!e)return null;var a=e[1].length===3?e[1].replace(/./g,"$&$&"):e[1],r=parseInt(a,16);return[r>>16&255,r>>8&255,r&255]}function ft(t){var e=t.map(function(a){return a/=255,a<=.03928?a/12.92:Math.pow((a+.055)/1.055,2.4)});return .2126*e[0]+.7152*e[1]+.0722*e[2]}function bt(t,e){var a=it(t),r=it(e);if(!a||!r)return 21;var n=ft(a),i=ft(r);return(Math.max(n,i)+.05)/(Math.min(n,i)+.05)}function q(t,e,a){var r=it(t),n=it(e);if(!r||!n||bt(t,e)>=a)return t;for(var i=ft(n)>.4,s=0,l=t;s<=1&&bt(l,e)<a;)s+=.05,l="#"+r.map(function(p){var f=i?p*(1-s):p+(255-p)*s;return("0"+Math.round(f).toString(16)).slice(-2)}).join("");return l}function ot(t,e){var a=t.decimals!=null?t.decimals:0,r=t.ratioDecimals!=null?t.ratioDecimals:1,n=t.prefix||"",i=t.suffix||"",s={},l=t.negatives==="brackets";function p(u){return s[u]||(s[u]=new Intl.NumberFormat(e||void 0,{minimumFractionDigits:u,maximumFractionDigits:u})),s[u]}function f(u,v){if(u==null||!isFinite(u))return"\u2013";v==null&&(v=a);var b=Number(u.toFixed(v)),k=n+p(v).format(Math.abs(b))+i;return b>=0?k:l?"("+k+")":"\u2212"+k}function c(u,v){var b=f(u,v);return u!=null&&Number(u.toFixed(v??a))>0?"+"+b:b}function o(u,v){return u==null||!isFinite(u)?"\u2013":(u<0?"\u2212":"")+p(v??r).format(Math.abs(u)*100)+"%"}function m(u,v){if(u==null||!isFinite(u))return"\u2013";var b=v??Math.max(r,1),k=Number((u*100).toFixed(b));return(k>0?"+":k<0?"\u2212":"")+p(b).format(Math.abs(k))+" pp"}function h(u,v){return u==null||!isFinite(u)?"\u2013":p(v??1).format(u*100)+"%"}return{value:f,signed:c,ratio:o,pp:m,percent:h,nf:p}}function yt(t,e){var a=Mt(t);return isFinite(a)||j(e+" must be a number."),a}function wt(t){var e=t.length,a=new Array(e),r=new Array(e),n,i;for(n=0;n<e;n++)a[n]=n;for(a.sort(function(p,f){return t[p]-t[f]}),n=0;n<e;n=i){for(i=n;i<e&&t[a[i]]===t[a[n]];)i++;for(var s=(n+i+1)/2,l=n;l<i;l++)r[a[l]]=s}return r}function zt(t,e){var a=t.length,r=0,n=0,i;for(i=0;i<a;i++)r+=t[i],n+=e[i];r/=a,n/=a;var s=0,l=0,p=0;for(i=0;i<a;i++){var f=t[i]-r,c=e[i]-n;s+=f*c,l+=f*f,p+=c*c}return l>0&&p>0?s/Math.sqrt(l*p):null}function Et(t,e){return zt(wt(t),wt(e))}function Pt(t,e){for(var a=t.slice().sort(function(m,h){return m-h}),r=e.slice().sort(function(m,h){return m-h}),n=a.length,i=r.length,s=0,l=0,p=0,f=a.length?a[0]:0;s<n&&l<i;){for(var c=Math.min(a[s],r[l]);s<n&&a[s]===c;)s++;for(;l<i&&r[l]===c;)l++;var o=Math.abs(s/n-l/i);o>p+1e-15&&(p=o,f=c)}return{d:p,at:f}}function Rt(t,e,a){return t==null?"red":(t=Math.round(t*1e12)/1e12,e=Math.round(e*1e12)/1e12,t>a.spearmanGreen&&e<a.ksGreen?"green":t<a.spearmanRed||e>a.ksRed?"red":"amber")}function kt(t,e){return Math.max(0,e.spearmanGreen-(t.spearman==null?-1:t.spearman))/.1+Math.max(0,t.ks-e.ksGreen)/.03}function st(t,e,a){var r=Et(t,e),n=Pt(t,e);return{spearman:r,ks:n.d,ksAt:n.at,zone:Rt(r,n.d,a)}}function ht(t){var e=t.length,a=0,r=0,n;for(n=0;n<e;n++)a+=t[n];for(a/=e,n=0;n<e;n++)r+=(t[n]-a)*(t[n]-a);return Math.sqrt(r/e)}function St(t){for(var e=0,a=0;a<t.length;a++)e+=t[a];return t.length?e/t.length:0}function Ft(t){(!t||typeof t!="object")&&j("data is required.");var e=t.dates;(!Array.isArray(e)||e.length<2)&&j("data.dates must be an array of at least two days.");var a=e.length,r=function(o,m,h){return o==null&&h?null:((!Array.isArray(o)||o.length!==a)&&j(m+" must be an array with one value per date ("+a+")."),o.map(function(u,v){return yt(u,m+"["+v+"]")}))};(!Array.isArray(t.factors)||!t.factors.length)&&j("data.factors must be a non-empty array of risk factors.");var n={},i=t.factors.map(function(o,m){(!o||typeof o!="object")&&j("data.factors["+m+"] is not an object."),o.key==null&&o.label==null&&j("data.factors["+m+"] needs a key or a label.");var h=String(o.key!=null?o.key:o.label),u='factor "'+h+'"';n[h]&&j(u+" is used twice."),n[h]=1;var v=r(o.hpl,u+": hpl"),b=r(o.rtpl,u+": rtpl",!0);return{key:h,label:o.label!=null?String(o.label):h,group:o.group!=null?String(o.group):"Other",modelled:b!=null,hpl:v,rtpl:b||v.map(function(){return 0}),index:m}}),s=r(t.hplTotal,"data.hplTotal",!0),l=r(t.rtplTotal,"data.rtplTotal",!0);if(s||l){var p=new Array(a),f=new Array(a),c;for(c=0;c<a;c++)p[c]=0,f[c]=0,i.forEach(function(o){p[c]+=o.hpl[c],f[c]+=o.rtpl[c]});i.push({key:"__unattributed",label:t.unattributedLabel||ut.unattributed,group:"Unattributed",modelled:!0,unattributed:!0,hpl:p.map(function(o,m){return s?s[m]-o:0}),rtpl:f.map(function(o,m){return l?l[m]-o:0}),index:i.length})}return{dates:e.map(String),factors:i,n:a}}function et(t,e){e=e||{};var a=Ft(t),r=a.n,n=a.factors,i=Y({},dt,e.thresholds||{}),s=e.window!=null?Math.floor(yt(e.window,"window")):250;s>=2||j("window must be at least 2 days.");var l=s>r;l&&(s=r);var p=r-s,f={};(e.fix||[]).forEach(function(x){n.some(function(g){return g.key===String(x)})||Q('fix: there is no factor "'+x+'", so it changes nothing.'),f[String(x)]=1}),n.forEach(function(x){x.fixed=!!f[x.key],x.rt=x.fixed?x.hpl:x.rtpl,x.gap=x.hpl.map(function(g,C){return g-x.rt[C]})});var c=new Array(r),o=new Array(r),m=new Array(r),h,u;for(h=0;h<r;h++){var v=0,b=0;for(u=0;u<n.length;u++)v+=n[u].hpl[h],b+=n[u].rt[h];c[h]=v,o[h]=b,m[h]=v-b}var k=c.slice(p),L=o.slice(p),d=m.slice(p),y={dates:a.dates,hpl:c,rtpl:o,unexplained:m,start:p,window:s,shortWindow:l,thresholds:i,fixed:Object.keys(f)};y.test=st(k,L,i);var _=ht(d),M=ht(k),$=St(d);y.sdUnexplained=_,y.sdHpl=M,y.meanUnexplained=$,y.volRatio=M>0?_/M:null,y.meanRatio=M>0?$/M:null,y.varRatio=M>0?_*_/(M*M):null,n.forEach(function(x){var g=x.gap.slice(p),C=St(g),w=0;for(h=0;h<s;h++)w+=(g[h]-C)*(d[h]-$);for(w/=s,x.meanGap=C,x.sdGap=ht(g),x.contribution=_>0?w/_:0,x.share=_>0?x.contribution/_:0,x.hplSum=0,x.rtplSum=0,h=p;h<r;h++)x.hplSum+=x.hpl[h],x.rtplSum+=x.rt[h];if(x.hasGap=x.sdGap>0||C!==0,x.hasGap){var T=L.map(function(N,U){return N+g[U]});x.ifFixed=st(k,T,i)}else x.ifFixed=y.test});var O=n.slice().sort(function(x,g){return Math.abs(g.contribution)-Math.abs(x.contribution)||x.index-g.index});O.forEach(function(x,g){x.rank=g+1}),y.factors=n,y.order=O;for(var D=e.pathSteps!=null?Math.max(0,Math.floor(e.pathSteps)):5,R=[],H=L.slice(),A=y.test,W={};A.zone!=="green"&&R.length<D;){var E=null;if(n.forEach(function(x){if(!(W[x.key]||x.fixed||!x.hasGap)){var g=x.gap.slice(p),C=H.map(function(N,U){return N+g[U]}),w=st(k,C,i),T=kt(w,i);(!E||T<E.d-1e-12||Math.abs(T-E.d)<=1e-12&&(w.spearman||-1)>(E.t.spearman||-1))&&(E={f:x,t:w,d:T,R:C})}}),!E||E.d>=kt(A,i)-1e-12)break;W[E.f.key]=1,R.push({key:E.f.key,label:E.f.label,test:E.t}),H=E.R,A=E.t}return y.path=R,y.pathReachesGreen=y.test.zone==="green"||R.length>0&&R[R.length-1].test.zone==="green",y.distribution={hpl:k.slice().sort(function(x,g){return x-g}),rtpl:L.slice().sort(function(x,g){return x-g})},y}function At(){if(!document.getElementById("vpa-styles")){var t=".vpa{position:relative;color:var(--vpa-text);background:var(--vpa-bg);font-family:var(--vpa-font);line-height:1.4}.vpa.vpa-filled{padding:14px 14px 8px;border-radius:10px}.vpa-summary{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:8px;margin:0 0 10px}.vpa-stat{background:var(--vpa-card);border-radius:8px;padding:9px 12px;min-width:0}.vpa-stat .k{font-size:11px;letter-spacing:.04em;text-transform:uppercase;color:var(--vpa-card-muted);font-weight:600;overflow-wrap:anywhere;hyphens:auto}.vpa-stat .v{font-size:19px;font-weight:700;font-variant-numeric:tabular-nums;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.vpa-stat .n{font-size:12px;color:var(--vpa-card-muted)}.vpa-stat .v.vpa-zone{font-weight:800}.vpa-stat .v.vpa-wrap{white-space:normal;overflow-wrap:anywhere;line-height:1.2;font-size:17px}.vpa-scope{font-size:12px;color:var(--vpa-muted);margin:0 0 4px}.vpa-legend{display:flex;flex-wrap:wrap;gap:4px 14px;font-size:12px;margin:0 0 6px;color:var(--vpa-text)}.vpa-legend span{display:inline-flex;align-items:center;gap:6px}.vpa-legend i{display:inline-block;width:12px;height:12px;border-radius:3px}.vpa-legend i.vpa-tick{width:16px;height:3px;border-radius:1px}.vpa-legend i.vpa-ring{background:transparent!important;border:2px solid;border-radius:2px;width:9px;height:9px;transform:rotate(45deg)}.vpa-plot{display:block;width:100%;height:auto;overflow:visible;outline:none}.vpa-plot:focus-visible{outline:2px solid var(--vpa-focus);outline-offset:4px;border-radius:4px}.vpa-animate .vpa-grow{animation:vpa-fade .45s ease both}@keyframes vpa-fade{from{opacity:0}to{opacity:1}}@media (prefers-reduced-motion:reduce){.vpa-animate .vpa-grow{animation:none}}.vpa-tip{position:absolute;left:0;top:0;pointer-events:none;background:var(--vpa-tip-bg);color:var(--vpa-tip-text);font-size:12px;line-height:1.45;padding:8px 10px;border-radius:8px;opacity:0;transform:translateY(4px);transition:opacity .12s,transform .12s;white-space:nowrap;z-index:5;font-variant-numeric:tabular-nums}.vpa-tip.vpa-on{opacity:1;transform:none}.vpa-tip b{font-weight:600}.vpa-table{border-collapse:collapse;font-size:12px;font-variant-numeric:tabular-nums;margin:0 0 14px;min-width:100%}.vpa-table caption{text-align:left;font-weight:700;padding:0 0 6px}.vpa-table th,.vpa-table td{padding:5px 8px;border-bottom:1px solid rgba(127,127,127,.25);text-align:right;white-space:nowrap}.vpa-table th:first-child,.vpa-table td:first-child{text-align:left}.vpa-table tr.vpa-breach td{color:var(--vpa-hedge);font-weight:600}@media (max-width:420px){.vpa-summary{grid-template-columns:repeat(auto-fit,minmax(118px,1fr))}.vpa-stat{padding:8px 10px}.vpa-stat .v{font-size:17px}}",e=P("style");e.id="vpa-styles",e.textContent=t,(document.head||document.documentElement).appendChild(e)}}function lt(t){var e;(t==null||t==="")&&(t="veyforma"),typeof t=="string"?(pt.hasOwnProperty(t)||(Q('Unknown theme "'+t+'". The "veyforma" theme is used instead.'),t="veyforma"),e=Y({},nt,pt[t])):typeof t=="object"?(e=Y(t.extends?lt(t.extends):Y({},nt),t),delete e.extends):(Q("The theme option must be a theme name or an object."),e=Y({},nt));var a=/^#/.test(e.background)?e.background:"#ffffff",r=/^#/.test(e.cardBackground)?e.cardBackground:a;return["axisText","mutedText"].forEach(function(n){e[n]=q(e[n],a,4.5)}),e.cardMuted=q(e.mutedText,r,4.5),e.offsetText=q(q(e.offset,a,4.5),r,4.5),e.zoneText={},["green","amber","red"].forEach(function(n){e.zoneText[n]=q(q(e.zones[n],a,4.5),r,4.5)}),e.accentText=q(e.accent,a,4.5),e}function S(t,e){var a=typeof t=="string"?document.querySelector(t):t;(!a||a.nodeType!==1)&&j("container not found ("+t+")."),(!e||!e.data)&&j("options.data is required."),At(),this.id=++Tt,this.container=a,this.options=Y({},vt,e),this.element=P("div","vpa"),this.element.setAttribute("role","figure"),this.summaryEl=P("div","vpa-summary",this.element),this.scopeEl=P("p","vpa-scope",this.element),this.legendEl=P("div","vpa-legend",this.element),this.plotHost=P("div","vpa-plot-host",this.element),this.tipEl=P("div","vpa-tip",this.element),this.tipEl.setAttribute("role","tooltip"),a.appendChild(this.element),this._animateNext=!!this.options.animate,this._lastWidth=0,this._active=-1;var r=this;K.ResizeObserver?(this._observer=new K.ResizeObserver(function(){var n=Math.round(r.element.clientWidth);n&&Math.abs(n-r._lastWidth)>1&&r.redraw()}),this._observer.observe(this.element)):(this._onResize=function(){r.redraw()},K.addEventListener("resize",this._onResize)),this._onDocTap=function(n){r.element.contains(n.target)||r._hideTip()},document.addEventListener("pointerdown",this._onDocTap),this.redraw(),this.options.toolbar!==!1&&K.VeyformaToolkit&&(this.toolkit=K.VeyformaToolkit.attach(this,this.options.toolbar))}S.prototype.update=function(t){return t?(t.data&&t.data!==this.options.data&&this.options.animate&&(this._animateNext=!0),t.view&&t.view!==this.options.view&&this.options.animate&&(this._animateNext=!0),Y(this.options,t),this.redraw()):this},S.prototype.setData=function(t){return this._res=null,this.update({data:t})},S.prototype.setView=function(t){return this.update({view:t})},S.prototype.setTheme=function(t){return this.update({theme:t})},S.prototype.getTheme=function(){return lt(this.options.theme)},S.prototype._opts=function(){return{window:this.options.window,fix:this.options.fix,thresholds:this.options.thresholds}},S.prototype.getResult=function(){return et(this.options.data,this._opts())},S.prototype.getFactor=function(t){return this.getResult().factors.filter(function(e){return e.key===String(t)})[0]||null},S.prototype.setFix=function(t){return this.update({fix:t})},S.prototype.setWindow=function(t){return this.update({window:t})},S.prototype._labels=function(){return Y({},ut,this.options.labels)},S.prototype.destroy=function(){this.toolkit&&this.toolkit.destroy&&this.toolkit.destroy(),this._observer&&this._observer.disconnect(),this._onResize&&K.removeEventListener("resize",this._onResize),document.removeEventListener("pointerdown",this._onDocTap),this.element.parentNode&&this.element.parentNode.removeChild(this.element)},S.prototype._hideTip=function(){this.tipEl.classList.remove("vpa-on"),this._hlEl&&this._hlEl.setAttribute("opacity",0)},S.prototype._placeTip=function(t,e,a){var r=this.tipEl;r.textContent="",t.forEach(function(c,o){o&&r.appendChild(document.createElement("br"));var m=o===0?document.createElement("b"):document.createElement("span");m.textContent=c,r.appendChild(m)});var n=this.element.getBoundingClientRect(),i=a&&a.clientX?a.clientX:e.left+e.width/2,s=a&&a.clientY?a.clientY:e.top+10;r.classList.add("vpa-on");var l=r.offsetWidth,p=r.offsetHeight;r.style.left=at(i-n.left-l/2,4,Math.max(4,n.width-l-4))+"px";var f=s-n.top-p-14;f<0&&(f=s-n.top+18),r.style.top=f+"px"};function ct(t,e,a){e>t||(e=t+1);var r=e-t,n=Math.pow(10,Math.floor(Math.log(r/a)/Math.LN10)),i=r/a/n;i>=7.5?n*=10:i>=3.5?n*=5:i>=1.5&&(n*=2);for(var s=Math.floor(t/n+1e-9)*n,l=Math.ceil(e/n-1e-9)*n,p=[],f=s;f<=l+n/2;f+=n)p.push(Math.round(f/n)*n);return{lo:s,hi:l,ticks:p,step:n}}function I(t,e,a,r,n){var i=G("text",Y({x:e,y:a},n||{}),t);return i.textContent=r,i}S.prototype._bindInteraction=function(t,e,a,r,n,i){var s=this,l=this._items,p=this.options;function f(o){for(var m=t.getBoundingClientRect(),h=(o.clientX-m.left)*(e/m.width),u=(o.clientY-m.top)*(a/m.height),v=0;v<l.length;v++){var b=l[v];if(h>=b.x&&h<=b.x+b.w&&u>=b.y&&u<=b.y+b.h)return v}return-1}function c(o,m){if(o<0){s._hideTip();return}s._active=o;var h=l[o],u=s._hlEl;u.setAttribute("x",h.x-4),u.setAttribute("y",h.y),u.setAttribute("width",h.w+8),u.setAttribute("height",h.h),u.setAttribute("opacity",1);var v=t.getBoundingClientRect(),b=v.width/e,k={left:v.left+h.px*b,top:v.top+h.py*(v.height/a),width:0,height:0};s._placeTip(s._tipLines(h),k,m&&m.clientX?m:null)}t.addEventListener("pointermove",function(o){c(f(o),o)}),t.addEventListener("pointerleave",function(){s._hideTip()}),t.addEventListener("click",function(o){var m=f(o);m>=0&&typeof p.onItemClick=="function"&&p.onItemClick(s._detail(l[m]),o)}),t.addEventListener("keydown",function(o){var m=s._active;if(o.key==="ArrowRight"||o.key==="ArrowDown")m=Math.min(l.length-1,m+1);else if(o.key==="ArrowLeft"||o.key==="ArrowUp")m=Math.max(0,m<0?0:m-1);else if(o.key==="Home")m=0;else if(o.key==="End")m=l.length-1;else if(o.key==="Escape"){s._hideTip();return}else if((o.key==="Enter"||o.key===" ")&&m>=0){typeof p.onItemClick=="function"&&p.onItemClick(s._detail(l[m]),o),o.preventDefault();return}else return;o.preventDefault(),c(m,null)}),t.addEventListener("blur",function(){s._hideTip()})},S.prototype.createTable=function(){var t=this.getExportData(),e=P("div","vpa-tables");return t.sheets.forEach(function(a){if(a.name!=="Notes"){var r=P("table","vpa-table",e);P("caption",null,r).textContent=a.name;var n=P("tr",null,P("thead",null,r));a.columns.forEach(function(s){var l=P("th",null,n);l.scope="col",l.textContent=s.label});var i=P("tbody",null,r);a.text.forEach(function(s,l){var p=P("tr",a.flag&&a.flag[l]?"vpa-breach":null,i);s.forEach(function(f,c){var o=P(c?"td":"th",null,p);c||(o.scope="row"),o.textContent=f})})}}),e},S.prototype.redraw=function(){var t=this.options,e=t.data,a=this._labels(),r=this._res=et(e,Y(this._opts(),{pathSteps:t.pathSteps})),n=lt(t.theme),i=ot(e,t.locale),s=this.element,l=s.style,p=n.fontFamily==="inherit"?K.getComputedStyle?K.getComputedStyle(this.container).fontFamily:"sans-serif":n.fontFamily,f=n.background&&n.background!=="transparent",c=f?n.background:"#ffffff";l.setProperty("--vpa-text",n.text),l.setProperty("--vpa-muted",n.mutedText),l.setProperty("--vpa-bg",f?n.background:"transparent"),l.setProperty("--vpa-font",p),l.setProperty("--vpa-card",n.cardBackground),l.setProperty("--vpa-card-muted",n.cardMuted),l.setProperty("--vpa-tip-bg",n.tooltipBackground),l.setProperty("--vpa-tip-text",n.tooltipText),l.setProperty("--vpa-focus",n.focus),l.setProperty("--vpa-hedge",n.zoneText.red),s.classList.toggle("vpa-filled",!!f),s.setAttribute("aria-label",t.ariaLabel||e.title||"P&L attribution");var o=t.view;["factors","daily","distribution"].indexOf(o)<0&&(Q('Unknown view "'+o+'". The factor view is shown instead.'),o="factors"),r.shortWindow&&!this._warnedShort&&(Q("There are fewer days than the "+(t.window||250)+"-day window; all "+r.window+" are used."),this._warnedShort=!0),this._view=o,this._renderSummary(r,i,a,n),this.scopeEl.textContent=[a[o],r.window+"-day window",e.unit||null,r.fixed.length?r.fixed.length+" fix"+(r.fixed.length>1?"es":"")+" applied":null].filter(Boolean).join(" \xB7 "),this._renderLegend(o,n,a,r);var m=Math.max(280,Math.round(this.plotHost.clientWidth||s.clientWidth||720));this._lastWidth=Math.round(s.clientWidth);var h=n.fontSize||12;this.plotHost.textContent="";var u=G("svg",{class:"vpa-plot",role:"img",tabindex:0,"font-family":p},this.plotHost);u.style.fontSize=h+"px";var v=this._animateNext&&t.animate;this._animateNext=!1,v&&u.classList.add("vpa-animate");var b={svg:u,W:m,fs:h,theme:n,f:i,L:a,family:p,bg:c,res:r,items:[],points:{}},k=o==="daily"?this._drawDaily(b):o==="distribution"?this._drawDistribution(b):this._drawFactors(b);return u.setAttribute("viewBox","0 0 "+m+" "+k),u.setAttribute("width",m),u.setAttribute("height",k),u.setAttribute("aria-label",(t.ariaLabel||e.title||"P&L attribution")+". "+this.describe()),this._items=b.items,this._points=b.points,this._hlEl=G("rect",{x:0,y:0,width:0,height:0,rx:4,fill:n.text,"fill-opacity":.06,opacity:0,"pointer-events":"none"},u),u.insertBefore(this._hlEl,u.firstChild),this._bindInteraction(u,m,k,n,a,i),this};function F(t){return t==null?"n/a":t.toFixed(2)}S.prototype._zoneName=function(t){var e=this._labels();return e[t]||t},S.prototype._renderSummary=function(t,e,a,r){var n=this.summaryEl,i=this.options,s=this;if(n.textContent="",n.style.display=i.summary===!1?"none":"",i.summary===!1)return;function l(c,o,m,h,u){var v=P("div","vpa-stat",n);P("div","k",v).textContent=c;var b=P("div",h?"v "+h:"v",v);b.textContent=o,u&&(b.style.color=u),m&&(P("div","n",v).textContent=m)}var p=t.test,f=t.order[0];l(a.test,this._zoneName(p.zone),"Spearman "+F(p.spearman)+" \xB7 KS "+F(p.ks)+" ("+t.window+" days)","vpa-zone",r.zoneText[p.zone]),l(a.unexplainedCard,"\xB1"+e.value(t.sdUnexplained),(t.volRatio!=null?e.percent(t.volRatio,0)+" of HPL volatility \xB7 ":"")+"mean "+e.signed(t.meanUnexplained)+" a day"),l(a.largest,f.label,e.percent(f.share,0)+" of the unexplained volatility","vpa-wrap"),p.zone==="green"?l(a.path,"Already green","margin: Spearman +"+(p.spearman-t.thresholds.spearmanGreen).toFixed(2)+", KS \u2212"+(t.thresholds.ksGreen-p.ks).toFixed(2)):t.pathReachesGreen?l(a.path,"Fix "+t.path.length+" factor"+(t.path.length>1?"s":""),t.path.map(function(c){return c.label}).join(", ")+" \u2192 Spearman "+F(t.path[t.path.length-1].test.spearman)+", KS "+F(t.path[t.path.length-1].test.ks),"vpa-wrap"):l(a.path,t.path.length?"Not within "+t.path.length+" fix"+(t.path.length>1?"es":""):"No single fix helps",t.path.length?"best reached: "+this._zoneName(t.path[t.path.length-1].test.zone).toLowerCase():"the gap is spread across factors"),Array.prototype.forEach.call(n.querySelectorAll(".vpa-stat .v"),function(c){c.style.fontSize="";for(var o=parseFloat(K.getComputedStyle?K.getComputedStyle(c).fontSize:19)||19;c.scrollWidth>c.clientWidth&&o>12;)o-=1,c.style.fontSize=o+"px"})},S.prototype._renderLegend=function(t,e,a,r){var n=this.legendEl;n.textContent="",n.style.display=this.options.legend===!1?"none":"";function i(s,l,p){var f=P("span",null,n),c=P("i",p||null,f);p==="vpa-ring"?c.style.borderColor=s:c.style.background=s,f.appendChild(document.createTextNode(l))}if(t==="factors"){i(e.contribution,a.adds),i(e.offset,a.offsets),i(e.standalone,a.standalone,"vpa-ring");return}i(e.hpl,a.hpl,"vpa-tick"),i(e.rtpl,a.rtpl,"vpa-tick"),t==="daily"&&(i(e.unexplained,a.unexplained),r.start>0&&i(e.outside,a.outside))};function Ct(t,e){var a=Math.min(Math.max.apply(null,e.map(function(n){return B(n,t.fs,600,t.family)}))+12,t.W*.34),r=t.W-a<300;return{nameW:r?0:a,narrow:r}}function _t(t,e,a,r,n,i,s,l,p,f){var c=t.theme,o=t.fs,m=t.family,h=t.W,u=-1/0;a.ticks.forEach(function(v){G("line",{x1:r(v),x2:r(v),y1:l,y2:p,stroke:v===0?c.axis:c.grid,"stroke-width":v===0?1.2:1},e);var b=f(v),k=B(b,o-1,400,m),L=r(v),d=L-k/2<0?"start":L+k/2>h?"end":"middle",y=d==="start"?L:d==="end"?L-k:L-k/2;y<u+8||(u=y+k,I(e,L,n+o,b,{"text-anchor":d,fill:c.axisText,"font-size":o-1,class:"vpa-tick"}))})}S.prototype._drawFactors=function(t){var e=t.res,a=t.theme,r=t.f,n=t.L,i=t.fs,s=t.family,l=t.W,p=t.svg,f=this,c=+this.options.top>0?Math.floor(+this.options.top):e.order.length,o=e.order.slice(c),m=e.order.slice(0,c).sort(function(g,C){return C.contribution-g.contribution}).map(function(g){return{kind:"factor",obj:g,name:g.label+(g.modelled?"":" *"),v:g.contribution,k:g.sdGap}});if(o.length){var h=0,u=0;o.forEach(function(g){h+=g.contribution,u+=g.sdGap}),m.push({kind:"other",obj:{count:o.length,contribution:h,factors:o},name:xt(n.other,{n:o.length}),v:h,k:null})}var v=Ct(t,m.map(function(g){return g.name})),b=Math.max(B("\u2192 "+n.amber,i-1,600,s),B("\u2192 "+n.green,i-1,600,s),B("\u2192 "+n.red,i-1,600,s))+14,k=Math.max.apply(null,m.map(function(g){return B(r.signed(g.v),i,700,s)}))+10,L=v.nameW,d=l-k-b,y=Math.max(26,i*2.1),_=v.narrow?i+4:0,M=4,$=Math.min(0,Math.min.apply(null,m.map(function(g){return g.v}))),O=Math.max.apply(null,m.map(function(g){return Math.max(g.v,g.k||0)}))||1,D=ct($,O,Math.max(2,Math.min(6,Math.floor((d-L)/90)))),R=function(g){return L+(at(g,D.lo,D.hi)-D.lo)/(D.hi-D.lo)*(d-L)},H=M+i+6,A=H+m.length*(y+_),W=G("g",{class:"vpa-factors"},p);_t(t,W,D,R,M,L,d,H,A,function(g){return r.value(g)}),I(W,l,M+i,n.ifFixed,{"text-anchor":"end",fill:a.axisText,"font-size":i-1,class:"vpa-tick"}),m.forEach(function(g,C){var w=H+C*(y+_)+_,T=y*.56,N=w+(y-T)/2,U=g.v<0&&r.value(-g.v)!==r.value(0);I(W,0,v.narrow?w-3:N+T/2+i*.35,rt(g.name,v.narrow?l-b:v.nameW-8,i,g.kind==="other"?400:600,s),{fill:g.kind==="other"?a.mutedText:a.text,"font-size":i,"font-weight":g.kind==="other"?400:600,class:"vpa-name"});var z=R(Math.min(0,g.v)),Z=R(Math.max(0,g.v));if(G("rect",{x:z,y:N,width:Math.max(.8,Z-z),height:T,rx:2,fill:U?a.offset:a.contribution,class:"vpa-grow vpa-cbar"+(U?" vpa-offset":"")},W),g.k!=null){var V=R(g.k),X=N+T/2;G("path",{d:"M"+V+","+(X-5)+"L"+(V+5)+","+X+"L"+V+","+(X+5)+"L"+(V-5)+","+X+"Z",fill:t.bg,stroke:a.standalone,"stroke-width":1.6,class:"vpa-sa"},W)}if(I(W,l-b,N+T/2+i*.35,r.signed(g.v),{"text-anchor":"end",fill:U?a.offsetText:a.text,"font-size":i,"font-weight":700,class:"vpa-val"}),g.kind==="factor"){var J=g.obj.ifFixed.zone;I(W,l,N+T/2+i*.35,"\u2192 "+f._zoneName(J),{"text-anchor":"end",fill:a.zoneText[J],"font-size":i-1,"font-weight":600,class:"vpa-zonefix"}),t.points[g.obj.key]={x:U?z:Z,y:N}}t.items.push({kind:g.kind,obj:g.obj,x:0,y:w-_,w:l,h:y+_,px:U?z:Z,py:N})});var E=["Shares add up to the unexplained volatility of \xB1"+r.value(e.sdUnexplained)+" a day over "+e.window+" days"];e.factors.some(function(g){return!g.modelled})&&E.push("* "+n.notModelled+" (RTPL taken as zero)"),E.push("\u201C"+n.ifFixed+"\u201D: test zone with that factor's RTPL set equal to its HPL");var x=A+i+8;return E.forEach(function(g){I(p,0,x,rt(g+".",l,i-1,400,s),{fill:a.mutedText,"font-size":i-1,class:"vpa-note"}),x+=i+3}),Math.ceil(x-i+6)},S.prototype._drawDaily=function(t){var e=t.res,a=t.theme,r=t.f,n=t.L,i=t.fs,s=t.family,l=t.W,p=t.svg,f=e.dates.length,c=e.hpl.concat(e.rtpl,e.unexplained),o=ct(Math.min(0,Math.min.apply(null,c)),Math.max(0,Math.max.apply(null,c)),l<480?4:6),m=Math.max.apply(null,o.ticks.map(function(w){return B(r.value(w),i-1,400,s)})),h=m+10,u=l-4,v=6,b=l<480?220:300,k=v+b,L=function(w){return h+(w+.5)/f*(u-h)},d=function(w){return v+(o.hi-at(w,o.lo,o.hi))/(o.hi-o.lo)*b},y=G("g",{class:"vpa-daily"},p);e.start>0&&G("rect",{x:h,y:v,width:L(e.start)-(u-h)/f/2-h,height:b,fill:a.outside,"fill-opacity":.35,class:"vpa-outside"},y),o.ticks.forEach(function(w){G("line",{x1:h,x2:u,y1:d(w),y2:d(w),stroke:w===0?a.axis:a.grid,"stroke-width":w===0?1.2:1},y),I(y,h-6,d(w)+(i-1)*.35,r.value(w),{"text-anchor":"end",fill:a.axisText,"font-size":i-1,class:"vpa-tick"})});var _=Math.max(.6,(u-h)/f*.7);e.unexplained.forEach(function(w,T){var N=d(Math.max(0,w)),U=d(Math.min(0,w));G("rect",{x:L(T)-_/2,y:N,width:_,height:Math.max(.6,U-N),fill:a.unexplained,"fill-opacity":T<e.start?.4:.85,class:"vpa-grow vpa-ubar"},y)});var M=function(w){return w.map(function(T,N){return(N?"L":"M")+L(N).toFixed(2)+","+d(T).toFixed(2)}).join("")};G("path",{d:M(e.hpl),fill:"none",stroke:a.hpl,"stroke-width":1.6,"stroke-linejoin":"round",class:"vpa-line vpa-hpl"},y),G("path",{d:M(e.rtpl),fill:"none",stroke:a.rtpl,"stroke-width":1.6,"stroke-dasharray":"4 2","stroke-linejoin":"round",class:"vpa-line vpa-rtpl"},y);for(var $=B(e.dates[0],i-1,400,s)+16,O=Math.max(1,Math.ceil($/((u-h)/f))),D=-1/0,R=0;R<f;R+=O){var H=L(R),A=B(e.dates[R],i-1,400,s),W=H-A/2<0?"start":H+A/2>l?"end":"middle",E=W==="start"?H:W==="end"?H-A:H-A/2;E<D+8||(D=E+A,I(y,H,k+i+9,e.dates[R],{"text-anchor":W,fill:a.axisText,"font-size":i-1,class:"vpa-tick"}))}var x=(u-h)/f;e.dates.forEach(function(w,T){t.items.push({kind:"day",index:T,x:L(T)-x/2,y:v,w:x,h:b,px:L(T),py:d(Math.max(e.hpl[T],e.rtpl[T]))}),t.points["date:"+w]={x:L(T),y:d(Math.max(e.hpl[T],e.rtpl[T]))}});var g=["Unexplained P&L \xB1"+r.value(e.sdUnexplained)+" a day ("+(e.volRatio!=null?r.percent(e.volRatio,0)+" of HPL volatility":"")+") over the last "+e.window+" days"],C=k+i*2+17;return g.forEach(function(w){I(p,0,C,rt(w+".",l,i-1,400,s),{fill:a.mutedText,"font-size":i-1,class:"vpa-note"}),C+=i+3}),Math.ceil(C-i+6)},S.prototype._drawDistribution=function(t){var e=t.res,a=t.theme,r=t.f,n=t.L,i=t.fs,s=t.family,l=t.W,p=t.svg,f=e.distribution,c=f.hpl.length,o=e.thresholds,m=Math.min(f.hpl[0],f.rtpl[0]),h=Math.max(f.hpl[c-1],f.rtpl[c-1]),u=ct(m,h,l<480?4:6),v=B("100%",i-1,400,s)+10,b=l-6,k=8,L=l<480?200:270,d=k+L,y=function(z){return v+(at(z,u.lo,u.hi)-u.lo)/(u.hi-u.lo)*(b-v)},_=function(z){return k+(1-z)*L},M=G("g",{class:"vpa-dist"},p);[0,.25,.5,.75,1].forEach(function(z){G("line",{x1:v,x2:b,y1:_(z),y2:_(z),stroke:z===0?a.axis:a.grid},M),I(M,v-6,_(z)+(i-1)*.35,Math.round(z*100)+"%",{"text-anchor":"end",fill:a.axisText,"font-size":i-1,class:"vpa-tick"})}),_t(t,M,u,y,d+8,v,b,k,d,function(z){return r.value(z)});var $=function(z){var Z="M"+y(u.lo)+","+_(0);return z.forEach(function(V,X){Z+="L"+y(V).toFixed(2)+","+_(X/c).toFixed(2)+"L"+y(V).toFixed(2)+","+_((X+1)/c).toFixed(2)}),Z+"L"+y(u.hi)+","+_(1)};G("path",{d:$(f.hpl),fill:"none",stroke:a.hpl,"stroke-width":1.8,class:"vpa-line vpa-hpl"},M),G("path",{d:$(f.rtpl),fill:"none",stroke:a.rtpl,"stroke-width":1.8,"stroke-dasharray":"4 2",class:"vpa-line vpa-rtpl"},M);var O=function(z,Z){for(var V=0,X=z.length;V<X;){var J=V+X>>1;z[J]<=Z?V=J+1:X=J}return V/z.length},D=e.test.ksAt,R=O(f.hpl,D),H=O(f.rtpl,D),A=y(D),W=e.test.zone;G("line",{x1:A,x2:A,y1:_(R),y2:_(H),stroke:a.zoneText[W],"stroke-width":3,class:"vpa-ksline"},M);var E="KS "+F(e.test.ks),x=B(E,i,700,s),g=A+8+x>l?A-8:A+8;G("rect",{x:(A+8+x>l?g-x:g)-3,y:(_(R)+_(H))/2-i*.7,width:x+6,height:i*1.4,rx:3,fill:t.bg,class:"vpa-lbg"},M),I(M,g,(_(R)+_(H))/2+i*.35,E,{"text-anchor":A+8+x>l?"end":"start",fill:a.zoneText[W],"font-size":i,"font-weight":700,class:"vpa-kslabel"});for(var C=0;C<=20;C++){var w=u.lo+(u.hi-u.lo)*C/20,T=(b-v)/20;t.items.push({kind:"point",value:w,ph:O(f.hpl,w),pr:O(f.rtpl,w),x:y(w)-T/2,y:k,w:T,h:L,px:y(w),py:_(Math.max(O(f.hpl,w),O(f.rtpl,w)))})}var N=["Spearman "+F(e.test.spearman)+" (green above "+o.spearmanGreen.toFixed(2)+", red below "+o.spearmanRed.toFixed(2)+") \xB7 KS "+F(e.test.ks)+" (green below "+o.ksGreen.toFixed(2)+", red above "+o.ksRed.toFixed(2)+")","Share of the "+e.window+" days with P&L at or below each value"],U=d+i*2+12;return N.forEach(function(z){I(p,0,U,rt(z+".",l,i-1,400,s),{fill:a.mutedText,"font-size":i-1,class:"vpa-note"}),U+=i+3}),Math.ceil(U-i+6)},S.prototype._tipLines=function(t){var e=this.options,a=ot(e.data,e.locale),r=this._labels(),n=this._res,i=this;if(t.kind==="other")return[xt(r.other,{n:t.obj.count}),"Together "+a.signed(t.obj.contribution)+" of the unexplained volatility"];if(t.kind==="day"){var s=t.index,l=n.factors.map(function(c){return{l:c.label,g:c.gap[s]}}).filter(function(c){return c.g!==0}).sort(function(c,o){return Math.abs(o.g)-Math.abs(c.g)}).slice(0,3);return[n.dates[s]+(s<n.start?" \xB7 "+r.outside.toLowerCase():""),r.hpl+" "+a.signed(n.hpl[s]),r.rtpl+" "+a.signed(n.rtpl[s]),r.unexplained+" "+a.signed(n.unexplained[s])].concat(l.length?["Largest gaps: "+l.map(function(c){return c.l+" "+a.signed(c.g)}).join(", ")]:[])}if(t.kind==="point")return["P&L at or below "+a.signed(t.value),"HPL "+a.percent(t.ph,0)+" of days \xB7 RTPL "+a.percent(t.pr,0),"Gap "+a.percent(Math.abs(t.ph-t.pr),0)+" (KS is the largest gap: "+F(n.test.ks)+")"];var p=t.obj,f=p.ifFixed;return[p.label+(p.modelled?"":" ("+r.notModelled+")"),p.group+" \xB7 rank "+p.rank+" of "+n.factors.length,"Share of unexplained volatility "+a.signed(p.contribution)+" ("+a.percent(p.share,0)+")","Gap on its own \xB1"+a.value(p.sdGap)+" a day \xB7 mean "+a.signed(p.meanGap),"HPL "+a.signed(p.hplSum)+" \xB7 RTPL "+a.signed(p.rtplSum)+" over "+n.window+" days",r.ifFixed+": "+i._zoneName(f.zone).toLowerCase()+" (Spearman "+F(f.spearman)+", KS "+F(f.ks)+")"]},S.prototype._detail=function(t){if(!t)return null;var e=this._res,a=this._view;if(t.kind==="other")return{view:a,kind:"other",count:t.obj.count,contribution:t.obj.contribution,factors:t.obj.factors.map(function(i){return i.key})};if(t.kind==="day"){var r=t.index;return{view:a,kind:"day",date:e.dates[r],hpl:e.hpl[r],rtpl:e.rtpl[r],unexplained:e.unexplained[r],inWindow:r>=e.start,gaps:e.factors.map(function(i){return{key:i.key,gap:i.gap[r]}})}}if(t.kind==="point")return{view:a,kind:"point",value:t.value,hplShare:t.ph,rtplShare:t.pr};var n=t.obj;return{view:a,kind:"factor",key:n.key,label:n.label,group:n.group,modelled:n.modelled,fixed:n.fixed,rank:n.rank,contribution:n.contribution,share:n.share,sdGap:n.sdGap,meanGap:n.meanGap,hplSum:n.hplSum,rtplSum:n.rtplSum,ifFixed:{spearman:n.ifFixed.spearman,ks:n.ifFixed.ks,zone:n.ifFixed.zone}}},S.prototype.howToRead=function(){var t=this._view||this.options.view;return t==="daily"?"The dark line is front-office hypothetical P&L (HPL) and the dashed line the risk model's P&L (RTPL), day by day. Grey bars are the difference, the unexplained P&L. Only the most recent days, inside the test window, count for the test.":t==="distribution"?"Each line shows the share of days with P&L at or below a value, for HPL and for RTPL. If the risk model matched the front office, the two lines would sit on top of each other. The coloured bar marks the largest gap between them, the Kolmogorov-Smirnov (KS) statistic.":"Each bar is one risk factor's share of the unexplained P&L volatility (HPL \u2212 RTPL). Shares add up exactly to the total; a bar to the left means the factor's gap offsets other gaps. The diamond is the factor's gap on its own, and the right column shows the test zone the desk would reach if that factor's gap were closed."},S.prototype.describe=function(){var t=this.options,e=ot(t.data,t.locale),a=this._labels(),r=this._res||et(t.data,this._opts()),n=r.test,i=r.thresholds,s=[],l=this,p=[];(n.spearman==null||n.spearman<=i.spearmanGreen)&&p.push("Spearman "+F(n.spearman)+(n.spearman!=null&&n.spearman>=i.spearmanRed?" is below "+i.spearmanGreen.toFixed(2):" is below "+i.spearmanRed.toFixed(2))),n.ks>=i.ksGreen&&p.push("KS "+F(n.ks)+(n.ks<=i.ksRed?" is above "+i.ksGreen.toFixed(2):" is above "+i.ksRed.toFixed(2))),s.push("Over the last "+r.window+" days the desk is in the "+this._zoneName(n.zone).toLowerCase()+" zone of the P&L attribution test"+(p.length?": "+p.join(" and ")+".":" (Spearman "+F(n.spearman)+", KS "+F(n.ks)+").")),s.push("Unexplained P&L is \xB1"+e.value(r.sdUnexplained)+" a day"+(r.volRatio!=null?", "+e.percent(r.volRatio,0)+" of the front office's P&L volatility":"")+".");var f=r.order[0],c=r.order[1];s.push(f.label+(f.modelled?"":", which is not in the risk model,")+" explains "+e.percent(f.share,0)+" of it"+(c?", followed by "+c.label+" ("+e.percent(c.share,0)+")":"")+".");var o=r.factors.filter(function(h){return h.share<-.02}).sort(function(h,u){return h.contribution-u.contribution});if(o.length&&s.push("The gap on "+o[0].label+" partly offsets the others."),n.zone!=="green")if(r.pathReachesGreen){var m=r.path[r.path.length-1].test;s.push("Closing the gap on "+r.path.map(function(h){return h.label}).join(" and ")+" would bring the desk to green (Spearman "+F(m.spearman)+", KS "+F(m.ks)+").")}else r.path.length&&s.push("Even closing the gaps on "+r.path.map(function(h){return h.label}).join(", ")+" only reaches "+this._zoneName(r.path[r.path.length-1].test.zone).toLowerCase()+".");return r.fixed.length&&s.push("Fixes applied: "+r.fixed.join(", ")+"."),s.join(" ")},S.prototype.getExportData=function(){var t=this.options,e=t.data,a=this._labels(),r=ot(e,t.locale),n=this._res||et(e,this._opts()),i=this,s=n.thresholds,l={prefix:e.prefix||"",suffix:e.suffix||"",decimals:e.decimals!=null?e.decimals:0,negatives:e.negatives},p=function(d){return{label:d,type:"number",unit:l}},f=function(d){return{label:d,type:"number",unit:{decimals:3}}},c=function(d){return{label:d,type:"percent",decimals:1}},o=function(d){return{label:d,type:"text"}},m=[];m.push({name:"Factors",columns:[{label:"Rank",type:"number",unit:{decimals:0}},o("Risk factor"),o("Group"),o("In risk model"),o("Fixed"),p("HPL over window"),p("RTPL over window"),p("Mean daily gap"),p("Gap volatility"),p("Share of unexplained volatility"),c("Share (%)"),f("Spearman if fixed"),f("KS if fixed"),o("Zone if fixed")],rows:n.order.map(function(d){return[d.rank,d.label,d.group,d.modelled?"Yes":"No",d.fixed?"Yes":"No",d.hplSum,d.rtplSum,d.meanGap,d.sdGap,d.contribution,d.share,d.ifFixed.spearman,d.ifFixed.ks,i._zoneName(d.ifFixed.zone)]}),text:n.order.map(function(d){return[String(d.rank),d.label,d.group,d.modelled?"Yes":"No",d.fixed?"Yes":"No",r.signed(d.hplSum),r.signed(d.rtplSum),r.signed(d.meanGap),r.value(d.sdGap),r.signed(d.contribution),r.percent(d.share),F(d.ifFixed.spearman),F(d.ifFixed.ks),i._zoneName(d.ifFixed.zone)]}),flag:n.order.map(function(d){return!d.modelled})});for(var h=t.exportDays!=null?t.exportDays:2e4,u=Math.max(0,n.dates.length-h),v=[],b=u;b<n.dates.length;b++)v.push(b);m.push({name:"Daily",columns:[o("Date"),p("HPL"),p("RTPL"),p("Unexplained"),o("In test window")],rows:v.map(function(d){return[n.dates[d],n.hpl[d],n.rtpl[d],n.unexplained[d],d>=n.start?"Yes":"No"]}),text:v.map(function(d){return[n.dates[d],r.signed(n.hpl[d]),r.signed(n.rtpl[d]),r.signed(n.unexplained[d]),d>=n.start?"Yes":"No"]})});var k=[["Spearman correlation",n.test.spearman,"green above "+s.spearmanGreen+", red below "+s.spearmanRed],["Kolmogorov-Smirnov statistic",n.test.ks,"green below "+s.ksGreen+", red above "+s.ksRed],["Unexplained volatility \xF7 HPL volatility",n.volRatio,""],["Mean unexplained \xF7 HPL volatility",n.meanRatio,""],["Variance of unexplained \xF7 variance of HPL",n.varRatio,""]];n.path.forEach(function(d,y){k.push(["Path step "+(y+1)+": fix "+d.label+" \u2192 Spearman",d.test.spearman,i._zoneName(d.test.zone)],["Path step "+(y+1)+": fix "+d.label+" \u2192 KS",d.test.ks,i._zoneName(d.test.zone)])}),m.push({name:"Test",columns:[o("Measure"),f("Value"),o("Note")],rows:k,text:k.map(function(d){return[d[0],d[1]==null?"n/a":d[1].toFixed(3),d[2]]})});var L=(t.annotations||[]).map(function(d,y){return[String(y+1),d.factor!=null?String(d.factor):"Date: "+String(d.date||""),d.text||""]});return L.length&&m.push({name:"Notes",columns:[o("No."),o("Factor or date"),o("Note")],rows:L}),{title:e.title||"P&L attribution",subtitle:e.subtitle||"",summary:this.describe(),library:"Veyforma P&L Attribution "+mt,sheets:m,meta:[["Unit",e.unit],["As of",e.asOf],["Desk",e.desk],["Test window",n.window+" days ("+n.dates[n.start]+" to "+n.dates[n.dates.length-1]+")"],["PLA test zone",i._zoneName(n.test.zone)+" (Spearman "+F(n.test.spearman)+", KS "+F(n.test.ks)+")"],["Fixes applied",n.fixed.length?n.fixed.join(", "):null],["Method","Spearman = correlation of the ranks of daily HPL and RTPL (ties averaged); KS = largest gap between their empirical distribution functions; green if Spearman > "+s.spearmanGreen+" and KS < "+s.ksGreen+", red if Spearman < "+s.spearmanRed+" or KS > "+s.ksRed+`, otherwise amber; factor share = cov(gap, unexplained) \xF7 sd(unexplained), adds up to sd(unexplained); "if fixed" sets that factor's RTPL equal to its HPL`+(u>0?"; daily sheet limited to the last "+h+" days":"")],["Data source",e.source]]}},S.prototype._annotationPoint=function(t){var e=this._points||{};return t.factor!=null?e[String(t.factor)]||null:t.date!=null&&e["date:"+String(t.date)]||null},S.prototype._annotationTarget=function(t){var e=this._items||[],a=null;if(!t)a=e[this._active];else for(var r=this.plotHost.querySelector("svg"),n=r.getBoundingClientRect(),i=r.viewBox.baseVal,s=(t.clientX-n.left)*(i.width/n.width),l=(t.clientY-n.top)*(i.height/n.height),p=0;p<e.length;p++){var f=e[p];if(s>=f.x&&s<=f.x+f.w&&l>=f.y&&l<=f.y+f.h){a=f;break}}return a?a.kind==="factor"?{factor:a.obj.key}:a.kind==="day"?{date:this._res.dates[a.index]}:null:null},S.prototype._annotationLabel=function(t){if(t.date!=null)return String(t.date);var e=(this._res||this.getResult()).factors.filter(function(a){return a.key===String(t.factor)})[0];return e?e.label:String(t.factor||"")},K.VeyPlaAttribution={version:mt,create:function(t,e){return new S(t,e)},compute:function(t,e){return et(t,e)},test:function(t,e,a){return st(t,e,Y({},dt,a||{}))},thresholds:dt,listThemes:function(){return Object.keys(pt)},getTheme:function(t){return lt(t)},defaults:{options:vt,labels:ut,theme:nt}}})(typeof window<"u"?window:void 0);