> ## Documentation Index
> Fetch the complete documentation index at: https://docs.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Rate a study against a market

> Rate every version of a book with every program in a market, and compare each scenario with the baseline, program by program.

Rate a study with a market to see how each program prices each scenario. This page uses the
current and proposed programs from [Create a market](/relativities/how-to/create-a-market) and a
collision deductible scenario from [Create a study](/relativities/how-to/create-a-study):

```python theme={null}
from decimal import Decimal

from relativities import Market, Program, Study, scenario

current = Program.load("effective://example-ca-ppa")
book = current.example_book
rates = current.rater.tables["BaseRate"]
collision = rates["coverage"] == "collision"
rates.loc[collision, "rate"] = rates.loc[collision, "rate"] * Decimal("1.05")
market = Market({"current": current, "proposed": current.with_rater(current.rater.with_tables({"BaseRate": rates}))})


@scenario("Collision deductible {deductible}")
def collision_deductible(book, deductible=1_000):
    """Raise each collision deductible below `deductible` to `deductible`."""

    def raised(coverages):
        lower = coverages["coverage_id"].eq("collision") & coverages["deductible"].lt(deductible)
        return coverages.assign(deductible=coverages["deductible"].mask(lower, deductible))

    return book.with_tables(coverages=raised)


study = Study(book, scenarios=[collision_deductible])
results = market.rate(study)
results[["submission_id", "scenario_id", "draw_id", "program", "status", "premium"]].head(4)
#   submission_id scenario_id  draw_id   program     status premium
# 0       S000001    baseline        0   current  succeeded    2636
# 1       S000001    baseline        0  proposed  succeeded    2694
# 2       S000002    baseline        0   current  succeeded    5339
# 3       S000002    baseline        0  proposed  succeeded    5461
```

Each row is one submission, scenario, draw and program. A study with zero draws has `draw_id` 0 alone.

## Compare each scenario with the baseline

Pivot to one column per scenario, and take the difference for each program:

```python theme={null}
premiums = results.pivot(index=["submission_id", "program"], columns="scenario_id", values="premium").astype(float)
saving = premiums["baseline"] - premiums["Collision deductible 1000"]
saving.groupby("program").agg(["count", "mean"]).round(2)
#           count    mean
# program
# current   96671  156.74
# proposed  96671  164.58
```

The saving is each policy's annual premium at its reported deductible, less its premium at a
deductible of \$1,000. A policy with a deductible of \$1,000 or more saves \$0. The proposed program's higher
collision rate makes the deductible worth \$7.84 more on average.

## Compare the programs in each scenario

```python theme={null}
totals = premiums.dropna().groupby("program").sum()
(totals.loc["proposed"] / totals.loc["current"] - 1).round(4)
# scenario_id
# Collision deductible 1000    0.0165
# baseline                     0.0181
```

The proposed program raises the total by 1.81% at reported deductibles, and by 1.65% at \$1,000 deductibles.

## Count the unrated submissions

```python theme={null}
results.query("status != 'succeeded'").groupby(["reason_code", "scenario_id"]).size().unstack()
# scenario_id  Collision deductible 1000  baseline
# reason_code
# CSL                               2112      2112
# FR-FILING                         2516      2516
# INPUT-TYPE                         788       788
# RIDESHARE                         1242      1242
```

Each count covers both programs. A scenario can change a submission's status, such as a deductible the
rater refuses. Report these rows with their reasons.

```python theme={null}
market.close()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.