def findDirichletPriors(ss, initAlphas, max_iter=1000, delta=0.01):
    priors = initAlphas
    currentLoss = getTotalLoss(priors, ss, delta)
    gradientToleranceSq = (2 ** (- 20))
    learnRateTolerance = (2 ** (- 10))
    count = 0
    while (count < max_iter):
        count += 1
        gradient = getGradientForMultinomials(priors, ss, delta)
        gradientSize = sqVectorSize(gradient)
        if (gradientSize < gradientToleranceSq):
            return priors
        trialStep = predictStepUsingHessian(gradient, priors, ss, delta)
        trialPriors = ([0] * len(priors))
        for i in range(0, len(priors)):
            trialPriors[i] = (priors[i] + trialStep[i])
        loss = testTrialPriors(trialPriors, ss, delta)
        if (loss < currentLoss):
            currentLoss = loss
            priors = trialPriors
            continue
        trialStep = predictStepLogSpace(gradient, priors, ss, delta)
        trialPriors = ([0] * len(priors))
        for i in range(0, len(priors)):
            trialPriors[i] = (priors[i] * math.exp(trialStep[i]))
        loss = testTrialPriors(trialPriors, ss, delta)
        loss = 10000000
        learnRate = 1.0
        while (loss > currentLoss):
            learnRate *= 0.9
            trialPriors = ([0] * len(priors))
            for i in range(0, len(priors)):
                trialPriors[i] = (priors[i] + (gradient[i] * learnRate))
            loss = testTrialPriors(trialPriors, ss, delta)
        if (learnRate < learnRateTolerance):
            return priors
        currentLoss = loss
        priors = trialPriors
    return priors
def findDirichletPriors(ss, initAlphas, max_iter=1000, delta=0.01):
    priors = initAlphas
    currentLoss = getTotalLoss(priors, ss, delta)
    gradientToleranceSq = (2 ** (- 20))
    learnRateTolerance = (2 ** (- 10))
    count = 0
    while (count < max_iter):
        count += 1
        gradient = getGradientForMultinomials(priors, ss, delta)
        gradientSize = sqVectorSize(gradient)
        if (gradientSize < gradientToleranceSq):
            return priors
        trialStep = predictStepUsingHessian(gradient, priors, ss, delta)
        trialPriors = ([0] * len(priors))
        for i in range(0, len(priors)):
            trialPriors[i] = (priors[i] + trialStep[i])
        loss = testTrialPriors(trialPriors, ss, delta)
        if (loss < currentLoss):
            currentLoss = loss
            priors = trialPriors
            continue
        trialStep = predictStepLogSpace(gradient, priors, ss, delta)
        trialPriors = ([0] * len(priors))
        for i in range(0, len(priors)):
            trialPriors[i] = (priors[i] * math.exp(trialStep[i]))
        loss = testTrialPriors(trialPriors, ss, delta)
        loss = 10000000
        learnRate = 1.0
        while (loss > currentLoss):
            learnRate *= 0.9
            trialPriors = ([0] * len(priors))
            for i in range(0, len(priors)):
                trialPriors[i] = (priors[i] + (gradient[i] * learnRate))
            loss = testTrialPriors(trialPriors, ss, delta)
        if (learnRate < learnRateTolerance):
            return priors
        currentLoss = loss
        priors = trialPriors
    return priors