Module: Xirr::Depreciation

Defined in:
lib/xirr/depreciation.rb

Overview

Writing an asset down from its cost to its salvage value over its life. Depreciation.sln spreads the loss evenly; Depreciation.syd, Depreciation.ddb, and Depreciation.db are accelerated methods that depreciate more early on and return the amount for a single period (counting from 1).

Class Method Summary collapse

Class Method Details

.db(cost, salvage, life, period, month = 12) ⇒ Float

Fixed-declining-balance depreciation for a single period. Like ddb, but the rate is derived from cost, salvage, and life (rounded to three places, as spreadsheets do). month is how many months the asset was in service during its first year (default 12); a shorter first year spills the remainder into an extra final period.

Returns:

  • (Float)


48
49
50
51
52
53
54
55
56
# File 'lib/xirr/depreciation.rb', line 48

def db(cost, salvage, life, period, month = 12)
  n = period.to_i
  unless cost.positive? && salvage >= 0 && life.positive? && month >= 1 && month <= 12 &&
         period == n && n >= 1 && n <= life + 1
    raise ArgumentError, 'undefined'
  end

  fixed_declining(cost, salvage, life, n, month)
end

.ddb(cost, salvage, life, period, factor = 2) ⇒ Float

Double-declining-balance depreciation for a single period: each period takes factor/life of the remaining book value (never below salvage). factor defaults to 2 (the usual double-declining rate).

Returns:

  • (Float)


33
34
35
36
37
38
39
40
# File 'lib/xirr/depreciation.rb', line 33

def ddb(cost, salvage, life, period, factor = 2)
  n = period.to_i
  unless life.positive? && factor.positive? && period == n && n >= 1 && n <= life
    raise ArgumentError, 'undefined'
  end

  declining_balance(cost, salvage, factor.to_f / life, n)
end

.sln(cost, salvage, life) ⇒ Float

Straight-line depreciation: the equal amount the asset is written down by each period.

Returns:

  • (Float)

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'lib/xirr/depreciation.rb', line 14

def sln(cost, salvage, life)
  raise ArgumentError, 'life must be non-zero' if life.zero?

  (cost - salvage) / life * 1.0
end

.syd(cost, salvage, life, period) ⇒ Float

Sum-of-years'-digits depreciation for a single period — an accelerated method charging more in the early periods.

Returns:

  • (Float)

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/xirr/depreciation.rb', line 23

def syd(cost, salvage, life, period)
  raise ArgumentError, 'undefined' if life <= 0 || period < 1 || period > life

  (cost - salvage) * (life - period + 1) * 2.0 / (life * (life + 1))
end