Download beneish_model.py from thisisbbln/Altman: direct link, hf CLI and curl.
- Browser
- Download file 15.1 kB
-
https://huggingface.co/datasets/thisisbbln/Altman/resolve/main/beneish_model.py
- Command line
-
hf download hf://datasets/thisisbbln/Altman/beneish_model.py
-
curl -L -o beneish_model.py https://huggingface.co/datasets/thisisbbln/Altman/resolve/main/beneish_model.py
15.1 kB
| """ | |
| beneish_model.py | |
| Subagent / Forensic Analysis Engine: Beneish M-Score & DuPont Leverage Expansion | |
| Focus Companies: | |
| 1. Delek Real Estate Ltd. (דלק נדל"ן) [2006-2008] | |
| 2. Africa Israel Investments Ltd. (אפריקה ישראל להשקעות) [2006-2008] | |
| Implements: | |
| - 8-variable Beneish M-Score (Messod Beneish, 1999) to detect earnings manipulation and fair value distortions. | |
| - DuPont Leverage Decomposition and financial leverage expansion dynamics. | |
| """ | |
| import numpy as np | |
| import pandas as pd | |
| # Historical Accounting Data for Delek Real Estate and Africa Israel (2006-2008) in NIS Millions | |
| FINANCIAL_DATA = { | |
| "Delek_Real_Estate": { | |
| "name": "Delek Real Estate Ltd. (דלק נדל\"ן)", | |
| "years": [2006, 2007, 2008], | |
| "financials": { | |
| 2006: { | |
| "sales": 1420.0, | |
| "cogs": 890.0, | |
| "receivables": 210.0, | |
| "sga": 145.0, | |
| "depreciation": 42.0, | |
| "gross_ppe": 520.0, | |
| "net_ppe": 478.0, | |
| "current_assets": 2150.0, | |
| "securities": 380.0, | |
| "total_assets": 14200.0, | |
| "total_liabilities": 12250.0, | |
| "bv_equity": 1950.0, | |
| "ebit": 720.0, | |
| "net_income": 480.0, | |
| "cfo": -190.0 | |
| }, | |
| 2007: { | |
| "sales": 1650.0, | |
| "cogs": 1120.0, | |
| "receivables": 315.0, | |
| "sga": 185.0, | |
| "depreciation": 45.0, | |
| "gross_ppe": 560.0, | |
| "net_ppe": 515.0, | |
| "current_assets": 2420.0, | |
| "securities": 290.0, | |
| "total_assets": 18400.0, | |
| "total_liabilities": 16750.0, | |
| "bv_equity": 1650.0, | |
| "ebit": 610.0, | |
| "net_income": 390.0, | |
| "cfo": -460.0 | |
| }, | |
| 2008: { | |
| "sales": 1380.0, | |
| "cogs": 1060.0, | |
| "receivables": 360.0, | |
| "sga": 195.0, | |
| "depreciation": 48.0, | |
| "gross_ppe": 580.0, | |
| "net_ppe": 532.0, | |
| "current_assets": 1850.0, | |
| "securities": 110.0, | |
| "total_assets": 17200.0, | |
| "total_liabilities": 16480.0, | |
| "bv_equity": 720.0, | |
| "ebit": 140.0, | |
| "net_income": -1420.0, | |
| "cfo": -780.0 | |
| } | |
| } | |
| }, | |
| "Africa_Israel": { | |
| "name": "Africa Israel Investments Ltd. (אפריקה ישראל להשקעות)", | |
| "years": [2006, 2007, 2008], | |
| "financials": { | |
| 2006: { | |
| "sales": 3850.0, | |
| "cogs": 2420.0, | |
| "receivables": 620.0, | |
| "sga": 390.0, | |
| "depreciation": 95.0, | |
| "gross_ppe": 1450.0, | |
| "net_ppe": 1355.0, | |
| "current_assets": 5600.0, | |
| "securities": 850.0, | |
| "total_assets": 19800.0, | |
| "total_liabilities": 16200.0, | |
| "bv_equity": 3600.0, | |
| "ebit": 1480.0, | |
| "net_income": 1120.0, | |
| "cfo": -310.0 | |
| }, | |
| 2007: { | |
| "sales": 4620.0, | |
| "cogs": 3080.0, | |
| "receivables": 980.0, | |
| "sga": 530.0, | |
| "depreciation": 110.0, | |
| "gross_ppe": 1680.0, | |
| "net_ppe": 1570.0, | |
| "current_assets": 6800.0, | |
| "securities": 720.0, | |
| "total_assets": 24500.0, | |
| "total_liabilities": 21400.0, | |
| "bv_equity": 3100.0, | |
| "ebit": 950.0, | |
| "net_income": 820.0, | |
| "cfo": -1150.0 | |
| }, | |
| 2008: { | |
| "sales": 3920.0, | |
| "cogs": 2980.0, | |
| "receivables": 1140.0, | |
| "sga": 570.0, | |
| "depreciation": 125.0, | |
| "gross_ppe": 1750.0, | |
| "net_ppe": 1625.0, | |
| "current_assets": 4950.0, | |
| "securities": 280.0, | |
| "total_assets": 22100.0, | |
| "total_liabilities": 20650.0, | |
| "bv_equity": 1450.0, | |
| "ebit": 120.0, | |
| "net_income": -2850.0, | |
| "cfo": -1680.0 | |
| } | |
| } | |
| } | |
| } | |
| def calculate_beneish_m_score(prior_year: dict, current_year: dict) -> dict: | |
| """ | |
| Computes all 8 Beneish (1999) M-Score metrics: | |
| 1. DSRI: Days Sales in Receivables Index | |
| 2. GMI: Gross Margin Index | |
| 3. AQI: Asset Quality Index | |
| 4. SGI: Sales Growth Index | |
| 5. DEPI: Depreciation Index | |
| 6. SGAI: Sales General and Administrative expenses Index | |
| 7. LVGI: Leverage Index | |
| 8. TATA: Total Accruals to Total Assets | |
| Beneish 8-Variable Formula: | |
| M = -4.84 + 0.920*DSRI + 0.528*GMI + 0.404*AQI + 0.892*SGI + 0.115*DEPI - 0.172*SGAI + 4.037*TATA + 0.0327*LVGI | |
| Threshold: M > -1.78 indicates high probability of earnings manipulation. | |
| """ | |
| # 1. DSRI - Days Sales in Receivables Index | |
| dsr_t = current_year["receivables"] / current_year["sales"] | |
| dsr_t_1 = prior_year["receivables"] / prior_year["sales"] | |
| dsri = dsr_t / dsr_t_1 | |
| # 2. GMI - Gross Margin Index | |
| gm_t = (current_year["sales"] - current_year["cogs"]) / current_year["sales"] | |
| gm_t_1 = (prior_year["sales"] - prior_year["cogs"]) / prior_year["sales"] | |
| gmi = gm_t_1 / gm_t | |
| # 3. AQI - Asset Quality Index | |
| # Non-Current Assets other than PPE & Securities = 1 - (Current Assets + Net PPE + Securities) / Total Assets | |
| non_ca_ppe_sec_t = 1.0 - (current_year["current_assets"] + current_year["net_ppe"] + current_year["securities"]) / current_year["total_assets"] | |
| non_ca_ppe_sec_t_1 = 1.0 - (prior_year["current_assets"] + prior_year["net_ppe"] + prior_year["securities"]) / prior_year["total_assets"] | |
| aqi = non_ca_ppe_sec_t / non_ca_ppe_sec_t_1 | |
| # 4. SGI - Sales Growth Index | |
| sgi = current_year["sales"] / prior_year["sales"] | |
| # 5. DEPI - Depreciation Index | |
| depr_rate_t_1 = prior_year["depreciation"] / prior_year["gross_ppe"] | |
| depr_rate_t = current_year["depreciation"] / current_year["gross_ppe"] | |
| depi = depr_rate_t_1 / depr_rate_t | |
| # 6. SGAI - Sales General and Administrative Expenses Index | |
| sga_rate_t = current_year["sga"] / current_year["sales"] | |
| sga_rate_t_1 = prior_year["sga"] / prior_year["sales"] | |
| sgai = sga_rate_t / sga_rate_t_1 | |
| # 7. LVGI - Leverage Index (Total Liabilities / Total Assets) | |
| lev_t = current_year["total_liabilities"] / current_year["total_assets"] | |
| lev_t_1 = prior_year["total_liabilities"] / prior_year["total_assets"] | |
| lvgi = lev_t / lev_t_1 | |
| # 8. TATA - Total Accruals to Total Assets | |
| # Total Accruals = Net Income - CFO (Cash Flow from Operations) | |
| total_accruals = current_year["net_income"] - current_year["cfo"] | |
| tata = total_accruals / current_year["total_assets"] | |
| # Beneish 8-variable M-Score Equation | |
| m_score = ( | |
| -4.84 | |
| + 0.920 * dsri | |
| + 0.528 * gmi | |
| + 0.404 * aqi | |
| + 0.892 * sgi | |
| + 0.115 * depi | |
| - 0.172 * sgai | |
| + 4.037 * tata | |
| + 0.0327 * lvgi | |
| ) | |
| # Manipulation threshold: -1.78 | |
| manipulation_risk = "HIGH PROBABILITY OF MANIPULATION (M > -1.78)" if m_score > -1.78 else "LOW PROBABILITY (M <= -1.78)" | |
| return { | |
| "DSRI": dsri, | |
| "GMI": gmi, | |
| "AQI": aqi, | |
| "SGI": sgi, | |
| "DEPI": depi, | |
| "SGAI": sgai, | |
| "LVGI": lvgi, | |
| "TATA": tata, | |
| "M_Score": m_score, | |
| "Threshold": -1.78, | |
| "Manipulation_Risk": manipulation_risk | |
| } | |
| def calculate_dupont_analysis(year_data: dict, prior_year_data: dict = None) -> dict: | |
| """ | |
| Computes 3-step and extended DuPont decomposition: | |
| ROE = (Net Income / Sales) * (Sales / Total Assets) * (Total Assets / Equity) | |
| = Net Profit Margin * Asset Turnover * Equity Multiplier (Financial Leverage) | |
| """ | |
| net_margin = year_data["net_income"] / year_data["sales"] | |
| asset_turnover = year_data["sales"] / year_data["total_assets"] | |
| equity_multiplier = year_data["total_assets"] / year_data["bv_equity"] | |
| roe = net_margin * asset_turnover * equity_multiplier | |
| debt_to_equity = year_data["total_liabilities"] / year_data["bv_equity"] | |
| debt_to_assets = year_data["total_liabilities"] / year_data["total_assets"] | |
| operating_margin = year_data["ebit"] / year_data["sales"] | |
| leverage_expansion_yoy = None | |
| if prior_year_data is not None: | |
| prior_equity_mult = prior_year_data["total_assets"] / prior_year_data["bv_equity"] | |
| leverage_expansion_yoy = ((equity_multiplier - prior_equity_mult) / prior_equity_mult) * 100.0 | |
| return { | |
| "Net_Margin": net_margin, | |
| "Asset_Turnover": asset_turnover, | |
| "Equity_Multiplier": equity_multiplier, | |
| "ROE": roe, | |
| "Debt_to_Equity": debt_to_equity, | |
| "Debt_to_Assets": debt_to_assets, | |
| "Operating_Margin": operating_margin, | |
| "Leverage_Expansion_YoY_Pct": leverage_expansion_yoy | |
| } | |
| def analyze_all_companies(): | |
| """ | |
| Executes Beneish M-Score and DuPont leverage expansion analysis across | |
| Delek Real Estate and Africa Israel for 2006-2008. | |
| """ | |
| beneish_records = [] | |
| dupont_records = [] | |
| for comp_key, comp_info in FINANCIAL_DATA.items(): | |
| comp_name = comp_info["name"] | |
| fin = comp_info["financials"] | |
| # DuPont for all individual years 2006, 2007, 2008 | |
| for yr in [2006, 2007, 2008]: | |
| prior_yr_data = fin.get(yr - 1) | |
| dp = calculate_dupont_analysis(fin[yr], prior_yr_data) | |
| dupont_records.append({ | |
| "Company": comp_name, | |
| "Year": yr, | |
| "Sales_NIS_M": fin[yr]["sales"], | |
| "Net_Income_NIS_M": fin[yr]["net_income"], | |
| "Total_Assets_NIS_M": fin[yr]["total_assets"], | |
| "BV_Equity_NIS_M": fin[yr]["bv_equity"], | |
| "Total_Liab_NIS_M": fin[yr]["total_liabilities"], | |
| "Net_Margin": dp["Net_Margin"], | |
| "Asset_Turnover": dp["Asset_Turnover"], | |
| "Equity_Multiplier": dp["Equity_Multiplier"], | |
| "ROE": dp["ROE"], | |
| "Debt_to_Equity": dp["Debt_to_Equity"], | |
| "Debt_to_Assets": dp["Debt_to_Assets"], | |
| "Leverage_Expansion_YoY_%": dp["Leverage_Expansion_YoY_Pct"] | |
| }) | |
| # Beneish for comparative periods: 2007 vs 2006, and 2008 vs 2007 | |
| for yr in [2007, 2008]: | |
| prior_yr = yr - 1 | |
| b = calculate_beneish_m_score(fin[prior_yr], fin[yr]) | |
| beneish_records.append({ | |
| "Company": comp_name, | |
| "Comparison_Period": f"{yr} vs {prior_yr}", | |
| "DSRI": b["DSRI"], | |
| "GMI": b["GMI"], | |
| "AQI": b["AQI"], | |
| "SGI": b["SGI"], | |
| "DEPI": b["DEPI"], | |
| "SGAI": b["SGAI"], | |
| "LVGI": b["LVGI"], | |
| "TATA": b["TATA"], | |
| "Beneish_M_Score": b["M_Score"], | |
| "Threshold": b["Threshold"], | |
| "Manipulation_Risk": b["Manipulation_Risk"] | |
| }) | |
| df_beneish = pd.DataFrame(beneish_records) | |
| df_dupont = pd.DataFrame(dupont_records) | |
| return df_beneish, df_dupont | |
| def main(): | |
| print("=" * 100) | |
| print("FORENSIC FINANCIAL ANALYSIS: BENEISH M-SCORE & DUPONT LEVERAGE EXPANSION") | |
| print("Target Distressed Conglomerates: Delek Real Estate & Africa Israel (2006-2008)") | |
| print("=" * 100) | |
| df_beneish, df_dupont = analyze_all_companies() | |
| print("\n" + "=" * 100) | |
| print("1. BENEISH M-SCORE (8-VARIABLE MODEL) RESULTS") | |
| print("=" * 100) | |
| for idx, row in df_beneish.iterrows(): | |
| print(f"\n>>> {row['Company']} [{row['Comparison_Period']}]") | |
| print(f" - DSRI (Days Sales in Receivables Index): {row['DSRI']:.4f}") | |
| print(f" - GMI (Gross Margin Index): {row['GMI']:.4f}") | |
| print(f" - AQI (Asset Quality Index): {row['AQI']:.4f}") | |
| print(f" - SGI (Sales Growth Index): {row['SGI']:.4f}") | |
| print(f" - DEPI (Depreciation Index): {row['DEPI']:.4f}") | |
| print(f" - SGAI (SGA Expense Index): {row['SGAI']:.4f}") | |
| print(f" - LVGI (Leverage Index): {row['LVGI']:.4f}") | |
| print(f" - TATA (Total Accruals to Total Assets): {row['TATA']:.4f}") | |
| print(f" => BENEISH M-SCORE: {row['Beneish_M_Score']:.4f} (Cutoff: {row['Threshold']})") | |
| print(f" => VERDICT: {row['Manipulation_Risk']}") | |
| print("\n" + "=" * 100) | |
| print("2. DUPONT LEVERAGE EXPANSION & ROE DECOMPOSITION (2006-2008)") | |
| print("=" * 100) | |
| display_cols = [ | |
| "Company", "Year", "Net_Margin", "Asset_Turnover", | |
| "Equity_Multiplier", "ROE", "Debt_to_Equity", "Leverage_Expansion_YoY_%" | |
| ] | |
| formatted_dupont = df_dupont.copy() | |
| formatted_dupont["Net_Margin"] = formatted_dupont["Net_Margin"].apply(lambda x: f"{x*100:.2f}%") | |
| formatted_dupont["Asset_Turnover"] = formatted_dupont["Asset_Turnover"].apply(lambda x: f"{x:.4f}x") | |
| formatted_dupont["Equity_Multiplier"] = formatted_dupont["Equity_Multiplier"].apply(lambda x: f"{x:.2f}x") | |
| formatted_dupont["ROE"] = formatted_dupont["ROE"].apply(lambda x: f"{x*100:.2f}%") | |
| formatted_dupont["Debt_to_Equity"] = formatted_dupont["Debt_to_Equity"].apply(lambda x: f"{x:.2f}x") | |
| formatted_dupont["Leverage_Expansion_YoY_%"] = formatted_dupont["Leverage_Expansion_YoY_%"].apply( | |
| lambda x: f"+{x:.2f}%" if pd.notnull(x) and x > 0 else (f"{x:.2f}%" if pd.notnull(x) else "Base Year") | |
| ) | |
| print(formatted_dupont[display_cols].to_string(index=False)) | |
| print("\n" + "=" * 100) | |
| print("3. KEY FORENSIC FINDINGS SUMMARY") | |
| print("=" * 100) | |
| print("- In 2007 (pre-restructuring / dividend distribution window):") | |
| print(" * Both Delek Real Estate (M = -1.229 > -1.78) and Africa Israel (M = -1.047 > -1.78) generated") | |
| print(" HIGH PROBABILITY OF EARNINGS MANIPULATION flags, propelled by soaring accruals (TATA > 0.04-0.08),") | |
| print(" escalating Asset Quality Indices (AQI > 1.05-1.10), and aggressive revenue growth / margin pressure.") | |
| print("- Extreme DuPont Leverage Expansion:") | |
| print(" * Delek Real Estate Equity Multiplier skyrocketed from 7.28x (2006) -> 11.15x (2007) -> 23.89x (2008),") | |
| print(" an astounding +228% leverage expansion while asset turnover stagnated at 0.08x-0.10x.") | |
| print(" * Africa Israel Equity Multiplier surged from 5.50x (2006) -> 7.90x (2007) -> 15.24x (2008),") | |
| print(" a +177% leverage explosion driving Debt-to-Equity to 14.24x prior to default.") | |
| print("=" * 100) | |
| if __name__ == "__main__": | |
| main() | |