function sharpe
sharpe(
x: array,
frisk?: number,
dim?: 0 | 1,
): number

Computes the Sharpe Ratio.

Calculates the Sharpe Ratio for an asset or portfolio given its returns and a risk-free rate. The risk-free rate should be adjusted to match the timeframe of the returns.

The Sharpe Ratio formula: SR = (Mean Return - Risk-Free Rate) / Standard Deviation

Examples

Sharpe Ratio for a single asset

import { assertEquals } from "jsr:@std/assert";

const x = [0.003, 0.026, 0.015, -0.009, 0.014, 0.024, 0.015, 0.066, -0.014, 0.039];
assertEquals(sharpe(x, 0.02 / 12), 0.6987943426529188);

Sharpe Ratio with zero risk-free rate

import { assertEquals } from "jsr:@std/assert";

const x = [0.003, 0.026, 0.015, -0.009, 0.014, 0.024, 0.015, 0.066, -0.014, 0.039];

assertEquals(sharpe(x), 0.7705391416932597);

Sharpe Ratio for different asset

import { assertEquals } from "jsr:@std/assert";

const y = [-0.005, 0.081, 0.04, -0.037, -0.061, 0.058, -0.049, -0.021, 0.062, 0.058];
assertEquals(sharpe(y), 0.23858007842472628);

Parameters

Asset/portfolio returns

optional
frisk: number

Annual risk-free rate (adjusted for the timeframe) (defaults to 0)

optional
dim: 0 | 1

Dimension to operate on (0: row-wise, 1: column-wise) (defaults to 0)

Return Type

number

The computed Sharpe Ratio

Throws

If the input is invalid

sharpe(
x: matrix,
frisk?: number,
dim?: 0 | 1,
): array | matrix

Computes the Sharpe Ratio.

Calculates the Sharpe Ratio for an asset or portfolio given its returns and a risk-free rate. The risk-free rate should be adjusted to match the timeframe of the returns.

The Sharpe Ratio formula: SR = (Mean Return - Risk-Free Rate) / Standard Deviation

Examples

Sharpe Ratio for a single asset

import { assertEquals } from "jsr:@std/assert";

const x = [0.003, 0.026, 0.015, -0.009, 0.014, 0.024, 0.015, 0.066, -0.014, 0.039];
assertEquals(sharpe(x, 0.02 / 12), 0.6987943426529188);

Sharpe Ratio with zero risk-free rate

import { assertEquals } from "jsr:@std/assert";

const x = [0.003, 0.026, 0.015, -0.009, 0.014, 0.024, 0.015, 0.066, -0.014, 0.039];

assertEquals(sharpe(x), 0.7705391416932597);

Sharpe Ratio for different asset

import { assertEquals } from "jsr:@std/assert";

const y = [-0.005, 0.081, 0.04, -0.037, -0.061, 0.058, -0.049, -0.021, 0.062, 0.058];
assertEquals(sharpe(y), 0.23858007842472628);

Parameters

Asset/portfolio returns

optional
frisk: number

Annual risk-free rate (adjusted for the timeframe) (defaults to 0)

optional
dim: 0 | 1

Dimension to operate on (0: row-wise, 1: column-wise) (defaults to 0)

Return Type

The computed Sharpe Ratio

Throws

If the input is invalid