RiskPremium

Measuring the market risk premium
Log | Files | Refs

test_dataimport.jl (2020B)


      1 # test/test_dataimport.jl
      2 using Test, CSV, DataFrames, Dates, Statistics
      3 
      4 # Load R reference
      5 ref = CSV.read("output/predict.csv", DataFrame)
      6 println("Reference predict.csv: $(nrow(ref)) obs, dateym $(ref.dateym[1]) to $(ref.dateym[end])")
      7 
      8 include(joinpath(@__DIR__, "..", "src", "DataImport.jl"))
      9 using .DataImport
     10 
     11 # --- Test D/P ratio ---
     12 dp_df = DataImport.compute_dp("output/msi.csv")
     13 # Merge on dateym
     14 comp = innerjoin(
     15     select(ref, :dateym, :dp => :dp_ref),
     16     dp_df,
     17     on=:dateym
     18 )
     19 maxerr = maximum(abs.(comp.dp .- comp.dp_ref))
     20 println("D/P ratio max absolute error: $maxerr")
     21 @test maxerr < 1e-10
     22 println("D/P ratio matches R output")
     23 
     24 # --- Test T-bill ---
     25 tbill_df = DataImport.compute_tbill()
     26 comp_rf = innerjoin(
     27     select(ref, :dateym, :rf => :rf_ref),
     28     tbill_df,
     29     on=:dateym
     30 )
     31 maxerr_rf = maximum(abs.(comp_rf.rf .- comp_rf.rf_ref))
     32 println("T-bill max absolute error: $maxerr_rf")
     33 @test maxerr_rf < 1e-6
     34 println("✓ T-bill matches R output")
     35 
     36 # --- Test future excess returns ---
     37 rmrf_df = DataImport.compute_excess_returns("output/msi.csv", tbill_df)
     38 comp_rmrf = innerjoin(
     39     select(ref, :dateym, :rmrf_y3 => :rmrf_ref),
     40     rmrf_df,
     41     on=:dateym
     42 )
     43 dropmissing!(comp_rmrf)
     44 maxerr_rmrf = maximum(abs.(comp_rmrf.rmrf_y3 .- comp_rmrf.rmrf_ref))
     45 println("Excess return max absolute error: $maxerr_rmrf")
     46 @test maxerr_rmrf < 1e-10
     47 println("✓ Future excess returns match R output")
     48 
     49 # --- Test full merge ---
     50 predict = DataImport.build_predictors("output/msi.csv", "input/cay_current.csv")
     51 println("Built predict: $(nrow(predict)) obs")
     52 
     53 # Compare against R reference (264 obs)
     54 comp_full = innerjoin(ref, predict, on=:dateym, makeunique=true)
     55 println("Matched $(nrow(comp_full)) obs (R has $(nrow(ref)))")
     56 @test nrow(comp_full) == nrow(ref)
     57 
     58 for col in [:dp, :rf, :rmrf_y3, :cay]
     59     local maxerr = maximum(abs.(comp_full[!, col] .- comp_full[!, Symbol(string(col, "_1"))]))
     60     println("$col max error: $maxerr")
     61     @test maxerr < 1e-6
     62 end
     63 println("✓ Full predictor merge validated")