Sub topic 12
Fitting a Curve to Existing Content
This chapter assumes you know the power-law cost function cost = scale * power ** exponent. It also assumes you can compute a power score for a unit from earlier chapters. Here you will work backwards. You start from content that already exists and recover the curve that best describes how it was priced.
Why fit a curve instead of choosing one
A new game can pick an exponent on design grounds. Most projects don't have that option. They have a catalog of units that shipped, were patched, and were tuned by feel. Fitting answers three practical questions:
- What exponent does the current catalog follow? An exponent near 1.0 means designers priced roughly linearly. A value of 1.5 or more means strong cards are being taxed heavily.
- What should a new unit cost, given its power score?
- Which existing units sit far from the trend and deserve a second look?
The fit does not say what the curve should be. It describes what you did. Whether the result is good is a separate question, covered later in the tutorial under validation.
The model and why we use logarithms
The power-law model is:
cost = scale * power ** exponentThis is not a straight line, so ordinary line fitting does not apply directly. Taking the natural logarithm of both sides gives:
log(cost) = log(scale) + exponent * log(power)This is a straight line. The slope is the exponent, and the intercept is log(scale). A least-squares line through the points (log(power), log(cost)) gives both parameters.
Fitting in log space has a useful side effect. Least squares minimizes squared errors, and in log space an error is a percentage error. A 6-point miss on a 10-cost unit counts about as much as a 60-point miss on a 100-cost unit. That matches how designers think: they care about being 20% off, not about being 20 points off.
A worked fit
The function below is the same least-squares fit as in the spine, plus two helpers. r_squared measures how well the line explains the data in log space. cost_ratio compares an actual cost to the cost the curve predicts.
import math
def fit_power_law(points):
# points is a list of (power, cost) pairs; fits cost = scale * power ** exponent
xs = [math.log(p) for p, _ in points]
ys = [math.log(c) for _, c in points]
n = len(points)
mean_x = sum(xs) / n
mean_y = sum(ys) / n
exponent = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys)) / sum((x - mean_x) ** 2 for x in xs)
scale = math.exp(mean_y - exponent * mean_x)
return scale, exponent
def r_squared(points, scale, exponent):
ys = [math.log(c) for _, c in points]
mean_y = sum(ys) / len(ys)
ss_total = sum((y - mean_y) ** 2 for y in ys)
ss_resid = sum((math.log(c) - math.log(scale * p ** exponent)) ** 2 for p, c in points)
return 1 - ss_resid / ss_total
def cost_ratio(power, cost, scale, exponent):
# Above 1.0 means overpriced for its power; below 1.0 means underpriced.
return cost / (scale * power ** exponent)
core_units = [(4, 16), (9, 54), (16, 128), (25, 250)]
scale, exponent = fit_power_law(core_units)
print(round(scale, 2), round(exponent, 2)) # 2.0 1.5
print(round(r_squared(core_units, scale, exponent), 3)) # 1.0
# A new unit with power 20 that the designers priced at 100.
print(round(cost_ratio(20, 100, scale, exponent), 2)) # 0.56The four core units were generated from cost = 2 * power ** 1.5, so the fit recovers a scale of 2.0 and an exponent of 1.5 with a perfect R². Real data is never this clean. The last line shows the main use of a fit. A power-20 unit should cost about 179 on this curve, so a price of 100 gives a ratio of 0.56. That unit is priced at roughly half of what the catalog trend implies.
Reading the result
| Output | What it tells you | What to watch for |
|---|---|---|
| Exponent | The shape of the curve. Near 1.0 is linear, above 1.0 is superlinear, below 1.0 is diminishing returns. | A single exponent assumes one shape across the whole range. |
| Scale | The cost of a unit with power 1. | It is only meaningful together with the exponent. Compare fits by predicted costs, not by scale alone. |
| R² (log space) | How much of the variation in log cost the line explains. | A high value does not mean the pricing is good, only that it is consistent. |
| Cost ratio per unit | How far each unit sits from the trend. | Large ratios are leads to investigate, not automatic bugs. |
Exponent and scale trade off against each other. Two fits on slightly different data can report quite different scales and still predict similar costs across the range you care about. Check predictions before comparing parameters.
A fitting workflow
The loop between steps D and G is deliberate. You usually fit twice: once to find the outliers, and once more without the ones you have decided are exceptions.
Common errors
Letting outliers drag the curve
Least squares gives every point a vote, and a badly priced unit pulls the line toward itself. Fit once, list the units with the largest cost ratios, and decide for each one whether it is a mistake or a deliberate exception. A promotional unit or a hero that is meant to be strong is not evidence about how normal units are priced.
Fit on the trusted core, then score everything against that line, as the worked example did. This is better than fitting everything and hoping the noise cancels out.
Circular reasoning
The power score comes from your stat values, and the fit then tells you how cost depends on that score. If you tuned the stat values to make costs look consistent, the fit will report a high R² that proves nothing. Fix the stat values from design intent first, and only then fit.
A narrow power range
If every unit in your data has power between 18 and 24, the line can tilt almost any way and still match. The exponent estimate is then unreliable. Check the range of your data before trusting the exponent. A ratio of at least 5 between the smallest and largest power is a reasonable rule of thumb. This is a heuristic, not a statistical guarantee.
Too few points
With three or four points, one unit changes the answer a lot. A simple check is leave-one-out. Refit once per unit with that unit removed, and see how much the exponent moves. If removing a single unit changes the exponent by 0.2, the estimate is fragile.
Zero and negative values
Logarithms are undefined for zero and negative numbers. A free unit with cost 0, or an item whose drawbacks push its power score to zero or below, will make math.log raise an error. Filter those out before fitting. Free units are special cases anyway, and a net-negative item is probably a drawback-heavy design that belongs outside the curve.
Mixing tiers or categories
A single curve across a whole catalog can hide the fact that two tiers follow different rules. If low-tier units are priced linearly and high-tier units superlinearly, the combined fit reports an exponent in between that describes neither. Plot log cost against log power. If the points bend instead of lying on a line, fit the groups separately. A bend also suggests the catalog follows a piecewise curve, which the chapter on tiers and progression covers.
Using the fitted curve
Once you trust the fit, there are three common uses.
- Pricing new content. Compute a power score, then
cost = scale * power ** exponent, and round to whatever granularity your game uses. - Auditing. Compute the cost ratio for every unit. Anything beyond your tolerance, such as 15% in either direction, goes on a review list.
- Comparing eras. Fit separate curves to the launch catalog and to the current one. A rising exponent shows that designers have been taxing strong content more over time.
Limits of fitting
A fit can only reproduce structure that is in the data. It cannot see synergies, counters, or context. A unit whose power score is modest but which combines strongly with another unit will look fairly priced on the curve and still break the game. Fitting also cannot tell you whether the exponent you inherited is a good one. That judgment comes from simulation and playtests, which the next chapter covers.
Exercise
A catalog has three units with (power, cost) pairs (10, 30), (20, 60), and (40, 120). What exponent and scale does fitpowerlaw return, and what does that tell you about the pricing?
Show answer
Doubling power doubles cost every time, so the exponent is 1.0 and the scale is 3.0 (cost = 3 * power). The catalog is priced linearly, with no extra tax on strong units.
Exercise
You fit your whole catalog and get an exponent of 1.25. Then you notice that one promotional unit has a cost ratio of 0.5 against that curve. Describe what you would do next and why.
Show answer
Decide whether the promotional unit is a deliberate exception. If it is, remove it and refit, because it pulls the line toward lower costs and may be lowering the exponent. If it is a mistake, correct its price. Either way, refit on the remaining trusted units and recompute the cost ratios, then check with leave-one-out that the new exponent is stable.
Exercise
Why does fitpowerlaw raise an error if one of the points has cost 0, and what is the sensible way to handle that unit?
Show answer
The function takes math.log of every cost, and the logarithm of zero is undefined. Exclude the free unit from the fit and treat it as a special case, then price it by a separate rule.