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 Z1Z_1 and Z2Z_2: each has mean 0 and SD 1. Choose correlation ρ between -1 and 1. Set A=Z1A=Z_1 and B=ρZ1+1−ρ2Z2B=\rho Z_1+\sqrt{1-\rho^2}Z_2. 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 .51\sqrt{.51}, about .714. B's variance is .49+.51=1.49+.51=1, 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 100+10A100+10A and cost B is 80+8B80+8B. 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.

ρ\rho is the requested correlation; Z1,Z2Z_1,Z_2 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.

Share one normal input, add fresh variation
-4-2024-4-2024Standardised input BStandardised input A
-4-2024-4-2024Standardised input BStandardised input A
B=ρZ1+1−ρ2Z2B=\rho Z_1+\sqrt{1-\rho^2}Z_2
Reference illustration. Published seeded standardised normal draws, displaying every 80th point without selecting by outcome. The target correlation is 0.7. Rescaling each coordinate changes covariance and units while preserving this linear correlation.

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.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

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?

Back to the Python editor

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?

Choose an answer
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.

Your notes