"""
RAO STATISTICS — SITDE OBJECTIVE CRITERIA WEIGHTING ENGINE
Standard Deviation, Information Entropy, and Discrepancy Evaluation
Website: https://rao-statistics.vercel.app
"""
import numpy as np
import pandas as pd

def sitde_weighting(matrix, criterion_types):
    """
    matrix: m x n numpy array of alternatives x criteria
    criterion_types: list of '+1' (benefit) or '-1' (cost)
    """
    m, n = matrix.shape
    norm = np.zeros_like(matrix, dtype=float)
    
    # 1. Linear Normalization
    for j in range(n):
        if criterion_types[j] == 1:
            norm[:, j] = (matrix[:, j] - np.min(matrix[:, j])) / (np.max(matrix[:, j]) - np.min(matrix[:, j]) + 1e-12)
        else:
            norm[:, j] = (np.max(matrix[:, j]) - matrix[:, j]) / (np.max(matrix[:, j]) - np.min(matrix[:, j]) + 1e-12)
            
    # 2. Entropy Component
    p = norm / (np.sum(norm, axis=0, keepdims=True) + 1e-12)
    k = 1.0 / np.log(m)
    entropy = -k * np.sum(p * np.log(p + 1e-12), axis=0)
    d_entropy = 1.0 - entropy
    
    # 3. Standard Deviation Dispersion Component
    sd = np.std(norm, axis=0)
    
    # 4. Synthesized SITDE Objective Weights
    combined = d_entropy * sd
    weights = combined / np.sum(combined)
    return weights

if __name__ == "__main__":
    test_matrix = np.array([
        [14.2, 42.0, 18, 94.5],
        [12.8, 68.0, 24, 89.0],
        [15.6, 28.0, 15, 96.0],
        [11.9, 95.0, 30, 83.0],
        [13.5, 52.0, 21, 91.5]
    ])
    types = [-1, -1, -1, 1] # 3 cost criteria, 1 benefit
    w = sitde_weighting(test_matrix, types)
    print("SITDE Weights:", np.round(w, 4))
