{"id":20603695,"url":"https://github.com/patrickm663/claim-prediction-in-julia","last_synced_at":"2025-06-30T12:35:25.119Z","repository":{"id":45481523,"uuid":"513680680","full_name":"patrickm663/claim-prediction-in-julia","owner":"patrickm663","description":"A simple example applying Julia's Flux libary to an auto-insurance dataset","archived":false,"fork":false,"pushed_at":"2022-11-05T18:59:44.000Z","size":1437,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-06T16:48:47.545Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrickm663.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-13T21:49:24.000Z","updated_at":"2023-04-09T17:41:42.000Z","dependencies_parsed_at":"2022-07-15T06:30:54.530Z","dependency_job_id":null,"html_url":"https://github.com/patrickm663/claim-prediction-in-julia","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/patrickm663/claim-prediction-in-julia","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickm663%2Fclaim-prediction-in-julia","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickm663%2Fclaim-prediction-in-julia/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickm663%2Fclaim-prediction-in-julia/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickm663%2Fclaim-prediction-in-julia/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickm663","download_url":"https://codeload.github.com/patrickm663/claim-prediction-in-julia/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickm663%2Fclaim-prediction-in-julia/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262774227,"owners_count":23362273,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T09:18:10.934Z","updated_at":"2025-06-30T12:35:25.087Z","avatar_url":"https://github.com/patrickm663.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# claim-prediction-in-julia\nA simple example applying Julia's Flux.jl libary to an auto-insurance dataset.\n\n## Purpose\nThis example (and more to come) aims to see how an artifical neural network fares in a claim prediction task. Most Flux examples seem to either show very simple examples (like predicting the function f(x) = 4x + 2) or focus on image recognition tasks (like the well-known MINST) and then start focussing on more complex neural network structures. Through this example, we hope to show a middle-ground and introduce users to other interesting datasets after they have exhausted 'Boston' et. al. :)\n\n## Overview of the Data\nThe choice of data is a collection of 67'856 motor vehicle insurance policies recorded by an Australian short-term insurer in 2004. The dataset comprises of 4'624 policyholders that had at least one claim, making it fairly unbalanced with a target class making up just under 7% of the dataset.\n\nThe dataset is found in R's 'CASDatasets' package, titled 'ausprivauto0405' (source: P. De Jong and G.Z. Heller (2008), Generalized linear models for insurance data, Cambridge University Press. _Retrieved from 'CASDatasets' version 1.0-11_). \n\nThe dataset consist of 9 columns comprising of:\n- **Exposure:** Number of policy years\n- **VehValue:** Vehicle value in 000s of AUD\n- **VehAge:** Age group of the vehicle\n- **VehBody:** Vehicle body group (e.g. hatchback, sedan)\n- **Gender:** Gender of the policyholder\n- **DrivAge:** Age of the policyholder\n- **ClaimOcc:** A binary indicator of whether or not a claim has occured (_target variable_)\n- **ClaimNb:** Number of claims that occured per policyholder in the year (max = 4) (_excluded as features_)\n- **ClaimAmount:** Sum of claim payments in AUD (_excluded as features_)\n\nWhen dummy encoded (dropping one feature per class as a baseline), we get 23 features feeding into our model. We use a low-variance filter to remove zero/low variance features.\n\nThe Yeo Johnson transformation for my repo https://github.com/patrickm663/YeoJohnson.jl is used to transform the features to more closely represent a normal distribution. \n\nSMOTE from my fork of ClassImbalance.jl (available at https://github.com/patrickm663/ClassImbalance.jl) is used to address class imbalance, resulting in better results. \n\nThe train/test split is 70-30 using partitioned random sampling to maintain a similar distribution of the target variable. The testing set does not have SMOTE applie The testing set does not have SMOTE applied.\n\n## Model Architecture\nThe ANN comprises of 23 input neurons, two hidden layer with 40 neurons and 15 neurons, respectively. Both have tanh() activation function, and a single output neuron with a sigmoid activation function. Adam is used as its optimiser using default parameters.\n\n## Results\nOn 1'000 epochs, the model achieves an MSE of 0.203:\n\n![MSE vs Epochs on Training Data](./images/training_graph.png)\n\nOn testing data, the model achieves an F1-score of 92.4% and accuracy of 86%:\n\n```julia\nmetrics(CM) = (Accuracy = 0.8603920027508964, Precision = 0.9349289048084228, Recall = 0.91293057763646, F1_Score = 0.9237987987987988)\n\n2×2 Matrix{Float64}:\n 17227.0  1643.0\n  1199.0   288.0\n```\n\nThe model however struggles to predict the minority class \"at least one claim\".\n\n**The Notebook available is a work in progress and results may vary.**\n\n## Custom Functions\nAs there were limited out-of-of the box solutions that support some of the tasks I want to use, `src/utils.jl` contains some data processing functions such as dummy variable encoding over an entire DataFrame, a low variance filter, and some confusion matrix helpers and metrics.\n\n## Docker\nThe Dockerfile provided creates a Dockerised Jupyter Lab at port 8888 with IJulia and the required packages installed.\n\n```\ndocker build -t claim-prediction-in-julia .\ndocker run -ti -p 8888:8888 claim-prediction-in-julia\n```\nFollow the link in the console output (https://127.0.0.1:8888/lab?token....).\n\n## TODO\n- [ ] The loss can be reduced further by making modifications to the model's architecture and/or parameters in the optimiser. In addition, further feature engineering will likely improve results too.\n\n- [ ] GPU training is currently in progress using a custom SageMaker notebook (since Sagemaker does not support Julia out of the box, Julia and IJulia needs to be installed via Jupyter's terminal).\n\n- [ ] Different ML models are being tested and compared to the neural network.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickm663%2Fclaim-prediction-in-julia","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickm663%2Fclaim-prediction-in-julia","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickm663%2Fclaim-prediction-in-julia/lists"}