cfs.js

0.0.8 • Public • Published

cfa.js

still in beta - ironing some bugs... readme is due to changes

Installation

via npm:

npm i cfs.js

or frontside:

<script src="cfa.js></script>

Contetnts

  1. Time value of money functions
  2. Statistics
  3. Linear Regression
  4. Probability
  5. Derivatives
  6. Helper Functions

Examples

Calculate rolling return in an array

rolling return day by day (you can input any number of days - lower than array length of course)

let array = [1, 1.02, 1.03, 1, 1.03, 1.05]
cfa.yield_array(array2, 1)

returns:

[
  0.020000000000000018,
  0.009803921568627416,
  -0.029126213592232997,
  0.030000000000000027,
  0.01941747572815533
]

Calculate irr of a project

irr functions takes as an input an array where array[0] (first element of the array) is the initial outlay.

const array = [-50, 20, 10, 5, 30, 50]

cfa.irr(array)

returns:

{ rate: 27.860000000001556, NPV: 0.007680729691662336 }

calculated irr must be from <0, 100> extent. irr function uses 0.001 step

Calculate value of a call option using Black Scholes formula:

let vol = 0.2 // standard deviation = 20%
let price = 42 
let strike = 40
let time = 0.5 // in years
let rate = 0.1 // yearly = 10%
let dividend = 0.03 // dividend yield = 3%

cfa.callOption(vol, price, strike, time, rate, dividend)

// {
//   d1: 0.6631966109280495,
//   d2: 0.5217752546907399,
//   Nd1: 0.7463978073568774,
//   Nd2: 0.6990866809248882,
//   callValue: 4.2823136053820505
// }

TIME VALUE OF MONEY FUNCTIONS

CAGR - compounded annual growth rate

cfa.cagr(ending, start, years)

cfa.cagr(125,100,2)

// 0.1180339887498949

Future value

cfa.fv(pv, r, n, m) 

Present value

cfa.pv(fv,r,n,m) 

Continous compounding

cfa.fv_continous(pv, r, n) 

Effective Annual Rate

cfa.ear(r,m) 

Continous Effective Annual Rate

cfa.ear_continous(r) 

Future Value of Ordinary Annuity

cfa.fv_annuity_ordinary(a,r,n)

Present Value of Ordinary Annuity

cfa.pv_annuity_ordinary(a,r,n)

Future Value of Unequal Cash Flows

cfa.fv_unequal_cf(array, r) 

Present Value of Unequal Cash Flows

cfa.pv_unequal_cf(array, r)

Present Value of Perpetuity

cfa.pv_perpetuity(a, r)

Net Present Value

cfa.npv(array, r)

Internal Rate of Return

cfa.irr(array)

returns object:

{rate, NPV}

for instance:

let array = [-50, 20, 10, 5, 30, 50]

cfa.irr(array)

returns:

{ rate: 27.860000000001556, NPV: 0.007680729691662336 }

Statistics

Normal Distribution

using Hastings approximation

cfa.normal(x)

// returns object {pdf, CDF, mean, variance}

cfa.normal(1)


// {
//   pdf: 0.24197072451914337,
//   CDF: 0.8413447721886403,
//   mean: 0,
//   variance: 1
// }

Binomial Coefficient - nCk, n Chooses k

cfa.nCk(9,3)

// 84

Binomial

cfa.binomial(trials, success, probability)

// returns object {pdf, CDF, mean, variance}

cfa.binomial(5,2,0.3)

// {
//   pdf: 0.3086999999999999,
//   CDF: 0.8369199999999997,
//   mean: 1.5,
//   variance: 1.0499999999999998
// }

Median

cfa.median(array)

Mode - Single modality so far

cfa.mode(array)

Weighted Mean

cfa.weighted_mean(array)

Geometric Mean

cfa.geometric_mean(array)

Harmonic Mean

cfa.harmonic_mean(array)

Covariance

cfa.covariance(array_X, array_Y)

Sample variance

cfa.sample_variance(array)

Sample standard deviation

cfa.standard_deviation(array)

Sample correlation

cfa.sample_correlation(array_X, array_Y)

Significance of correlation coefficient

cfa.corr_significance(array_X, array_Y)

Linear regression

Mean Absolute Deviation

cfa.mad(array) 

Semivariance

cfa.semivariance(array) 

Semideviation

cfa.semideviation(array) 

Target semivariance

cfa.target_semivariance(array, target)

Target Semideviation

cfa.target_semideviation(array, target) 

Coefficient of variation

cfa.cv(array) 

Sharpe Ratio

cfa.sharpe(array_portfolio, array_rf)

Sample Skewness

cfa.sample_skewness(array)

Sample Kurtosis

cfa.sample_kurtosis(array)

Probabililty

Covariance Matrix - takes array of arrays

cfa.covariance_matrix(array)

Correlation Matrix - takes array of arrays

cfa.correlation_matrix(array)

Derivatives

Call option - valuation of a call option using Black Scholes formula

cfa.callOption(vol, price, strike, time, rate, dividend)

// returns
// {d1, d2, Nd1, Nd2, callValue }

HELPER FUNCTIONS

Factorial

cfa.factorial(num)

cfa.factorial(3)

// 6

cfa.factorial(5)

// 120

__Yield__

```javascript
cfa.yield(a,b)

Average

cfa.average(array)

Array parse to float

cfa.float_array(array)

Sum of array

cfa.sum(array)

Array - sort descending

cfa.sort_desc(array)

Array - Extent

cfa.extent(array)

MAX

cfa.max(array)

MIN

cfa.min(array)

Range

cfa.range(array)

k days yield array

cfa.yield_array(array, days)

Rolling function with callback

cfa.rolling(array, days, callback)

Package Sidebar

Install

npm i cfs.js

Weekly Downloads

0

Version

0.0.8

License

MIT

Unpacked Size

19.3 kB

Total Files

3

Last publish

Collaborators

  • lukaszwos