Conditional expectation calculator
Conditional expectation describes the average value of a random variable after you learn that a particular event occurred.
Run — free
Runs in your browser. Free, unlimited — your data never leaves this page.
This calculator accepts a finite list of possible values, the joint probability attached to each outcome, and a marker showing whether that outcome belongs to the conditioning event. It adds the probability mass inside the event, renormalizes those selected outcomes, and returns the resulting expected value. It also reports the intermediate probability and weighted sum, making the calculation easy to inspect or incorporate into a larger analysis.
Represent the joint outcomes and conditioning event
Enter one row for every possible joint outcome in the discrete model. The value field is the value taken by the random variable X, while probability is the probability mass assigned to that complete outcome. Set in_event to true exactly when the outcome belongs to the event A that you want to condition on. Repeated values are allowed because different underlying outcomes can produce the same value of X while differing in event membership or probability. The rows together must describe a complete probability distribution, so their probabilities must be nonnegative and sum to one. This explicit row format avoids ambiguity about whether a probability is marginal, conditional, or joint: every probability belongs to the outcome on its own row. Include zero-probability outcomes if they are useful for documenting the model, although they do not affect the result. Do not pre-normalize only the selected rows; provide the original joint probabilities so the calculator can determine P(A) and perform the conditioning correctly.
Understand how the conditional expectation is calculated
The calculator first adds the probabilities of all rows marked in_event, producing the conditioning probability P(A). It separately multiplies each selected value by its original joint probability and adds those products. The conditional expectation is the weighted selected sum divided by P(A): E[X given A] equals the sum of x times P(X equals x and A), divided by P(A). Dividing by the event probability renormalizes the selected probability mass so that it behaves like a complete distribution after the event is known. The response includes conditional_expectation, conditioning_probability, and weighted_event_sum, along with selected and total row counts. These intermediate fields are useful for auditing a result and catching modeling mistakes. If P(A) is zero, division would be undefined, so the capability returns an invalid-input error instead of producing Infinity, NaN, or a misleading numeric answer. All arithmetic is deterministic and uses the supplied finite numbers directly.
Check assumptions before using the result
A conditional mean is only as meaningful as the event and distribution supplied to it. Confirm that the rows are mutually exclusive and collectively exhaustive, since overlapping rows would double-count probability and missing rows would leave the distribution incomplete. The sum-to-one validation catches many omissions, but it cannot determine whether two semantic outcomes overlap. Also check that in_event expresses the information actually observed. Conditioning on a related but different event can produce a plausible number that answers the wrong question. For large or tiny values, review the returned weighted_event_sum and conditioning_probability as a quick numerical sanity check. A rare event can make the conditional expectation depend strongly on a small amount of probability mass; that sensitivity is mathematically valid, but the input probabilities may deserve extra scrutiny. For automated work, the same deterministic calculation is available through the API for $0.002 per request, allowing reproducible checks in risk models, quality-control pipelines, classroom tools, and decision analyses.
What you can do with it
Expected loss after a trigger
Calculate average loss among scenarios where a policy trigger, default condition, or operational threshold occurs.
Quality outcome after inspection
Find the expected measurement given that an item passed or failed a specified inspection event.
Teach discrete conditioning
Show students the event probability, selected weighted sum, and normalized conditional mean from one transparent table.
FAQ
What formula does the calculator use?
It uses E[X | A] = sum of x times P(X = x and A), divided by P(A), over the rows marked as belonging to A.
What happens when the conditioning probability is zero?
The request returns an invalid-input error because conditional expectation given a zero-probability event is undefined in this finite calculation.
Must the probabilities sum to one?
Yes. The supplied rows represent the complete joint distribution, and their probabilities must sum to one within a small floating-point tolerance.
Can several rows have the same value?
Yes. Rows represent joint outcomes, so the same random-variable value may occur in several outcomes with different probabilities or event membership.
How much does an API request cost?
Each API request costs $0.002. The calculation is deterministic and does not call a model or external service.
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/stat/conditional-expectation \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"outcomes":[{"value":10,"probability":0.2,"in_event":true},{"value":20,"probability":0.3,"in_event":true},{"value":40,"probability":0.5,"in_event":false}]}'const res = await fetch("https://api.kit.forhosting.com/stat/conditional-expectation", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"outcomes": [
{
"value": 10,
"probability": 0.2,
"in_event": true
},
{
"value": 20,
"probability": 0.3,
"in_event": true
},
{
"value": 40,
"probability": 0.5,
"in_event": false
}
]
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/stat/conditional-expectation",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"outcomes": [
{
"value": 10,
"probability": 0.2,
"in_event": true
},
{
"value": 20,
"probability": 0.3,
"in_event": true
},
{
"value": 40,
"probability": 0.5,
"in_event": false
}
]
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/stat/conditional-expectation", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"outcomes":[{"value":10,"probability":0.2,"in_event":true},{"value":20,"probability":0.3,"in_event":true},{"value":40,"probability":0.5,"in_event":false}]}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"outcomes":[{"value":10,"probability":0.2,"in_event":true},{"value":20,"probability":0.3,"in_event":true},{"value":40,"probability":0.5,"in_event":false}]}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/stat/conditional-expectation", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"outcomes": [
{
"value": 10,
"probability": 0.2,
"in_event": true
},
{
"value": 20,
"probability": 0.3,
"in_event": true
},
{
"value": 40,
"probability": 0.5,
"in_event": false
}
]
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "stat.conditional_expectation",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |