Skip to content

Commit

Permalink
feat: add nominal apc
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed May 22, 2020
1 parent 6f813db commit 6b24e77
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/models/monetary_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class MonetaryData
VALID_INDICATORS = %w(nominal_apc).freeze
INITIAL_SUPPLY = 33.6
SECONDARY_SUPPLY_PER_YEAR = 1.344

def id
Time.current.to_i
end

def nominal_apc
initial_supply = 33.6
secondary_supply_per_year = 1.344
total_supplies_per_year.each_with_index.map do |_, index|
cumulative_total_supply = index.zero? ? 0 : (0..index).reduce(0) { |memo, value| memo + total_supplies_per_year[value] }
total_supply_so_far = initial_supply + cumulative_total_supply
(secondary_supply_per_year / total_supply_so_far * 100).truncate(2)
end
end

private

def total_supplies_per_year
@total_supplies_per_year ||=
begin
max_year = 20
secondary_supply_per_month = SECONDARY_SUPPLY_PER_YEAR / 12
total_supplies_per_year =
(0...max_year).map do |year|
primary_supply_per_year = 4.2/(2**(year / 4))
primary_supply_per_month = primary_supply_per_year / 12
[primary_supply_per_month + secondary_supply_per_month] * 12
end

total_supplies_per_year.flatten
end
end
end

0 comments on commit 6b24e77

Please sign in to comment.