From 9da4cbd49902ff4a32fabaa9b3560d02b056fb55 Mon Sep 17 00:00:00 2001 From: Shraddha Singh Date: Thu, 13 Jul 2023 16:11:00 +0530 Subject: [PATCH 1/3] Policy premium endpoint is added --- DOCUMENTATION.md | 7 +++++ ENDPOINTS.md | 18 +++++++++++++ helpers/functions.py | 17 ++++++++++++ main.py | 18 ++++++++++++- tasks/PolicyPremium.py | 45 ++++++++++++++++++++++++++++++++ validators/request_validators.py | 9 +++++++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tasks/PolicyPremium.py diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index f0719c3d..71775848 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -50,5 +50,12 @@ | | | - `cost_of_goods_sold` (float): The total cost related to producing goods sold by a business. | | | | - `net_credit_sales` (float): Sales where the cash is collected at a later date. | |----------------------------|----------------------------------------|----------------------------------------------------------------------| +| GET /policy_premium | Calculate Policy Premium | - `policy_type` (str): The type of insurance policy. | +| | | - `age` (int): The age of the policyholder. | +| | | - `coverage_amount` (int): The desired coverage amount for the policy. | +| | | - `deductible` (int): The deductible amount for the policy.| +| | | - `num_claims` (int): The number of claims made by the policyholder. | +| | | - `num_accidents` (int): The number of accidents the policyholder has been involved in. | +|----------------------------|----------------------------------------|----------------------------------------------------------------------| diff --git a/ENDPOINTS.md b/ENDPOINTS.md index 4cdd7567..ce583036 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -2203,4 +2203,22 @@ Sample Output "Days of Payables Outstanding": 103.42, "Cash Conversion Cycle": 90.64 days", } +``` + +**POST** `/policy-premium` + +- Request body : `{ + "policy_type": "auto", + "age": 35, + "coverage_amount": 250000, + "deductible": 500, + "num_claims": 0, + "num_accidents": 1 +}` +- Sample output + +```py +{ + "premium_amount": 1200.50 +} ``` \ No newline at end of file diff --git a/helpers/functions.py b/helpers/functions.py index a310dd63..e75fa505 100644 --- a/helpers/functions.py +++ b/helpers/functions.py @@ -2021,3 +2021,20 @@ def cash_conversion_cycle(beginning_inventory: float, ending_inventory: float, b days_of_payables_outstanding = (average_payable / cost_of_goods_sold / 365) ccc = days_of_inventory_outstanding + days_of_sales_outstanding - days_of_payables_outstanding return ccc + +# Function to Calculate Policy Premium. + +def calculate_policy_premium_ratios(premiums_collected, claims_paid, commissions_paid, operating_expenses): + loss_ratio = (claims_paid / premiums_collected) * 100 + expense_ratio = ((commissions_paid + operating_expenses) / premiums_collected) * 100 + combined_ratio = loss_ratio + expense_ratio + profit_margin = 100 - combined_ratio + + ratios = { + "loss_ratio": loss_ratio, + "expense_ratio": expense_ratio, + "combined_ratio": combined_ratio, + "profit_margin": profit_margin + } + + return ratios diff --git a/main.py b/main.py index 95181d4d..fc905b90 100644 --- a/main.py +++ b/main.py @@ -133,8 +133,9 @@ from tasks.defensive_interval_ratio import defensive_interval_ratio_task from tasks.RateofReturn import calculate_rate_of_return from tasks.cash_conversion_cycle import cash_conversion_cycle_task -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio +from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PolicyPremium from tasks.financialAssestRatio import financial_assest_ratio +from tasks.PolicyPremium import calculate_policy_premium # Creating the app app = FastAPI( @@ -1922,3 +1923,18 @@ def cash_conversion_cycle(request: CashConversionCycle): return cash_conversion_cycle_task(request.beginning_inventory , request.ending_inventory , request.beginning_receivables, request.ending_receivables , request.beginning_payable, request.ending_payable , request.net_credit_sales , request.cost_of_goods_sold) + +# Endpoint to calculate Policy Premium + +@app.post( + "/policy_premium", + tags=["policy_premium"], + description="Calculate Policy premium", +) +def policy_premium(request: PolicyPremium): + return policy_premium(request.policy_type, + request.age, + request.coverage_amount, + request.deductible, + request.num_claims, + request.num_accidents) \ No newline at end of file diff --git a/tasks/PolicyPremium.py b/tasks/PolicyPremium.py new file mode 100644 index 00000000..f5214559 --- /dev/null +++ b/tasks/PolicyPremium.py @@ -0,0 +1,45 @@ +from helpers import functions +from fastapi import HTTPException, status + +def calculate_policy_premium(policy_type: str, age: int, coverage_amount: int, deductible: int, num_claims: int, num_accidents: int): + try: + policy_factors = { + "auto": { + "age": { + "18-25": 1.5, + "26-40": 1.2, + "41-60": 1.0, + "61+": 1.3 + }, + "claims": { + "0": 1.0, + "1-3": 1.2, + "4+": 1.5 + }, + "accidents": { + "0": 1.0, + "1-2": 1.2, + "3+": 1.5 + } + }, + } + + if policy_type not in policy_factors: + return None + + factors = policy_factors[policy_type] + + base_premium = coverage_amount * 0.01 + age_multiplier = factors["age"].get(age, 1.0) + claims_multiplier = factors["claims"].get(num_claims, 1.0) + accidents_multiplier = factors["accidents"].get(num_accidents, 1.0) + deductible_factor = 1 - (deductible / coverage_amount) + + premium_amount = base_premium * age_multiplier * claims_multiplier * accidents_multiplier * deductible_factor + + return { + "Tag": "Debt Service Coverage Ratio", + "Premium Amount": premium_amount, + } + except: + return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) \ No newline at end of file diff --git a/validators/request_validators.py b/validators/request_validators.py index 29d2b423..62c9997c 100644 --- a/validators/request_validators.py +++ b/validators/request_validators.py @@ -628,3 +628,12 @@ class CashConversionCycle(BaseModel): ending_payable: float cost_of_goods_sold: float net_credit_sales: float + +class PolicyPremium(BaseModel): + policy_type: str + age: int + coverage_amount: int + deductible: int + num_claims: int + num_accidents: int + From 77b76e635397e2c5b87767e60437b18541d84ee0 Mon Sep 17 00:00:00 2001 From: Subho Ghosh <93722719+ighoshsubho@users.noreply.github.com> Date: Fri, 14 Jul 2023 13:37:02 +0530 Subject: [PATCH 2/3] Update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 7bd8302e..dff19afa 100644 --- a/main.py +++ b/main.py @@ -1935,7 +1935,7 @@ def cash_conversion_cycle(request: CashConversionCycle): description="Calculate Policy premium", ) def policy_premium(request: PolicyPremium): - return policy_premium(request.policy_type, + return calculate_policy_premium(request.policy_type, request.age, request.coverage_amount, request.deductible, From 8ed34591d5dbd0af08680146c6d4add7f9c9b615 Mon Sep 17 00:00:00 2001 From: Subho Ghosh <93722719+ighoshsubho@users.noreply.github.com> Date: Fri, 14 Jul 2023 13:38:53 +0530 Subject: [PATCH 3/3] Update main.py --- main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.py b/main.py index dff19afa..da264914 100644 --- a/main.py +++ b/main.py @@ -133,10 +133,9 @@ from tasks.defensive_interval_ratio import defensive_interval_ratio_task from tasks.RateofReturn import calculate_rate_of_return from tasks.cash_conversion_cycle import cash_conversion_cycle_task -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PolicyPremium from tasks.financialAssestRatio import financial_assest_ratio from tasks.PolicyPremium import calculate_policy_premium -from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity +from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity, PolicyPremium from tasks.financialAssestRatio import financial_assest_ratio from tasks.PriceElasticity import calculate_price_elasticity