Skip to content

Commit

Permalink
Squashing a couple of bugs in gain/lose table.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamstark committed Sep 12, 2023
1 parent 72bce00 commit 1bd70ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/STBOutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,21 +581,22 @@ const EXTRA_INC_COLS = 18
end

const GL_COLNAMES = [
"Lose over £10",
"Lose £1-9.99",
"Lose £10.01+",
"Lose £1.01-£10",
"No Change",
"Gain £1-9.99",
"Gain over £10"
"Gain £1.01-£10",
"Gain £10.01+"
]

function gl( v :: Number ) :: String
return if v < -10.0
function gl( vf :: Number ) :: String
v = round(vf; digits=2)
return if v <= -10.01
GL_COLNAMES[1]
elseif v < -1.0
elseif v <= -1.01
GL_COLNAMES[2]
elseif v > 10.0
elseif v >= 10.01
GL_COLNAMES[5]
elseif v > 1
elseif v >= 1.01
GL_COLNAMES[4]
else
GL_COLNAMES[3]
Expand Down Expand Up @@ -682,10 +683,12 @@ const EXTRA_INC_COLS = 18
ns = Symbol.(colnames)
select!( sort!(vhh, col), ns... )
# average change column - sum of weighted changes (since they're already divided by total popn)
avch = combine( groupby( dhh, [col]),(:weighted_change=>sum))
gavch = combine( groupby( dhh, [col]),(:weighted_change=>sum), (:weight=>sum)) # total change for each group
gavch.avch = gavch.weighted_change_sum ./ gavch.weight_sum # => average change for each group
# ... put av changes in the right order
sort!( avch, col )
vhh."Average Change(£s)" = avch[:,2]
sort!( gavch, col )
vhh."Average Change(£s)" = gavch.avch
# remove missing: Do we need this?
glf = coalesce.( vhh, 0.0)
# add an average change column
return glf
Expand Down Expand Up @@ -718,7 +721,7 @@ const EXTRA_INC_COLS = 18
in_poverty = prehh.in_poverty,
change = posthh[:, incomes_col] - prehh[:,incomes_col]
)
dhh.weighted_change = (dhh.change .* dhh.weight) ./ sum( dhh.weight ) # for average gains
dhh.weighted_change = (dhh.change .* dhh.weight) # for average gains
ten_gl = one_gain_lose( dhh, :tenure )
dec_gl = one_gain_lose( dhh, :decile )
children_gl = one_gain_lose( dhh, :num_children )
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ include( "output_tests.jl")
include( "vs_policy_in_practice_tests.jl")
include( "vs_age_uk_tests.jl")
include( "affordability_tests.jl")
include( "stboutput_tests.jl")


# These will only run if datasets are locally installed
if IS_LOCAL
Expand Down
15 changes: 15 additions & 0 deletions test/stboutput_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ScottishTaxBenefitModel
using .STBOutput
using DataFrames
using Test

@testset "basic gain lose tests" begin

d = DataFrame( weight=[200,300,200,100,100],i=[1,1,2,2,2],change=[10,2,4,5,3])
d.weighted_change = d.weight.*d.change
ogl = STBOutput.one_gain_lose( d, :i )

@test ogl."Average Change(£s)" [5.2,4.0]
@test sum( ogl."No Change") == 0
@test sum( ogl."Gain £1.01-£10" ) == sum(d.weight)
end

0 comments on commit 1bd70ae

Please sign in to comment.