"""Generate synthetic online pharmacy & unregistered medicines dataset for SSA. Research-based parameterization: - WHO: 50% of medicines purchased online from illegal sites are falsified; growing e-pharmacy market in Africa. - SAHPRA: Fake medicines common; painkillers, antibiotics, antimalarials, ARVs, sexual stimulants most counterfeited online. - SSA context: Limited regulation of online pharmacies; social media sales; WhatsApp/Facebook marketplace; cross-border e-commerce. """ from __future__ import annotations from pathlib import Path import numpy as np import pandas as pd SEED = 42 N_PER_SCENARIO = 10_000 YEAR_RANGE = np.arange(2010, 2025) YEAR_WEIGHTS = np.linspace(0.85, 1.3, len(YEAR_RANGE)) YEAR_WEIGHTS = YEAR_WEIGHTS / YEAR_WEIGHTS.sum() SCENARIOS = { "social_media_marketplace": { "setting_probs": {"whatsapp": 0.30, "facebook_marketplace": 0.25, "instagram": 0.20, "telegram": 0.15, "tiktok": 0.10}, "product_probs": {"sexual_stimulant": 0.20, "weight_loss": 0.15, "antibiotic": 0.15, "analgesic": 0.12, "skin_lightening": 0.10, "supplement": 0.10, "antimalarial": 0.08, "other": 0.10}, "sf_prevalence": 0.45, "unregistered_pct": 0.70, "no_prescription_pct": 0.95, "pharmacist_involved_pct": 0.02, "verified_seller_pct": 0.05, }, "dedicated_online_pharmacy": { "setting_probs": {"dedicated_website": 0.35, "mobile_app": 0.25, "aggregator_platform": 0.20, "telemedicine_linked": 0.20}, "product_probs": {"antibiotic": 0.15, "antihypertensive": 0.12, "antidiabetic": 0.10, "analgesic": 0.12, "contraceptive": 0.08, "ARV": 0.05, "supplement": 0.15, "sexual_stimulant": 0.10, "other": 0.13}, "sf_prevalence": 0.20, "unregistered_pct": 0.40, "no_prescription_pct": 0.60, "pharmacist_involved_pct": 0.25, "verified_seller_pct": 0.30, }, "cross_border_ecommerce": { "setting_probs": {"alibaba_aliexpress": 0.30, "jumia_marketplace": 0.20, "international_website": 0.25, "dark_web": 0.10, "unknown_platform": 0.15}, "product_probs": {"antibiotic": 0.15, "sexual_stimulant": 0.15, "controlled_substance": 0.10, "weight_loss": 0.10, "supplement": 0.12, "antimalarial": 0.08, "skin_lightening": 0.10, "other": 0.20}, "sf_prevalence": 0.50, "unregistered_pct": 0.80, "no_prescription_pct": 0.90, "pharmacist_involved_pct": 0.01, "verified_seller_pct": 0.03, }, } SCENARIO_FILES = { "social_media_marketplace": "online_social_media.csv", "dedicated_online_pharmacy": "online_dedicated.csv", "cross_border_ecommerce": "online_cross_border.csv", } def _choice(rng, prob_map): keys = list(prob_map.keys()) weights = np.array(list(prob_map.values()), dtype=float) weights = weights / weights.sum() return rng.choice(keys, p=weights) def _simulate_scenario(name, params, seed): rng = np.random.default_rng(seed) records = [] for idx in range(N_PER_SCENARIO): year = int(rng.choice(YEAR_RANGE, p=YEAR_WEIGHTS)) platform = _choice(rng, params["setting_probs"]) product = _choice(rng, params["product_probs"]) age = int(np.clip(rng.normal(28, 10), 15, 65)) sex = rng.choice(["male", "female"], p=[0.50, 0.50]) no_prescription = int(rng.random() < params["no_prescription_pct"]) self_medication = int(no_prescription and rng.random() < 0.80) pharmacist_involved = int(rng.random() < params["pharmacist_involved_pct"]) verified_seller = int(rng.random() < params["verified_seller_pct"]) # Registration status unregistered = int(rng.random() < params["unregistered_pct"]) nmra_approved = int(not unregistered and rng.random() < 0.60) manufacturer_known = int(rng.random() < 0.40) batch_traceable = int(manufacturer_known and rng.random() < 0.30) # Quality is_sf = int(rng.random() < params["sf_prevalence"]) is_falsified = int(is_sf and rng.random() < 0.50) is_substandard = int(is_sf and not is_falsified) no_active_ingredient = int(is_falsified and rng.random() < 0.30) wrong_ingredient = int(is_falsified and rng.random() < 0.15) contaminated = int(is_sf and rng.random() < 0.10) # Delivery delivery_method = rng.choice(["courier", "postal", "pickup", "rider"], p=[0.30, 0.20, 0.20, 0.30]) cold_chain_needed = int(product in ("vaccine", "ARV") and rng.random() < 0.30) cold_chain_maintained = int(cold_chain_needed and rng.random() < 0.10) delivery_days = int(np.clip(rng.exponential(5), 1, 30)) # Health outcomes adr_occurred = int(is_sf and rng.random() < 0.08) treatment_failure = int(is_sf and rng.random() < 0.12) hospitalisation = int((adr_occurred or treatment_failure) and rng.random() < 0.10) delayed_care = int(self_medication and rng.random() < 0.15) # Regulation & enforcement reported_to_nmra = int(is_sf and rng.random() < 0.02) platform_removed = int(is_sf and rng.random() < 0.05) consumer_aware_risk = int(rng.random() < 0.10) checked_registration = int(rng.random() < 0.05) price_usd = float(np.clip(rng.lognormal(np.log(3), 0.8), 0.20, 100)) any_adverse = int(adr_occurred or treatment_failure or delayed_care) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "platform": platform, "product": product, "age": age, "sex": sex, "no_prescription": no_prescription, "self_medication": self_medication, "pharmacist_involved": pharmacist_involved, "verified_seller": verified_seller, "unregistered": unregistered, "nmra_approved": nmra_approved, "manufacturer_known": manufacturer_known, "batch_traceable": batch_traceable, "is_substandard_falsified": is_sf, "is_falsified": is_falsified, "no_active_ingredient": no_active_ingredient, "wrong_ingredient": wrong_ingredient, "contaminated": contaminated, "delivery_method": delivery_method, "delivery_days": delivery_days, "adr_occurred": adr_occurred, "treatment_failure": treatment_failure, "hospitalisation": hospitalisation, "delayed_care": delayed_care, "reported_to_nmra": reported_to_nmra, "consumer_aware_risk": consumer_aware_risk, "price_usd": round(price_usd, 2), "any_adverse": any_adverse, } records.append(record) return pd.DataFrame(records) def main(): output_dir = Path("data") output_dir.mkdir(parents=True, exist_ok=True) for idx, (name, params) in enumerate(SCENARIOS.items()): df = _simulate_scenario(name, params, SEED + idx * 211) df.to_csv(output_dir / SCENARIO_FILES[name], index=False) print(f"Saved {name} -> {SCENARIO_FILES[name]}") if __name__ == "__main__": main()