Class: FinanceMath::Loan

Inherits:
Object
  • Object
show all
Defined in:
lib/finance_math/loan.rb

Overview

the Loan class provides an interface for working with interest rates.

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loan

create a new Loan instance

Examples:

create a 10.5% Nominal rate

Loan.new(10.5, 12, 1000)

Parameters:

  • value of the interest rate

  • of the loan period

  • amount

  • fee - fee for the market in percentages

  • protection - Protection for currency changes - usually 3%, default to 0%

See Also:

API:

  • public



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/finance_math/loan.rb', line 49

def initialize(options = {})
  @nominal_rate = options.fetch(:nominal_rate).to_f
  @duration = options.fetch(:duration).to_f
  @amount = options.fetch(:amount).to_f
  @structure_fee = options.fetch(:structure_fee, 5).to_f
  @currency_protection = options.fetch(:currency_protection, 3).to_f
  @fee = options.fetch(:fee, 0).to_f
  @nominal_rate, @amount, @duration, @structure_fee, @currency_protection, @fee = nominal_rate.to_f, amount, duration, structure_fee.to_f, currency_protection.to_f, fee.to_f
  @principal = principal_calculation
  @monthly_rate = @nominal_rate / 100 / 12
end

Instance Attribute Details

#amountFloat

Returns the amount of loan request.

Returns:

  • the amount of loan request

API:

  • public



11
12
13
# File 'lib/finance_math/loan.rb', line 11

def amount
  @amount
end

#currency_protectionDecNum (readonly)

Returns the currency protection.

Returns:

  • the currency protection

API:

  • public



23
24
25
# File 'lib/finance_math/loan.rb', line 23

def currency_protection
  @currency_protection
end

#durationInteger

Returns the duration for which the rate is valid, in months.

Returns:

  • the duration for which the rate is valid, in months

API:

  • public



7
8
9
# File 'lib/finance_math/loan.rb', line 7

def duration
  @duration
end

#feeFloat (readonly)

Returns fee.

Returns:

  • fee

API:

  • public



35
36
37
# File 'lib/finance_math/loan.rb', line 35

def fee
  @fee
end

#monthly_rateDecNum (readonly)

Returns the monthly rate.

Returns:

  • the monthly rate

API:

  • public



19
20
21
# File 'lib/finance_math/loan.rb', line 19

def monthly_rate
  @monthly_rate
end

#nominal_rateFloat

Returns the nominal annual rate.

Returns:

  • the nominal annual rate

API:

  • public



15
16
17
# File 'lib/finance_math/loan.rb', line 15

def nominal_rate
  @nominal_rate
end

#principalDecNum (readonly)

Returns P principal.

Returns:

  • P principal

API:

  • public



31
32
33
# File 'lib/finance_math/loan.rb', line 31

def principal
  @principal
end

#structure_feeDecNum (readonly)

Returns the fee for the bank/market.

Returns:

  • the fee for the bank/market

API:

  • public



27
28
29
# File 'lib/finance_math/loan.rb', line 27

def structure_fee
  @structure_fee
end

Instance Method Details

#aprObject

API:

  • public



67
68
69
# File 'lib/finance_math/loan.rb', line 67

def apr
  @apr ||= calculate_apr
end

#pmt(options = {}) ⇒ Object

API:

  • public



61
62
63
64
65
# File 'lib/finance_math/loan.rb', line 61

def pmt(options = {})
  future_value = options.fetch(:future_value, 0)
  type = options.fetch(:type, 0)
  ((@amount * interest(@monthly_rate, @duration) - future_value ) / ((1.0 + @monthly_rate * type) * fvifa(@monthly_rate, duration)))
end