Work in progress. Comments welcome.
!pip install git+https://github.com/Techtonique/mlreserving.git --verbose %load_ext rpy2.ipython %%R install.packages("reticulate") %%R library(reticulate) library(ggplot2) library(dplyr) # Import Python modules pd <- import("pandas") np <- import("numpy") plt <- import("matplotlib.pyplot") MLReserving <- import("mlreserving")$MLReserving RidgeCV <- import("sklearn.linear_model")$RidgeCV ExtraTreesRegressor <- import("sklearn.ensemble")$ExtraTreesRegressor RandomForestRegressor <- import("sklearn.ensemble")$RandomForestRegressor # Load the dataset url <- "https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/triangle/raa.csv" df <- pd$read_csv(url) # Print head and tail print(head(df)) print(tail(df)) library(reticulate) library(ggplot2) library(dplyr) # Import Python modules with convert=FALSE for more control pd <- import("pandas", convert=FALSE) np <- import("numpy", convert=FALSE) MLReserving <- import("mlreserving", convert=FALSE)$MLReserving RidgeCV <- import("sklearn.linear_model", convert=FALSE)$RidgeCV ExtraTreesRegressor <- import("sklearn.ensemble", convert=FALSE)$ExtraTreesRegressor RandomForestRegressor <- import("sklearn.ensemble", convert=FALSE)$RandomForestRegressor # Load the dataset and convert to Python object url <- "https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/triangle/raa.csv" py_df <- pd$read_csv(url) # Print head and tail print(py_df$head()) print(py_df$tail()) # Create models list models <- list( RidgeCV(), ExtraTreesRegressor(), RandomForestRegressor() ) # Helper function to create Python integers py_int <- function(x) { floor(x) } for (mdl in models) { # Initialize the model with explicit Python integers model <- MLReserving( model = mdl, level = py_int(95), # 95% confidence level random_state = py_int(42) ) # Fit the model model$fit( py_df, origin_col = "origin", development_col = "development", value_col = "values" ) # Uncomment to make predictions result <- model$predict() print(result) } (mean_df <- py_to_r(result$ibnr_mean)) (lower_df <- py_to_r(result$ibnr_lower)) (upper_df <- py_to_r(result$ibnr_upper)) mean_vals <- mean_df lower_vals <- lower_df upper_vals <- upper_df # Get origin years as numeric values years <- as.numeric(names(mean_vals)) # Set up plot plot(years, mean_vals, ylim = range(c(lower_vals, upper_vals)), xlab = "Origin Year", ylab = "IBNR Value", main = "IBNR Estimates with Confidence Interval", pch = 19, col = "blue", type = "n") # Add polygons and lines for (i in seq_along(years)) { # Coordinates for polygon: x and y pairs for upper and lower bounds x_poly <- c(years[i] - 0.2, years[i] + 0.2, years[i] + 0.2, years[i] - 0.2) y_poly <- c(lower_vals[i], lower_vals[i], upper_vals[i], upper_vals[i]) polygon(x_poly, y_poly, col = rgb(0, 0, 1, 0.2), border = NA) # Draw the mean point on top points(years[i], mean_vals[i], pch = 19, col = "blue") } # Optional: Add a legend legend("topleft", legend = c("IBNR Mean", "Confidence Interval"), col = c("blue", rgb(0, 0, 1, 0.2)), pch = c(19, 15), bty = "n") development origin values 1 1981 1981 5012 2 1982 1982 106 3 1983 1983 3410 4 1984 1984 5655 5 1985 1985 1092 6 1986 1986 1513 development origin values 50 1988 1981 18608 51 1989 1982 16169 52 1990 1983 23466 53 1989 1981 18662 54 1990 1982 16704 55 1990 1981 18834 development origin values 0 1981 1981 5012.00 1 1982 1982 106.00 2 1983 1983 3410.00 3 1984 1984 5655.00 4 1985 1985 1092.00 development origin values 50 1989 1982 16169.00 51 1990 1983 23466.00 52 1989 1981 18662.00 53 1990 1982 16704.00 54 1990 1981 18834.00 DescribeResult(mean=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 22908.59 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 21823.11 24108.16 1986 1513.00 6445.00 11702.00 12935.00 15852.00 20182.39 22964.19 25369.88 1987 557.00 4020.00 10946.00 12314.00 17925.30 21236.16 24164.31 26696.94 1988 1351.00 6947.00 13112.00 15015.00 18859.87 22344.37 25426.49 28092.70 1989 3133.00 5395.00 11465.39 15796.71 19842.66 23509.80 26753.91 29560.66 1990 2063.00 7394.65 12061.46 16618.70 20876.12 24735.39 28149.89 31104.52 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 23699.40 1983 23509.79 24943.97 1984 24742.62 26253.21 1985 26039.42 27630.45 1986 27403.47 29079.18 1987 28838.23 30603.08 1988 30347.32 32206.00 1989 31934.55 33891.99 1990 33603.93 35665.33 , lower=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 8974.39 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 8549.13 9444.35 1986 1513.00 6445.00 11702.00 12935.00 15852.00 7906.33 8996.17 9938.66 1987 557.00 4020.00 10946.00 12314.00 7022.06 8319.17 9466.35 10458.57 1988 1351.00 6947.00 13112.00 5881.88 7388.20 8753.34 9960.84 11005.39 1989 3133.00 5395.00 4491.24 6188.14 7773.24 9209.93 10480.88 11580.50 1990 2063.00 2896.43 4724.76 6510.17 8178.12 9690.08 11027.79 12185.34 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 9284.21 1983 9209.92 9771.80 1984 9692.91 10284.72 1985 10200.96 10824.29 1986 10735.37 11391.87 1987 11297.47 11988.89 1988 11888.69 12616.87 1989 12510.53 13277.40 1990 13164.55 13972.15 , upper=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 58475.51 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 55704.84 61537.39 1986 1513.00 6445.00 11702.00 12935.00 15852.00 51516.90 58617.43 64757.93 1987 557.00 4020.00 10946.00 12314.00 45755.71 54206.64 61680.72 68145.25 1988 1351.00 6947.00 13112.00 38327.19 48141.20 57035.33 64902.42 71707.89 1989 3133.00 5395.00 29266.86 40322.51 50649.76 60010.10 68290.64 75454.84 1990 2063.00 18876.34 30788.30 42420.62 53287.66 63138.39 71853.88 79395.53 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 60494.05 1983 60010.06 63670.80 1984 63156.85 67012.63 1985 66466.91 70528.02 1986 69948.64 74225.89 1987 73610.85 78115.62 1988 77462.79 82207.05 1989 81514.19 86510.54 1990 85775.26 91036.96 , ibnr_mean=origin 1982 23699.40 1983 48453.76 1984 73904.42 1985 99601.14 1986 124999.11 1987 149464.02 1988 172291.74 1989 192755.68 1990 210209.98 Name: values, dtype: float64, ibnr_lower=origin 1982 9284.21 1983 18981.72 1984 28952.02 1985 39018.73 1986 48968.39 1987 58552.50 1988 67495.21 1989 75511.84 1990 82349.38 Name: values, dtype: float64, ibnr_upper=origin 1982 60494.05 1983 123680.86 1984 188644.98 1985 254237.16 1986 319066.80 1987 381514.79 1988 439783.88 1989 492019.43 1990 536572.94 Name: values, dtype: float64) DescribeResult(mean=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 23466.00 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 23192.71 23466.00 1986 1513.00 6445.00 11702.00 12935.00 15852.00 22839.00 23192.71 23466.00 1987 557.00 4020.00 10946.00 12314.00 21412.04 22839.00 23192.71 23466.00 1988 1351.00 6947.00 13112.00 20144.66 21412.04 22839.00 23192.71 23466.00 1989 3133.00 5395.00 18855.41 20144.66 21412.04 22839.00 23192.71 23466.00 1990 2063.00 14144.04 18855.41 20144.66 21412.04 22839.00 23192.71 23466.00 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 16824.72 1983 21775.25 21906.31 1984 21775.25 21906.31 1985 21775.25 21906.31 1986 21775.25 21906.31 1987 21775.25 21906.31 1988 21775.25 21906.31 1989 21775.25 21906.31 1990 21775.25 21906.31 , lower=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 5439.95 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 5376.58 5439.95 1986 1513.00 6445.00 11702.00 12935.00 15852.00 5294.57 5376.58 5439.95 1987 557.00 4020.00 10946.00 12314.00 4963.73 5294.57 5376.58 5439.95 1988 1351.00 6947.00 13112.00 4669.88 4963.73 5294.57 5376.58 5439.95 1989 3133.00 5395.00 4370.96 4669.88 4963.73 5294.57 5376.58 5439.95 1990 2063.00 3278.60 4370.96 4669.88 4963.73 5294.57 5376.58 5439.95 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 3900.13 1983 5047.94 5078.33 1984 5047.94 5078.33 1985 5047.94 5078.33 1986 5047.94 5078.33 1987 5047.94 5078.33 1988 5047.94 5078.33 1989 5047.94 5078.33 1990 5047.94 5078.33 , upper=dev 1 2 3 4 5 6 7 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 100034.29 1986 1513.00 6445.00 11702.00 12935.00 15852.00 98508.72 100034.29 1987 557.00 4020.00 10946.00 12314.00 92354.22 98508.72 100034.29 1988 1351.00 6947.00 13112.00 86887.95 92354.22 98508.72 100034.29 1989 3133.00 5395.00 81327.36 86887.95 92354.22 98508.72 100034.29 1990 2063.00 61007.06 81327.36 86887.95 92354.22 98508.72 100034.29 dev 8 9 10 origin 1981 18608.00 18662.00 18834.00 1982 16169.00 16704.00 72568.93 1983 23466.00 93920.76 94486.02 1984 101213.01 93920.76 94486.02 1985 101213.01 93920.76 94486.02 1986 101213.01 93920.76 94486.02 1987 101213.01 93920.76 94486.02 1988 101213.01 93920.76 94486.02 1989 101213.01 93920.76 94486.02 1990 101213.01 93920.76 94486.02 , ibnr_mean=origin 1982 16824.72 1983 43681.56 1984 67147.56 1985 90340.27 1986 113179.27 1987 134591.31 1988 154735.97 1989 173591.37 1990 187735.41 Name: values, dtype: float64, ibnr_lower=origin 1982 3900.13 1983 10126.26 1984 15566.21 1985 20942.79 1986 26237.37 1987 31201.09 1988 35870.97 1989 40241.93 1990 43520.53 Name: values, dtype: float64, ibnr_upper=origin 1982 72568.93 1983 188406.78 1984 289619.79 1985 389654.08 1986 488162.80 1987 580517.03 1988 667404.98 1989 748732.34 1990 809739.40 Name: values, dtype: float64) DescribeResult(mean=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 22799.06 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 22560.59 22799.06 1986 1513.00 6445.00 11702.00 12935.00 15852.00 22227.42 22560.59 22799.06 1987 557.00 4020.00 10946.00 12314.00 20320.41 22227.42 22560.59 22799.06 1988 1351.00 6947.00 13112.00 19568.73 20320.41 22227.42 22560.59 22799.06 1989 3133.00 5395.00 16013.73 19568.73 20320.41 22227.42 22560.59 22799.06 1990 2063.00 13812.49 16013.73 19568.73 20320.41 22227.42 22560.59 22799.06 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 17280.71 1983 22747.00 22747.00 1984 22747.00 22747.00 1985 22747.00 22747.00 1986 22747.00 22747.00 1987 22747.00 22747.00 1988 22747.00 22747.00 1989 22747.00 22747.00 1990 22747.00 22747.00 , lower=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 6829.01 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 6757.57 6829.01 1986 1513.00 6445.00 11702.00 12935.00 15852.00 6657.77 6757.57 6829.01 1987 557.00 4020.00 10946.00 12314.00 6086.50 6657.77 6757.57 6829.01 1988 1351.00 6947.00 13112.00 5861.33 6086.50 6657.77 6757.57 6829.01 1989 3133.00 5395.00 4796.39 5861.33 6086.50 6657.77 6757.57 6829.01 1990 2063.00 4136.98 4796.39 5861.33 6086.50 6657.77 6757.57 6829.01 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 5175.93 1983 6813.41 6813.41 1984 6813.41 6813.41 1985 6813.41 6813.41 1986 6813.41 6813.41 1987 6813.41 6813.41 1988 6813.41 6813.41 1989 6813.41 6813.41 1990 6813.41 6813.41 , upper=dev 1 2 3 4 5 6 7 8 \ origin 1981 5012.00 8269.00 10907.00 11805.00 13539.00 16181.00 18009.00 18608.00 1982 106.00 4285.00 5396.00 10666.00 13782.00 15599.00 15496.00 16169.00 1983 3410.00 8992.00 13873.00 16141.00 18735.00 22214.00 22863.00 23466.00 1984 5655.00 11555.00 15766.00 21266.00 23425.00 26083.00 27067.00 76110.60 1985 1092.00 9565.00 15836.00 22169.00 25955.00 26180.00 75314.53 76110.60 1986 1513.00 6445.00 11702.00 12935.00 15852.00 74202.33 75314.53 76110.60 1987 557.00 4020.00 10946.00 12314.00 67836.33 74202.33 75314.53 76110.60 1988 1351.00 6947.00 13112.00 65327.03 67836.33 74202.33 75314.53 76110.60 1989 3133.00 5395.00 53459.67 65327.03 67836.33 74202.33 75314.53 76110.60 1990 2063.00 46111.46 53459.67 65327.03 67836.33 74202.33 75314.53 76110.60 dev 9 10 origin 1981 18662.00 18834.00 1982 16704.00 57689.14 1983 75936.79 75936.79 1984 75936.79 75936.79 1985 75936.79 75936.79 1986 75936.79 75936.79 1987 75936.79 75936.79 1988 75936.79 75936.79 1989 75936.79 75936.79 1990 75936.79 75936.79 , ibnr_mean=origin 1982 17280.71 1983 45493.99 1984 68293.05 1985 90853.64 1986 113081.06 1987 133401.48 1988 152970.20 1989 168983.93 1990 182796.42 Name: values, dtype: float64, ibnr_lower=origin 1982 5175.93 1983 13626.82 1984 20455.83 1985 27213.40 1986 33871.17 1987 39957.67 1988 45818.99 1989 50615.38 1990 54752.36 Name: values, dtype: float64, ibnr_upper=origin 1982 57689.14 1983 151873.58 1984 227984.18 1985 303298.71 1986 377501.04 1987 445337.37 1988 510664.40 1989 564124.08 1990 610235.54 Name: values, dtype: float64)
Related