Balancing the Scales: A Theoretical and Algorithmic Framework for Learning from Imbalanced Data