1

Currently, I have some code written that finds the combination of parameters that maximizes the log-likelihood function with some field data. The model now randomly selects the parameters out of a uniform distribution and then selects the best parameter set. This method however doesn't give reproducible results. I am wondering if anyone has any sample codes for how to update this code to run the mini-batch gradient descent (or ascent) method to find the best parameter set?

Also, the model now uses all of the field data at once, whereas I want small batches of the data to be tested at each iteration. Below is my code:

from math import *
import numpy as np
import random
import matplotlib.pyplot as plt

def ff(xs,vx,Dx,XSpill,sigmax0,SP):
    mux = XSpill + vx*SP
    sigmax = sigmax0 + np.sqrt(2.0*Dx*SP)
    return (1.0/(np.sqrt(2.0*np.pi*(sigmax**2.0))))*np.exp((-(x-mux)**2.0)/(2.0*(sigmax**2.0)))

def likelihood(xs,XSpill,ConData,sigmax0,SP):
    vx1 = [random.uniform(-50.0,50.0) for i in range(1000)]
    Dx1 = [random.uniform(0.01,20.1) for i in range(1000)]

    IniIndLikelihood = np.ones([len(xs),len(vx1)])
    Lamda = np.zeros(shape=(len(vx1)))
    Prob = np.zeros(shape=(len(vx1)))
    for ci in range(len(xs)):
        if ConData[ci] > 0.0:
            for i in range(len(vx1)):
            Prob[i] = ff(xs[ci],vx1[i],Dx1[i],XSpill,sigmax0,SP)
            if Prob[i] > 1e-308:
                Lamda[i] = 1/Prob[i]
                IniIndLikelihood[ci,i] = Lamda[i]*np.exp(-Lamda[i]*ConData[ci])
            else:
                Lamda[i] = 0.0
                IniIndLikelihood[ci,i] = 0.0

    CompLikelihood = np.ones([len(vx1)])
    Likelihood = np.zeros([len(vx1)])
    for i in range(len(vx1)):
        for ci in range(len(xs)):
            if ConData[ci] > 0.0:
                if IniIndLikelihood[ci,i] == 0.0:
                    CompLikelihood[i] = 0.0

    MaxLogLike = -22.0
    for i in range(len(vx1)):
        for ci in range(len(xs)):
            if ConData[ci] > 0.0:
                if CompLikelihood[i] == 1.0:
                    Likelihood[i] = Likelihood[i] + np.log(IniIndLikelihood[ci,i])

        if CompLikelihood[i] == 1.0:
            if MaxLogLike == -22.0:
                MaxLogLike = Likelihood[i]
            else:
                MaxLogLike = np.max([MaxLogLike,Likelihood[i]])

    for i in range(len(vx1)):
        if CompLikelihood[i] == 1.0:
            Likelihood[i] = Likelihood[i] - MaxLogLike

    return Likelihood

if __name__ == "__main__":

    sigmax0 = 0.0
    XSpill = 0.0
    SP = 1.0
    xs = [1,3,5,9,20,34,40,60]
    ConData = np.array([5,7,30,5,5,15,30,5])/100

    Result = likelihood(xs,XSpill,ConData,sigmax0,SP)

Where xs is the location and ConData is the concentration of the field data. Then after getting the probabilities of the log likelihood, I use argmax to find the best parameter combination.

Any suggestions, links, or sample codes would be helpful since I am having trouble finding Python examples for MLE method!

mj1496
  • 61
  • 1
  • 9

0 Answers0