| """ |
| Dataset Generator: Housing Tenure Security Africa |
| |
| Parameter Evidence Table: |
| | Parameter | Value | Source | |
| |-----------|-------|--------| |
| | Informal tenure prevalence | 60-80% in slums | UN-Habitat 2023 | |
| | Rental housing share | 30-50% urban | OECD Africa Urbanization 2025 | |
| | Title deed availability | 10-30% informal | World Bank land data | |
| | Eviction risk in informal | 15-40% | UN-Habitat estimates | |
| | Year range | 2018-2025 | Project scope | |
| | Countries | 15 African nations | Regional coverage | |
| |
| DAG Structure: |
| country -> tenure_type -> security_level -> documentation -> risk_factors |
| """ |
|
|
| import numpy as np |
| import pandas as pd |
| from pathlib import Path |
|
|
| COUNTRIES = [ |
| 'Nigeria', 'Kenya', 'Ethiopia', 'Egypt', 'South Africa', |
| 'Tanzania', 'Morocco', 'Algeria', 'Ghana', 'Uganda', |
| 'Mozambique', 'Zambia', 'Malawi', 'Rwanda', 'Senegal' |
| ] |
|
|
| SCENARIOS = { |
| 'low_burden': {'n': 4000, 'informal_pct': 0.45, 'seed': 42}, |
| 'moderate': {'n': 5000, 'informal_pct': 0.60, 'seed': 43}, |
| 'high': {'n': 6000, 'informal_pct': 0.75, 'seed': 44} |
| } |
|
|
| CITIES = { |
| 'Nigeria': ['Lagos', 'Kano', 'Ibadan'], |
| 'Kenya': ['Nairobi', 'Mombasa', 'Kisumu'], |
| 'Ethiopia': ['Addis Ababa', 'Dire Dawa'], |
| 'Egypt': ['Cairo', 'Alexandria'], |
| 'South Africa': ['Johannesburg', 'Cape Town'], |
| 'Tanzania': ['Dar es Salaam'], |
| 'Morocco': ['Casablanca', 'Rabat'], |
| 'Algeria': ['Algiers'], |
| 'Ghana': ['Accra', 'Kumasi'], |
| 'Uganda': ['Kampala'], |
| 'Mozambique': ['Maputo', 'Beira'], |
| 'Zambia': ['Lusaka'], |
| 'Malawi': ['Lilongwe', 'Blantyre'], |
| 'Rwanda': ['Kigali'], |
| 'Senegal': ['Dakar'] |
| } |
|
|
| def dag_sample_tenure_type(informal_pct, rng): |
| weights = [informal_pct, 0.35, 0.15, 0.08] |
| weights = [w / sum(weights) for w in weights] |
| return rng.choice(['informal', 'rental', 'owner_occupied', 'customary'], p=weights) |
|
|
| def dag_sample_security_level(tenure_type, informal_pct, rng): |
| security_map = { |
| 'informal': rng.uniform(0.2, 0.5), |
| 'rental': rng.uniform(0.4, 0.7), |
| 'owner_occupied': rng.uniform(0.7, 0.95), |
| 'customary': rng.uniform(0.3, 0.6) |
| } |
| if tenure_type == 'informal': |
| security_map['informal'] *= (1 - informal_pct * 0.3) |
| return round(security_map[tenure_type], 3) |
|
|
| def dag_sample_documentation(tenure_type, security, rng): |
| doc_chances = { |
| 'informal': 0.15, |
| 'rental': 0.55, |
| 'owner_occupied': 0.75, |
| 'customary': 0.25 |
| } |
| has_title = rng.random() < doc_chances.get(tenure_type, 0.3) |
| |
| return { |
| 'has_title_deed': has_title, |
| 'has_formal_lease': rng.random() < doc_chances.get(tenure_type, 0.3) * 0.8, |
| 'proof_of_occupancy': rng.random() < 0.7, |
| 'registration_status': rng.choice(['registered', 'pending', 'none']) |
| } |
|
|
| def generate_dataset(scenario, output_dir): |
| config = SCENARIOS[scenario] |
| rng = np.random.default_rng(config['seed']) |
| |
| data = { |
| 'country': [], |
| 'city': [], |
| 'year': [], |
| 'tenure_type': [], |
| 'security_score': [], |
| 'has_title_deed': [], |
| 'has_formal_lease': [], |
| 'proof_of_occupancy': [], |
| 'registration_status': [], |
| 'years_in_tenure': [], |
| 'eviction_risk_pct': [], |
| 'perceived_security': [], |
| 'willingness_to_invest': [], |
| 'tenure_type_detail': [], |
| 'landlord_relationship': [], |
| 'rent_control_applicable': [] |
| } |
| |
| for _ in range(config['n']): |
| country = rng.choice(COUNTRIES) |
| city = rng.choice(CITIES.get(country, ['Unknown'])) |
| year = rng.integers(2018, 2026) |
| |
| tenure_type = dag_sample_tenure_type(config['informal_pct'], rng) |
| security = dag_sample_security_level(tenure_type, config['informal_pct'], rng) |
| docs = dag_sample_documentation(tenure_type, security, rng) |
| |
| eviction_risk = (1 - security) * 100 |
| |
| data['country'].append(country) |
| data['city'].append(city) |
| data['year'].append(year) |
| data['tenure_type'].append(tenure_type) |
| data['security_score'].append(security) |
| data['has_title_deed'].append(docs['has_title_deed']) |
| data['has_formal_lease'].append(docs['has_formal_lease']) |
| data['proof_of_occupancy'].append(docs['proof_of_occupancy']) |
| data['registration_status'].append(docs['registration_status']) |
| data['years_in_tenure'].append(int(rng.uniform(1, 25))) |
| data['eviction_risk_pct'].append(round(eviction_risk, 1)) |
| data['perceived_security'].append(rng.choice(['very_secure', 'secure', 'insecure', 'very_insecure'])) |
| data['willingness_to_invest'].append(round(rng.uniform(0.1, 0.9), 2)) |
| data['tenure_type_detail'].append(rng.choice(['formal', 'informal', 'customary', 'communal'])) |
| data['landlord_relationship'].append(rng.choice(['family', 'private', 'government', 'community'])) |
| data['rent_control_applicable'].append(rng.choice([True, False], p=[0.2, 0.8])) |
| |
| df = pd.DataFrame(data) |
| output_dir.mkdir(parents=True, exist_ok=True) |
| df.to_csv(output_dir / f'{scenario}.csv', index=False) |
| print(f"Generated {scenario}: {len(df)} rows") |
|
|
| if __name__ == '__main__': |
| base_dir = Path(__file__).parent |
| for scenario in SCENARIOS: |
| generate_dataset(scenario, base_dir) |
|
|