File size: 1,815 Bytes
d517dc9 | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "reason-0075", "name": "causal"}
{"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}}
{"type": "message", "message": {"role": "user", "content": "Explain causal reasoning with concrete examples and common pitfalls. Include a runnable code example with its expected output."}}
{"type": "message", "message": {"role": "assistant", "content": "**Causal Reasoning:** X causes Y, not just X correlates with Y.\n\n**Confounding:** A third variable Z causes both X and Y.\n\n```python\n# Spurious correlation example\nimport numpy as np\n\nnp.random.seed(42)\nn = 1000\n\n# Ice cream sales and drowning (confounded by temperature)\ntemperature = np.random.normal(30, 5, n)\nice_cream = 0.8 * temperature + np.random.normal(0, 2, n)\ndrownings = 0.5 * temperature + np.random.normal(0, 3, n)\n\ncorrelation = np.corrcoef(ice_cream, drownings)[0, 1]\nprint(f'Correlation: {correlation:.2f}') # ~0.4 (strong!)\nprint('But ice cream does not cause drowning!')\n\n# Control for confounder\nmask = (temperature > 25) & (temperature < 35) # same temperature range\ncontrolled_corr = np.corrcoef(ice_cream[mask], drownings[mask])[0, 1]\nprint(f'Controlled: {controlled_corr:.2f}') # ~0.1 (much weaker)\n```\n\n**Methods to establish causation:**\n- Randomized controlled trials (RCTs)\n- Natural experiments\n- Instrumental variables\n- Difference-in-differences\n- Propensity score matching"}}
|