Functional Weave
Code in Rust

insurance.loss-ratio@1.0.1

impl/typescript.ts

1,176 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { type RoundingMode, roundDiv } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type Money, assertSameCurrency } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { type LossRatios } from "./insurance_loss_ratio_types.ts";

/**
 * Loss, expense and combined ratios against earned premium, in basis points.
 *
 * The combined ratio is worked from claims plus expenses, rounded once, not
 * by adding the two rounded ratios, so it can differ from their sum by one
 * basis point: 1.00 and 1.00 on 3.00 are 33.33% each but 66.67% together.
 */
export function lossRatios(earnedPremium: Money, incurredClaims: Money, expenses: Money, mode: RoundingMode): LossRatios {
  assertSameCurrency(earnedPremium, incurredClaims);
  assertSameCurrency(earnedPremium, expenses);
  if (earnedPremium.minor <= 0) {
    throw new RangeError(`earnedPremium must be more than zero, received ${earnedPremium.minor}`);
  }
  const p = earnedPremium.minor;
  return {
    lossRatioBasisPoints: roundDiv(incurredClaims.minor * 10000, p, mode),
    expenseRatioBasisPoints: roundDiv(expenses.minor * 10000, p, mode),
    combinedRatioBasisPoints: roundDiv((incurredClaims.minor + expenses.minor) * 10000, p, mode),
  };
}