Dynamic PricingClickHouse Workshops

04 Apply and measure

Approve a price and compare observed sales, stock, and contribution with unchanged pricing.

Your computer
macOS terminal: Run workshop commands in Terminal using zsh or bash.

Outcome

Approve your own price change. Watch subsequent synthetic sales and compare their observed revenue, units, remaining stock, and contribution with unchanged pricing.

Starting point

Complete Module 03. Keep the sales writer running.

How an approved price becomes sales

The forecast and the generated sales have different roles. ClickHouse SQL uses the prepared model's fitted coefficients to forecast price options. The sales generator uses a known synthetic response rule to produce transaction rows. It reuses the baseline demand captured at approval, but does not use the fitted coefficients to turn the recommended forecast into matching sales.

A prepared model and ClickHouse SQL produce forecasts for learner review. Approval commits a new price and receipt in Postgres. The writer scales captured baseline demand using a fixed price-response rule, shares a random draw between approved and old-price paths, caps sales by separate stock, and emits transactions. ClickHouse stores the rows, maintains minute aggregates, and computes receipt-scoped observed results.
  1. Approve and commit. The workbench saves the chosen price and an approval receipt in Managed Postgres. For that store/SKU, the writer replaces its ordinary sales feed with paired applied-price and unchanged-price generation, avoiding duplicate primary sales.
  2. Calculate synthetic demand. The writer spreads the captured baseline demand across the remaining business time before expiry. For each batch, it applies the fixed response (new price / old price)^−2. Reducing SGD 8 to SGD 6 gives approximately 1.78× expected demand, before stock limits. This is an assumption of the synthetic environment, not a learned fact about real shoppers.
  3. Generate integer transactions. The writer samples a random nonnegative integer sales count around each expected mean (a Poisson distribution). Both paths use the same random input, then cap their counts by separate available stock. One possible batch could sell two units at SGD 6 and one at SGD 8; actual counts vary. Each sold unit becomes a transaction row carrying its price, branch, and receipt ID. Only applied sales reduce operational inventory.
  4. Ingest and aggregate. The writer inserts those rows into pricing.retail_sales. The incremental materialized views update the minute aggregates, preserving source and branch. ClickPipes delivers the operational batch and receipt evidence.
  5. Query observed results. Chain queries use applied sales. The receipt-scoped retail_outcome_series view reads the transaction rows and CDC evidence to calculate cumulative units, receipts, stock, waste, and contribution for both paths. The chart displays these recorded synthetic outcomes; they can differ from the earlier forecast.

This exercise demonstrates a controlled pricing feedback loop. Because the generator shares captured baseline inputs and uses a predefined response, its results are not an independent validation of production model accuracy. The model reference explains both components and their limits.

Actions

1. Approve the price yourself

  1. Return to the workbench and refresh your selected product's comparison.
  2. Select Review SGD … for your chosen price.
  3. Read the store, SKU, expected profit change, and current stock.
  4. Select Approve and apply price.
  5. Wait for the Price applied: SGD … confirmation beside your selection. It includes the Receipt ID and a View observed results button. Select that button to follow subsequent sales.

The server checks current inventory, price, policy, and model before it commits. If the evidence changed, refresh the comparison and review again.

Price applied confirmation above the captured price comparison for Store 185 and SKU 101

In this example, Price applied: SGD 6.00 confirms the committed change. The table retains the comparison captured when the price was SGD 8.00. Its SGD 42.33 profit change is a projection until expiry; the next step measures subsequent sales.

2. Watch observed transactions

  1. Stop the continuing sales writer in its terminal with Ctrl+C and wait for its container to exit.
  2. Advance the next two hours of synthetic selling time:
.venv/bin/python -m app.loadgen.retail_writer --transactions-per-minute 10000 --minutes 120 --speed 20
  1. Keep Observed results open. Watch the business-time boundary advance as completed batches reach ClickHouse. Compare the applied-price and unchanged-price curves.
  2. Open View observed sales data to inspect the underlying rows.

Read the early observation

Observed sales for Store 61, SKU 102 from 03:20 to 03:30 UTC: the solid green applied-price branch has one sale at SGD 6.50, and the dashed purple unchanged-price branch has one sale at SGD 9.00. Both have 56 units remaining.

Read the chart:

PartHow to interpret it
Horizontal axisSimulated business time in UTC, here 03:20–03:30. Each point represents a business minute.
Vertical axisCumulative sales receipts in SGD since approval. Read the price itself from the cards below.
Solid green: Applied priceRecorded synthetic sales using the approved SGD 6.50 price. This branch updates operational stock.
Dashed purple: Unchanged priceA simulated comparison that keeps the previous SGD 9.00 price, with its own stock balance. Exclude it from the workshop's chain-sales totals.

A rising segment means additional receipts between those business minutes. A flat segment means no additional receipts in that interval. ClickHouse groups the receipt-linked transactions by minute and branch, then uses running sums to produce these cumulative curves.

Interpret this example: Each branch has sold one unit, so receipts are SGD 6.50 versus SGD 9.00, with 56 units remaining in each branch. At this early point, unchanged pricing has collected more revenue. The lower price needs enough additional sales to compensate for earning less per unit. Watch both units and contribution as more sales arrive; revenue alone does not determine profit. Your observation window and totals will differ.

The contribution cards deduct the purchase cost of all starting stock, plus selling costs and disposal costs when applicable. They can start negative because early sales have recovered only part of that purchase cost. Before expiry, the 56 unsold units are remaining stock; they become waste only at expiry.

Follow the same comparison later

The next screenshot follows the same receipt, Store 61 / SKU 102, later in business time. Additional sales at the applied price move the green revenue curve above the unchanged-price curve.

Later observed results for Store 61, SKU 102: the SGD 6.50 applied-price branch has sold five units for SGD 32.50, with 52 remaining and contribution of minus SGD 225.00. The SGD 9.00 unchanged-price branch has sold one unit for SGD 9.00, with 56 remaining and contribution of minus SGD 247.70.

Measure at this pointApplied SGD 6.50Unchanged SGD 9.00
Units sold51
Cumulative receiptsSGD 32.50SGD 9.00
Remaining stock5256
Contribution−SGD 225.00−SGD 247.70

Connect the two observations: Initially, one sale at SGD 9.00 earned more than one sale at SGD 6.50. Later, the lower-price branch has sold four additional units, collected SGD 23.50 more revenue, and improved contribution by SGD 22.70. The contribution improvement is smaller than the revenue increase because the additional sales also incur selling costs.

The comparison is still running. Both contribution values remain negative, and the remaining stock has not expired. Continue observing sales and compare waste and contribution again at expiry; these screenshots show an interim synthetic outcome.

Advance two business hours faster

A single product may sell only a few units per hour. --minutes 120 --speed 20 requests two hours of simulated sales in about six minutes of elapsed time: 120 ÷ 20 = 6. Each batch still has to complete, so delivery can take longer. The writer stops earlier if the trading day ends.

At speed 20, 120 business minutes target six elapsed minutes. For example, business time advances from 03:30 to 05:30 UTC. Every batch still generates sales, commits and delivers them, reaches ClickHouse, and refreshes the chart.

Follow Live business time (UTC) as new batches arrive. The diagram's times are an example: the writer resumes your current business clock and stock, without repeating Module 02's high-rate burst. The speed setting changes the simulation's pacing; measure ClickHouse query performance separately.

Two prices under the same simulated conditions

Think of two copies of the same product in the same store. Both start with the same stock and receive the same simulated demand conditions, including the same random draw for each batch. One copy uses the approved price; the other keeps the previous price. Those prices can produce different numbers of sales.

Both copies start with 57 units and share the simulated demand input. The approved SGD 6.50 branch sells five units and updates operational stock to 52. The unchanged SGD 9.00 branch sells one unit and keeps its own comparison balance of 56. ClickHouse records both, but chain KPIs include only applied sales.

Only the applied-price branch changes operational inventory in Managed Postgres. The unchanged-price branch keeps a separate simulated stock balance. ClickHouse stores their generated transactions and compares the running totals. The five-versus-one counts illustrate the screenshot above; your counts change as sales arrive.

This controlled simulation illustrates price response. Production effects require their own evidence, and the observed integer sales can differ from the earlier weighted forecasts.

Record both branches' units, revenue, and contribution.

3. Query the observed series directly

Run this query in the SQL console without edits. It finds the current run and its latest approved price, then returns the recorded sales for both price branches.

WITH
 (SELECT run_id FROM pricing.retail_analysis_context LIMIT 1) AS current_run,
 (SELECT receipt_id FROM pricing.retail_receipts_cdc FINAL
  WHERE run_id = current_run AND _peerdb_is_deleted = 0
  ORDER BY acknowledged_at DESC, receipt_id DESC LIMIT 1) AS latest_receipt
SELECT current_run AS run_id, latest_receipt AS receipt_id,
 minute, branch, price_minor / 100 AS price_sgd, units,
 receipts_minor / 100 AS receipts_sgd, remaining_stock, waste_units,
 contribution_minor / 100 AS contribution_sgd
FROM pricing.retail_outcome_series(
 run=current_run, receipt=latest_receipt)
ORDER BY minute, branch LIMIT 1000;

Check that receipt_id matches the receipt shown in Observed results, then compare the newest values with the chart. If you approved more than one price, open the latest approval in the workbench. If no rows appear, finish Approve and apply price and allow CDC to deliver the receipt before running the query again.

Use branch='applied' for chain KPIs; including the comparison branch would count simulated alternatives as extra business sales.

Verify

Explain the difference between a projected scenario and an observed event series. Why must the comparison branch be excluded from total chain revenue? How does the receipt ID connect an approved price to the observed sales in ClickHouse?

Next: Create your pricing strategy agent.

Trên trang này

Track your progress?

Optional. We email a link to confirm your address; progress records once you open it.

Please use your work email address, not a personal one.

By continuing you accept the Terms.

VI