Model dependenceLesson 28 of 76 · 8 min
Generate correlated normal inputs
Generate two correlated normal inputs using shared and independent variation.
Start with independent standard normal draws and : each has mean 0 and SD 1. Choose correlation ρ between -1 and 1. Set and . The second input shares part of A and adds fresh independent variation; the coefficients preserve its model variance.
For ρ=.7, the shared coefficient is .7 and the fresh coefficient is , about .714. B's variance is , while its correlation with A is .7. Covariance is the expected product of deviations from the means; a positive value indicates movement in the same direction. For these standardised variables, covariance equals correlation.
Scale and shift into units: cost A is and cost B is . Standardised means zero mean and unit SD; scaling changes covariance and units but preserves correlation here. The printed covariance matrix is a table: variances down the diagonal and pairwise covariances off it. Nonlinear transformations can change correlation, so check the resulting joint model.
is the requested correlation; are independent mean-zero, unit-SD normal draws. The square-root coefficient keeps B's variance equal to one. The assert line stops with a message if the chosen correlation is invalid.
If correlation changes from .7 to −.7, must either input’s own variance change?
Write a prediction (optional)
Seeded teaching simulation
Select Run Python below with the code unchanged.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
Published example output
This result comes from the original Python. Your runs appear above. Build environment ↗
Sample standardised covariance: [[0.995 0.701] [0.701 1.01 ]] Correlation: 0.7 Total-cost SD: 16.626
Read the first result
Check mean approximately 0, variance approximately 1 and covariance approximately ρ before scaling. The finite sample approximates those model quantities; it need not match exactly.
Try an edit
Replace rho = .7 with rho = -.7. Select Run Python again. Compare the two diagonals of Sample standardised covariance, the off-diagonal entries and Total-cost SD. Which reflects changes to the connection between the inputs rather than to their individual spreads?
Scope. Normal inputs allow negative values and linear dependence. They may be inappropriate for bounded or strongly skewed quantities.
Try a fresh case
You specify a correlation of 1.4 between standardised variables. What should you do?
Need a hint?
Hint 1
Check the mathematical constraints on correlation.
Hint 2
Check the coefficients that keep the second input’s variance equal to one.