UNPKG

crit-maxfest

Version:

Criticide Application Library

2 lines (1 loc) 9.52 kB
import{prepend,reduce,mergeDeepLeft}from"ramda";var name="crit-maxfest",version="1.0.48",description="Criticide Application Library",main="./dist/crit-maxfest.cjs.js",module$1="./dist/crit-maxfest.esm.js",unpkg="./dist/crit-maxfest.umd.js",browser="./dist/crit-maxfest.umd.js",license="AGPL-3.0",scripts={test:"mocha --check-leaks --reporter=spec --require babel-register",build:"rollup --config rollup.config.js"},dependencies={financejs:"^4.1.0",ramda:"^0.25.0","vega-statistics":"^1.2.1"},devDependencies={"babel-cli":"^6.26.0","babel-core":"^6.26.0","babel-helpers":"^6.24.1","babel-plugin-external-helpers":"^6.22.0","babel-preset-env":"^1.6.1","babel-preset-flow":"^6.23.0","babel-register":"^6.26.0",chai:"^4.1.2",dotenv:"^5.0.1","flow-bin":"^0.70.0","flow-typed":"^2.4.0",mocha:"^5.1.1",rollup:"^0.58.2","rollup-plugin-babel":"^3.0.4","rollup-plugin-clear":"^2.0.7","rollup-plugin-commonjs":"^9.1.0","rollup-plugin-flow":"^1.1.1","rollup-plugin-node-resolve":"^3.3.0"},keywords=["application","api","probablity","modeling","simulation","decisions"],author="Ray King <taipan.king@gmail.com>",homepage="https://medium.com/critically-deciding",bugs="https://github.com/rayk/critilib/issues",repository={type:"git",url:"git@github.com:rayk/critilib.git"},contributors=[{name:"Ray King",email:"taipan.king@gmail.com",url:"https://github.com/rayk"},{name:"Amanda Woo",email:"amandawoo@interestingbydefault.com",url:"https://github.com/awoo"},{name:"Marko Vuksanovic",email:"markovuksanovic@gmail.com",url:"https://github.com/markovuksanovic"}],pkg={name:name,version:version,description:description,main:main,module:module$1,unpkg:unpkg,browser:browser,license:license,scripts:scripts,dependencies:dependencies,devDependencies:devDependencies,keywords:keywords,author:author,homepage:homepage,bugs:bugs,repository:repository,contributors:contributors,private:!1};function createCommonjsModule(e,r){return e(r={exports:{}},r.exports),r.exports}var finance=createCommonjsModule(function(e,r){var t=function(){};function n(e,r,t){for(var n=0,a=0,o=0;o<e.length;o++)n+=e[o]/Math.pow(1+t,r[o]);for(o=0;o<e.length;o++)a+=-e[o]*r[o]*Math.pow(1+t,-1-r[o]);return n/a}t.prototype.PV=function(e,r,t){t=void 0!==t?t:1;var n;e/=100;return n=r/Math.pow(1+e,t),Math.round(100*n)/100},t.prototype.FV=function(e,r,t){var n;e/=100;return n=r*Math.pow(1+e,t),Math.round(100*n)/100},t.prototype.NPV=function(e){e/=100;for(var r=arguments[1],t=2;t<arguments.length;t++)r+=arguments[t]/Math.pow(1+e,t-1);return Math.round(100*r)/100},t.prototype.IRR=function(e){var r,t,n=arguments,a=1;if(Array.prototype.slice.call(n).forEach(function(e){e>0&&(r=!0),e<0&&(t=!0)}),!r||!t)throw new Error("IRR requires at least one positive value and one negative value");return Math.round(100*function(e){for(var r=1;e(r)>0;)r+=1;for(;e(r)<0;)r-=.01;return r+.01}(function(e){if(++a>1e3)throw new Error("IRR can't find a result");for(var r=1+e/100,t=n[0],o=1;o<n.length;o++)t+=n[o]/Math.pow(r,o);return t}))/100},t.prototype.PP=function(e,r){if(0===e)return Math.abs(arguments[1])/arguments[2];var t=arguments[1],n=1;for(i=2;i<arguments.length;i++){if((t+=arguments[i])>0)return n+=(t-arguments[i])/arguments[i];n++}},t.prototype.ROI=function(e,r){var t=(r-Math.abs(e))/Math.abs(e)*100;return Math.round(100*t)/100},t.prototype.AM=function(e,r,t,n,a){var o,i,u,s=r/12/100;return n?1===n?(o=c(t),i=Math.pow(1+s,t)-1):console.log("not defined"):(o=c(12*t),i=Math.pow(1+s,12*t)-1),u=e*(o/i),Math.round(100*u)/100;function c(e){return a&&(e-=1),s*Math.pow(1+s,e)}},t.prototype.PI=function(e,r){for(var t,n=0,a=2;a<arguments.length;a++){var o;o=1/Math.pow(1+e/100,a-1),n+=arguments[a]*o}return t=n/Math.abs(arguments[1]),Math.round(100*t)/100},t.prototype.DF=function(e,r){for(var t,n=[],a=1;a<r;a++)t=1/Math.pow(1+e/100,a-1),roundedDiscountFactor=Math.ceil(1e3*t)/1e3,n.push(roundedDiscountFactor);return n},t.prototype.CI=function(e,r,t,n){var a=t*Math.pow(1+e/100/r,r*n);return Math.round(100*a)/100},t.prototype.CAGR=function(e,r,t){var n=Math.pow(r/e,1/t)-1;return Math.round(1e4*n)/100},t.prototype.LR=function(e,r,t){return(e+r)/t},t.prototype.R72=function(e){return 72/e},t.prototype.WACC=function(e,r,t,n,a){E=e,D=r,V=e+r,Re=t,Rd=n,T=a;var o=E/V*Re/100+D/V*Rd/100*(1-T/100);return Math.round(1e3*o)/10},t.prototype.PMT=function(e,r,t){return-t*e/(1-Math.pow(1+e,-r))},t.prototype.IAR=function(e,r){return 100*((1+e)/(1+r)-1)},t.prototype.XIRR=function(e,r,t){if(e.length!=r.length)throw new Error("Number of cash flows and dates should match");var a,o;if(Array.prototype.slice.call(e).forEach(function(e){e>0&&(a=!0),e<0&&(o=!0)}),!a||!o)throw new Error("XIRR requires at least one positive value and one negative value");t=t||0;var i,u,s,c=100,l=[];l.push(0);for(var p=1;p<r.length;p++)l.push((u=r[0],s=r[p],Math.abs(s.getTime()-u.getTime())/31536e6));do{t=(i=t)-n(e,l,i),c--}while(i.toFixed(5)!=t.toFixed(5)&&c>0);var f=i.toFixed(5)!=t.toFixed(5)?null:100*t;return Math.round(100*f)/100},e.exports&&(e.exports=t,e.exports.Finance=t)}),finance_1=finance.Finance,random=Math.random;function ascending(e,r){return e<r?-1:e>r?1:e>=r?0:NaN}function bisector(e){return 1===e.length&&(e=ascendingComparator(e)),{left:function(r,t,n,a){for(null==n&&(n=0),null==a&&(a=r.length);n<a;){var o=n+a>>>1;e(r[o],t)<0?n=o+1:a=o}return n},right:function(r,t,n,a){for(null==n&&(n=0),null==a&&(a=r.length);n<a;){var o=n+a>>>1;e(r[o],t)>0?a=o:n=o+1}return n}}}function ascendingComparator(e){return function(r,t){return ascending(e(r),t)}}var ascendingBisect=bisector(ascending);function gaussian(e,r){var t,n,a=NaN,o={mean:function(e){return arguments.length?(t=e||0,a=NaN,o):t},stdev:function(e){return arguments.length?(n=null==e?1:e,a=NaN,o):n},sample:function(){var e,r,o=0,i=0;if(a==a)return o=a,a=NaN,o;do{e=(o=2*random()-1)*o+(i=2*random()-1)*i}while(0===e||e>1);return r=Math.sqrt(-2*Math.log(e)/e),a=t+i*r*n,t+o*r*n},pdf:function(e){var r=Math.exp(Math.pow(e-t,2)/(-2*Math.pow(n,2)));return 1/(n*Math.sqrt(2*Math.PI))*r},cdf:function(e){var r,a=(e-t)/n,o=Math.abs(a);if(o>37)r=0;else{var i=Math.exp(-o*o/2);o<7.07106781186547?(r=i*((((((.0352624965998911*o+.700383064443688)*o+6.37396220353165)*o+33.912866078383)*o+112.079291497871)*o+221.213596169931)*o+220.206867912376),r/=((((((.0883883476483184*o+1.75566716318264)*o+16.064177579207)*o+86.7807322029461)*o+296.564248779674)*o+637.333633378831)*o+793.826512519948)*o+440.413735824752):r=i/(o+1/(o+2/(o+3/(o+4/(o+.65)))))/2.506628274631}return a>0?1-r:r},icdf:function(e){if(e<=0||e>=1)return NaN;var r=2*e-1,a=8*(Math.PI-3)/(3*Math.PI*(4-Math.PI)),o=2/(Math.PI*a)+Math.log(1-Math.pow(r,2))/2,i=Math.log(1-r*r)/a,u=(r>0?1:-1)*Math.sqrt(Math.sqrt(o*o-i)-o);return t+n*Math.SQRT2*u}};return o.mean(e).stdev(r)}var toConsumableArray=function(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)},fin=new finance,internalReturnRate=function(e,r,t){var n=new finance,a=Array(t).fill(r/t),o=1===Math.sign(e)?-1*e:e,i=prepend(o,a);try{n.IRR.apply(n,toConsumableArray(i))}catch(e){var u;(u=console).log.apply(u,[e.message].concat(toConsumableArray(i),[r]))}return n.IRR.apply(n,toConsumableArray(i))},probableValue=function(e,r){var t=standardMean(e);return gaussian(t.mean,t.stdDev).icdf(r)},standardMean=function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],t=2===e.length&&r?prepend((Math.max.apply(Math,toConsumableArray(e))-Math.min.apply(Math,toConsumableArray(e)))/2,e):e,n=reduce(function(e,r){var t=e.sum+r,n=e.count+1,a=r*r,o=t/n,i=e.sqSum+a;return{mean:o,stDev:Math.sqrt(i/n-o*o),count:n,sum:t,sqSum:i}},{mean:0,stDev:0,sum:0,count:0,sqSum:0},t);return Object.freeze({mean:n.mean,count:n.count,stdDev:n.stDev})},presentValue=function(e,r){var t=r<1?100*r:r;return fin.PV(t,e)},futureValue=function(e,r,t){var n=r<1?100*r:r;return fin.FV(n,e,t)},setTarget=function(e,r){var t=e>1?e/100:e,n=t*(r>1?r/100:r);return Object.freeze({target:t,upperBoundary:t+n,lowerBoundary:t-n})},rangeConstraintPredicate=function(e){return function(r){return r>=e.lowerBoundary&&r<=e.upperBoundary}},targetVarianceCalc=function(e){return function(r){return(e.target-r)/e.target}},adjustInvestment=function(e,r){var t=e*Math.abs(r);return Math.sign(r)<0?e+t:e-t},adjustRevenue=function(e,r){var t=e*Math.abs(r);return Math.sign(r)<0?e-t:e+t},solveForMaxFesInvest=function(e,r,t){for(var n=rangeConstraintPredicate(setTarget(t,.01)),a=targetVarianceCalc(setTarget(t,.01)),o=0,i=presentValue(e,t),u=!0;u;){var s=internalReturnRate(i,e,r)/100;n(s)?(o=i,u=!1):i=adjustInvestment(i,a(s))}return o},solveForSupportRevenue=function(e,r,t,n){for(var a=rangeConstraintPredicate(setTarget(t,.02)),o=targetVarianceCalc(setTarget(t,.02)),i=futureValue(e,t,r/12),u=0,s=i+i*n,c=!0,l=s,p=0;c;)if(a(p=internalReturnRate(e,s,r)/100))u=s,c=!1;else{var f=adjustRevenue(l,o(p));l=f,s=f+f*n}return u},version$1=function(){return pkg.name+" - Version "+pkg.version+" - License "+pkg.license},defaultValues=Object.freeze({invested:0,margin:.33,period:48,revenue:0,revProb:.5,target:.25}),solveForInvestment=function(e){var r=mergeDeepLeft(e,defaultValues),t=probableValue(r.revenue,r.revProb),n=solveForMaxFesInvest(t,r.period,r.target);return{revenue:t,period:r.period,return:r.target,investment:n}},solveForRevenue=function(e){var r=mergeDeepLeft(e,defaultValues);return{revenue:solveForSupportRevenue(r.invested,r.period,r.target,r.margin),period:r.period,return:r.target,investment:r.invested}};export{version$1 as version,solveForInvestment,solveForRevenue};