0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

簿記のためのPython数学

Posted at
# accounting_analysis_toolkit.py

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

# --- 原価率・利益率・マークアップ率 / Cost, Profit, Markup Ratios ---
def cost_profit_markup(sales, cost):
    profit = sales - cost
    cost_ratio = cost / sales * 100
    profit_ratio = profit / sales * 100
    markup_ratio = profit / cost * 100
    return cost_ratio, profit_ratio, markup_ratio

# --- 歩留率・減損率・仕損率 / Yield, Scrap, Defect Rates ---
def yield_scrap_defect(input_qty, good_qty, defect_qty):
    scrap_qty = input_qty - good_qty - defect_qty
    yield_rate = good_qty / input_qty * 100
    scrap_rate = scrap_qty / input_qty * 100
    defect_rate = defect_qty / input_qty * 100
    return yield_rate, scrap_rate, defect_rate

# --- 市場占有率・構成比率 / Market Share & Product Mix ---
def market_share(company_sales, industry_sales):
    return company_sales / industry_sales * 100

def product_mix(product_sales, total_sales):
    return product_sales / total_sales * 100

# --- 加重平均資本コスト率 / Weighted Average Cost of Capital ---
def WACC(weights, costs):
    return np.dot(weights, costs)

# --- 固変分解(一次関数)/ Linear Cost Equation ---
def linear_cost(a, b, x):
    return a + b * x

# --- CVP 損益分岐点分析 / Break-even Point ---
def break_even(fixed_cost, price, variable_cost):
    return fixed_cost / (price - variable_cost)

# --- 最小二乗法 / Least Squares Method ---
def least_squares(x, y):
    b = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
    a = y.mean() - b * x.mean()
    return a, b

# --- 一般的な連立方程式 / Solve Simple Simultaneous Equations ---
def solve_equations():
    def equations(p):
        x, y = p
        return (x + 0.2 * y - 100, 0.1 * x + y - 200)
    return fsolve(equations, (0, 0))

# --- IRR 補間法 / IRR Interpolation ---
def irr_interpolation(r1, r2, npv1, npv2):
    return r1 + (npv1 / (npv1 - npv2)) * (r2 - r1)

# --- 現在価値 / Present Value ---
def present_value(future_value, r, n):
    return future_value / ((1 + r) ** n)

# --- 償却原価法(利息法)/ Amortized Cost Method ---
def amortized_cost(book_value, interest_rate, repayment):
    interest = book_value * interest_rate
    return book_value + interest - repayment

# --- 線形計画法(単純な手動評価)/ Simple Linear Programming ---
def linear_programming(c1, c2, a1, a2, b1):
    x_vals = np.linspace(0, b1 / a1, 100)
    y_vals = (b1 - a1 * x_vals) / a2
    z_vals = c1 * x_vals + c2 * y_vals
    idx = np.argmax(z_vals)
    return x_vals[idx], y_vals[idx], z_vals[idx]

# --- 補助部門費配賦(連立方程式)/ Departmental Cost Allocation ---
def accounting_allocations():
    def equations(p):
        x, y = p
        eq1 = x - (40000 + 0.2 * y)
        eq2 = y - (20000 + 0.3 * x)
        return (eq1, eq2)
    return fsolve(equations, (0, 0))

# --- 最小二乗法デモグラフ / Least Squares Demo Plot ---
def plot_least_squares_demo():
    x_demo = np.array([1, 2, 3, 4, 5])
    y_demo = np.array([2, 4.2, 5.9, 8.1, 10.2])
    a, b = least_squares(x_demo, y_demo)

    plt.scatter(x_demo, y_demo, label="Data")
    plt.plot(x_demo, a + b * x_demo, color='red', label="Least Squares Fit")
    plt.title("Least Squares Regression")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.grid(True)
    plt.show()
    return a, b

# --- メイン処理 / Main Execution ---
if __name__ == "__main__":
    # 各種計算を実行して結果を表示 / Run all functions with sample values

    print("📊 原価・利益分析 / Cost and Profit Analysis")
    cr, pr, mr = cost_profit_markup(1000, 700)
    print(f"Cost Ratio: {cr:.2f}%, Profit Ratio: {pr:.2f}%, Markup Ratio: {mr:.2f}%\n")

    print("📦 歩留まり計算 / Yield Analysis")
    yld, scr, dft = yield_scrap_defect(1000, 850, 100)
    print(f"Yield Rate: {yld:.2f}%, Scrap Rate: {scr:.2f}%, Defect Rate: {dft:.2f}%\n")

    print("📈 市場占有率・構成比率 / Market Share & Mix")
    print(f"Market Share: {market_share(500, 2000):.2f}%")
    print(f"Product Mix: {product_mix(300, 1200):.2f}%\n")

    print("🏦 WACC")
    print(f"WACC: {WACC(np.array([0.6, 0.4]), np.array([0.08, 0.05])):.4f}\n")

    print("💸 固変分解")
    print(f"Total Cost: {linear_cost(500, 20, 30):.2f}\n")

    print("📉 CVP損益分岐点")
    print(f"Break-even Point: {break_even(1000, 100, 60):.2f} units\n")

    print("🧮 最小二乗法回帰")
    a, b = plot_least_squares_demo()
    print(f"Regression Line: y = {a:.2f} + {b:.2f}x\n")

    print("🔁 連立方程式")
    x, y = solve_equations()
    print(f"Solution: x = {x:.2f}, y = {y:.2f}\n")

    print("📉 IRR補間")
    print(f"IRR: {irr_interpolation(0.08, 0.10, 500, -200):.4f}\n")

    print("💰 現在価値")
    print(f"Present Value: {present_value(10000, 0.05, 3):.2f}\n")

    print("📘 償却原価")
    print(f"New Book Value: {amortized_cost(1000, 0.06, 300):.2f}\n")

    print("🧮 線形計画法")
    x_opt, y_opt, z_opt = linear_programming(30, 20, 1, 2, 40)
    print(f"Optimal Solution: x = {x_opt:.2f}, y = {y_opt:.2f}, Z = {z_opt:.2f}\n")

    print("📊 補助部門配賦")
    x_dep, y_dep = accounting_allocations()
    print(f"Final Allocations: Dept A = {x_dep:.2f}, Dept B = {y_dep:.2f}")

結果

📊 原価・利益分析 / Cost and Profit Analysis
Cost Ratio: 70.00%, Profit Ratio: 30.00%, Markup Ratio: 42.86%

📦 歩留まり計算 / Yield Analysis
Yield Rate: 85.00%, Scrap Rate: 5.00%, Defect Rate: 10.00%

📈 市場占有率・構成比率 / Market Share & Mix
Market Share: 25.00%
Product Mix: 25.00%

🏦 WACC
WACC: 0.0680

💸 固変分解
Total Cost: 1100.00

📉 CVP損益分岐点
Break-even Point: 25.00 units

🧮 最小二乗法回帰

Regression Line: y = -0.01 + 2.03x

🔁 連立方程式
Solution: x = 61.22, y = 193.88

📉 IRR補間
IRR: 0.0943

💰 現在価値
Present Value: 8638.38

📘 償却原価
New Book Value: 760.00

🧮 線形計画法
Optimal Solution: x = 40.00, y = 0.00, Z = 1200.00

📊 補助部門配賦
Final Allocations: Dept A = 46808.51, Dept B = 34042.55

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?