Signature
b_hat = S_XY / S_XX; mu_hat = Y_bar - b_hat * (X_bar - mu_X); RSS = S_YY - b_hat * S_XY; SE_reg = sqrt(RSS / (n - 2) * (1 / n + (X_bar - mu_X)^2 / S_XX))
| Inputs | Definition | Unit |
|---|---|---|
S_XY | Sum over the run of (X_i minus X_bar) times (Y_i minus Y_bar) | control units times target units |
S_XX | Sum over the run of (X_i minus X_bar) squared, above zero | control units squared |
Y_bar | Mean of the target output over the run | target units |
X_bar | Mean of the control output over the run | control units |
mu_X | Expected value of the control, known from the input distribution or a closed form | control units |
S_YY | Sum over the run of (Y_i minus Y_bar) squared | target units squared |
n | Number of independent replications in the run, at least 3 | count of replications |
b_hat | Least squares slope of the target on the control in the run | target units per control unit |
|---|---|---|
mu_hat | Adjusted estimate of the expected target at b_hat, equal to the intercept of the regression on the centred control | target units |
RSS | Sum of squared residuals from the regression of the target on the centred control | target units squared |
SE_reg | Standard error of the fitted intercept, reported as the standard error of mu_hat | target units |
Function
Control variate adjustment of a Monte Carlo estimate using an output of known mean
Takes a target output and a control output from the same n independent replications of a simulation, the known expected value of the control and the computing cost of each, and returns the adjusted estimate of the expected target with its standard error, the coefficient that scales the correction and the ratio that shows whether the control repays its cost. In the article's example the survival times come from HE-FM-DES-001 and the discounted life years take the form of HE-FM-YLL-003 with a simulated survival time in place of its L. Antithetic sampling (HE-FM-ANTV-001) changes how the draws are made, whereas these records leave the sampling unchanged and work on the outputs after the run. Notation follows the Control Variates article.
Computational function
Computational function for a control variate estimate from uniform draws in a discounted survival model
Takes a hazard, a continuous discount rate and a table of uniform draws, simulates exponential survival, and returns the control variate estimate of mean discounted life years using the undiscounted survival time as the control. Its inputs differ from those of HE-FM-CONV-004, which starts from the run's sums. The survival times follow HE-FM-DES-001, the discounted outcome takes the form of HE-FM-YLL-003 with T in place of L, and the slope and estimate follow HE-FM-CONV-004 and HE-FM-CONV-001. The known mean of the control is 1 over h. The exact coefficient b_opt, the square of h over h plus r, is the ratio of the article's moments in HE-EX-CONV-005 (derived). The calculator line writes five patients out term by term; the Excel, R and Python code take any number of patients.
Inputs and outputs:
his the constant hazard of death per year, above zero;ris the continuous discount rate per year, above zero;U_1toU_5are uniform draws, each above 0 and at most 1;T_1toT_5are survival times in years;Y_1toY_5are discounted life years;mu_Xis the known mean survival in years;X_barandY_barare the run means;S_XXandS_XYare the sums of squares and cross-products;b_hatis the fitted coefficient;mu_hatis the estimate atb_hatin years;b_optis the exact coefficient;mu_optis the estimate atb_optin years. The code also returnsse_reg, the intercept standard error, and takesy,xandmuas vectors of target and control outputs and the known mean, so it applies to any model outputs.Assumption: Survival is exponential with a constant hazard and each patient has one independent draw, so the mean survival of 1 over h is known exactly from the input distribution. The Assumption rows of HE-FM-CONV-004 apply, and five patients are for reading only.
Worked example (Article's five patients): The article's table of five draws gives a plain mean of 6.258, a fitted estimate of 7.859 and an estimate of 7.483 at the exact coefficient, against a true 7.407. The chain carries unrounded draws, so its control mean is 7.7675 where the article's rounded table gives 7.767.
h = 0.1; r = 0.035; U_1 = 0.15; U_2 = 0.35; U_3 = 0.55; U_4 = 0.75; U_5 = 0.95; X_bar = 7.7675; Y_bar = 6.2576; b_hat = 0.717; mu_hat = 7.859; b_opt = 0.5487; mu_opt = 7.483Worked example (Other five draws): Illustrative draws. The plain mean of 7.235 is 0.172 below the true 7.407, while the estimate at the exact coefficient, 7.606, is 0.199 above it, since the method lowers the variance across runs and does not improve every single run.
h = 0.1; r = 0.035; U_1 = 0.1; U_2 = 0.3; U_3 = 0.5; U_4 = 0.7; U_5 = 0.9; X_bar = 9.3235; Y_bar = 7.2352; b_hat = 0.6638; mu_hat = 7.684; b_opt = 0.5487; mu_opt = 7.606Excel: Fixed uniform draws in cells A2 to A6, with cells named Hazard and DiscountRate.
=-LN(A2)/Hazardin B2,=(1-EXP(-DiscountRate*B2))/DiscountRatein C2 and=B2-1/Hazardin D2, filled down to row 6.=SLOPE(C2:C6,D2:D6)in a cell named FittedCoefficient,=AVERAGE(C2:C6)-FittedCoefficient*(AVERAGE(B2:B6)-1/Hazard)in a cell named ControlVariateEstimate,=INDEX(LINEST(C2:C6,D2:D6,TRUE,TRUE),2,2)for the standard error,=(Hazard/(Hazard+DiscountRate))^2in a cell named ExactCoefficient and=AVERAGE(C2:C6)-ExactCoefficient*(AVERAGE(B2:B6)-1/Hazard)in a cell named ExactCoefficientEstimate. A draw of 0 in column A returns #NUM! and a zero DiscountRate returns #DIV/0!.R:
cv_reg <- function(y, x, mu) { n <- length(y); xb <- mean(x); yb <- mean(y); sxx <- sum((x-xb)^2); sxy <- sum((x-xb)*(y-yb)); syy <- sum((y-yb)^2); b <- if (sxx > 0) sxy/sxx else 0; est <- yb-b*(xb-mu); rss <- max(syy-b*sxy, 0); se <- if (n > 2 && sxx > 0) sqrt(rss/(n-2)*(1/n+(xb-mu)^2/sxx)) else NaN; c(b_hat = b, mu_hat = est, se_reg = se) }Withu <- c(0.15, 0.35, 0.55, 0.75, 0.95); t <- -log(u)/0.1; y <- (1-exp(-0.035*t))/0.035,cv_reg(y, t, 10)returns about 0.7173, 7.8589 and 0.2707, andsummary(lm(y ~ I(t-10)))$coefficientsgives the same intercept and standard error. A control with no spread returns a coefficient of 0 and the plain mean with a standard error of NaN, and fewer than 3 patients return NaN for the standard error.Python:
def cv_reg(y, x, mu): n = len(y); xb = sum(x)/n; yb = sum(y)/n; sxx = sum((a-xb)**2 for a in x); sxy = sum((a-xb)*(c-yb) for a, c in zip(x, y)); syy = sum((c-yb)**2 for c in y); b = sxy/sxx if sxx > 0 else 0.0; est = yb-b*(xb-mu); rss = max(syy-b*sxy, 0.0); se = math.sqrt(rss/(n-2)*(1/n+(xb-mu)**2/sxx)) if n > 2 and sxx > 0 else float('nan'); return b, est, seUses the math module and returns the same three values as the R code, with the same results for a control with no spread and for fewer than 3 patients. An empty list raises ZeroDivisionError.Test (Fitted estimate matches the article): With the article's five draws, h of 0.1 and r of 0.035, ControlVariateEstimate is compared with 7.859. Expected result TRUE. Doubling the cell to 15.718 or halving it to 3.93 gives FALSE, and so does the plain mean of 6.258. Check
=ABS(ControlVariateEstimate-7.859)<0.001Test (Fitted estimate equals the regression intercept): ControlVariateEstimate is compared with the intercept from the centred column D, a route that does not use the coefficient cell. Expected result TRUE. Doubling or halving the cell gives FALSE, and column D holding the raw survival times makes LINEST return 0.686 and FALSE. Check
=ABS(ControlVariateEstimate-INDEX(LINEST(C2:C6,D2:D6,TRUE,TRUE),1,2))<1E-9Test (Exact coefficient reproduces the article's adjusted figure): With the same draws, ExactCoefficientEstimate is compared with the unrounded article figure of 7.4826, and ExactCoefficient with 0.5487. Expected result TRUE. Doubling ExactCoefficient gives 1.0974 and an estimate of 8.708, and halving it gives 0.2743 and 6.870, all FALSE. Check
=AND(ABS(ExactCoefficientEstimate-7.4826)<0.0001,ABS(ExactCoefficient-0.5487)<0.0001)Common error (Control mean taken from the run): Setting the known mean to the run mean of 7.7675 in place of 1 over h, 10, makes the correction zero and returns the plain 6.258 for these draws.
Source: Owen AB. Monte Carlo Theory, Methods and Examples. 2013. Chapter 8, Variance reduction, section 8.9, equation 8.29 and Algorithm 8.3, which regresses the outputs on the centred controls and delivers the intercept and its standard error.
T_1 = -log(U_1) / h; T_2 = -log(U_2) / h; T_3 = -log(U_3) / h; T_4 = -log(U_4) / h; T_5 = -log(U_5) / h; Y_1 = (1 - exp(-r * T_1)) / r; Y_2 = (1 - exp(-r * T_2)) / r; Y_3 = (1 - exp(-r * T_3)) / r; Y_4 = (1 - exp(-r * T_4)) / r; Y_5 = (1 - exp(-r * T_5)) / r; mu_X = 1 / h; X_bar = (T_1 + T_2 + T_3 + T_4 + T_5) / 5; Y_bar = (Y_1 + Y_2 + Y_3 + Y_4 + Y_5) / 5; S_XX = (T_1 - X_bar)^2 + (T_2 - X_bar)^2 + (T_3 - X_bar)^2 + (T_4 - X_bar)^2 + (T_5 - X_bar)^2; S_XY = (T_1 - X_bar) * (Y_1 - Y_bar) + (T_2 - X_bar) * (Y_2 - Y_bar) + (T_3 - X_bar) * (Y_3 - Y_bar) + (T_4 - X_bar) * (Y_4 - Y_bar) + (T_5 - X_bar) * (Y_5 - Y_bar); b_hat = S_XY / S_XX; mu_hat = Y_bar - b_hat * (X_bar - mu_X); b_opt = (h / (h + r))^2; mu_opt = Y_bar - b_opt * (X_bar - mu_X)
Try this function
Implementations
Excel
Fitted coefficient, estimate and standard error from draw ranges
With equal ranges named TargetDraws and ControlDraws and the known mean in KnownMeanControl, the formulas fill cells named EstCoefficient, RegressionEstimate, ResidualSumSquares and RegressionStandardError. Every cell in both ranges must hold a number, since AVERAGE and COUNT skip a blank where the SUMPRODUCT arithmetic counts it as 0. A constant control returns #DIV/0!.
=SUMPRODUCT((ControlDraws-AVERAGE(ControlDraws))*(TargetDraws-AVERAGE(TargetDraws)))/SUMPRODUCT((ControlDraws-AVERAGE(ControlDraws))^2); =AVERAGE(TargetDraws)-EstCoefficient*(AVERAGE(ControlDraws)-KnownMeanControl); =SUMPRODUCT((TargetDraws-AVERAGE(TargetDraws))^2)-EstCoefficient*SUMPRODUCT((ControlDraws-AVERAGE(ControlDraws))*(TargetDraws-AVERAGE(TargetDraws))); =SQRT(ResidualSumSquares/(COUNT(TargetDraws)-2)*(1/COUNT(TargetDraws)+(AVERAGE(ControlDraws)-KnownMeanControl)^2/SUMPRODUCT((ControlDraws-AVERAGE(ControlDraws))^2)))
Assumptions
Bias from a coefficient fitted on the same run neglected
The bias from estimating b on the run is ordinarily negligible when the number of controls is far below the root of n. When an unbiased estimate is required, the coefficient comes from an independent pilot sample and enters HE-FM-CONV-001 as a fixed b.
Control with spread and enough replications for the regression route
The control varies within the run, so S_XX is above zero, and the run has at least 3 replications for the residual degrees of freedom. Five replications are for reading only; a usable coefficient needs a run of thousands or a pilot.
Intercept standard error alongside the plug-in variance estimate
SE_reg divides RSS by n minus 2 and includes the term in the squared control gap, as regression software does. The plug-in estimate RSS over n squared is nearly the same in a long run (derived). In the seeded run of HE-EX-CONV-013 the two are 0.0531 and 0.0530 years; in the five-patient run they are 0.271 and 0.198, one more reason five points are too few.
Worked examples
Five patients with a coefficient fitted to the run
The article's five illustrative patients, with the means and sums taken from draws rounded to three decimals. The slope of 0.7173 is well above the optimal 0.5487 and the estimate of 7.859 is further from the true 7.407 than the 7.483 of HE-EX-CONV-001, which is why the article takes the coefficient from a large run or a pilot. The article's figures are 0.717 and 7.859.
n = 5; Y_bar = 6.2576; X_bar = 7.7674; mu_X = 10; S_XX = 212.7211; S_XY = 152.5843; S_YY = 110.4332; b_hat = 0.7173; mu_hat = 7.859; RSS = 0.985; SE_reg = 0.271
Seeded run of 1,000 patients in the survival model
A run of 1,000 patients in the article's illustrative model, with each U equal to 1 minus random.random() after Python random.seed(20261107), and with the sums rounded to whole numbers. The plain mean of 7.220 is 0.187 years below the true 7.407 with a standard error of 0.182; the estimate of 7.304 is 0.103 below it with a standard error of 0.053. The fitted slope of 0.5238 is close to the optimal 0.5487.
n = 1000; Y_bar = 7.22; X_bar = 9.84; mu_X = 10; S_XX = 110727; S_XY = 58004; S_YY = 33195; b_hat = 0.5238; mu_hat = 7.304; RSS = 2809.8; SE_reg = 0.0531
Common errors
Regressing the target on the control without centring
The fitted intercept matches the expected target only when the known mean is subtracted from the control first. With the raw control the five-patient regression returns an intercept of 0.686, with the same slope, in place of 7.859.
Reading the slope standard error as that of the control variate estimate
LINEST returns the slope's standard error in row 2, column 1 and the intercept's in row 2, column 2. In the five-patient run the first is 0.039 and the second 0.271 years, so the wrong cell understates the standard error about seven times.
Carrying a fitted control coefficient to another parameter set
A coefficient fitted under one parameter set or strategy may not hold under another. In the article's model the optimal coefficient is the square of h over h plus r (derived), 0.5487 at a hazard of 0.1 and 0.7243 at a hazard of 0.2, so a value carried across hazards is no longer the variance-minimising one.
Sources
Owen on regression software for control variates and the bias from fitting the coefficient
Owen AB. Monte Carlo Theory, Methods and Examples. 2013. Chapter 8, Variance reduction, section 8.9. Algorithm 8.3 regresses the outputs on the centred controls and delivers the fitted intercept and its standard error, and the text states that the known means must be subtracted for the intercept to match the expected value. Equation 8.33 is the least squares estimate of the coefficient. Equation 8.34 and the text after it give the error of the fitted estimator, with a first term of order 1 over n and a second of order 1 over the root of n, and common neglect of the bias when the number of controls is far below the root of n; a pilot sample gives an unbiased version. The records follow Algorithm 8.3. The estimated variance printed before Example 8.3 centres the control term on its sample mean; the fitted residuals of Algorithm 8.3 centre it on the known mean, which gives RSS over n squared (derived).
Rubinstein and Marcus on the loss from estimating the control coefficient
Rubinstein RY, Marcus R. Efficiency of multivariate control variates in Monte Carlo simulation. Operations Research. 1985;33(3):661-677. Gives a result that quantifies the loss in variance reduction caused by estimating the optimal control matrix, and numerical results showing good variance reduction when the number of control variates is relatively small.
Canonical Identity
Stable URI · Machine-readable · Resolvable · CC BY 4.0
