1

I am trying to do Monte Carlo minimization to solve for parameters of a given equation. My equation has 4 parameters, making my iteration about 4**n

when I try iteration n = 100, I saw it is not a good idea to search all the parameter space.

Here is my code:

import sys
import numpy as np
#import matplotlib.pyplot as plt
#import pandas as pd
import random


#method returns sum square for given parameter m and c
def currentFunc(x,alpha1,alpha2,alpha3,alpha4):
    term = -(x/alpha4)
    term_Norm = term
    expoterm = np.exp(term_Norm)
    expoterm = np.exp(term_Norm)
    #print('check term: x: %0.10f %0.10f exp: %0.10f' % (x,term_Norm,expoterm) )
    return(-alpha1*( (alpha2/(alpha3+ expoterm )) - 1))


def sumsquarecurr(x,y,a1,a2,a3,a4):
    xsize = len(x)
    ysize = len(y)
    sumsqdiff = 0
    if(xsize != ysize):
        print("check your X and Y length exiting ...")
        sys.exit(0)
    for i in range(ysize):
        diff = y[i] - currentFunc(x[i],a1,a2,a3,a4)
        sumsqdiff+=diff*diff
    return sumsqdiff
# number of random number (this affects the accuracy of the Monte Carlo method
n = 10
a_rnad = []
b_rnad = []
c_rnad = []
d_rnad = []
for i in range(n):
    #random.seed(555)
    xtemp = random.uniform(0.0, 2.0)
    print('check %.4f ' % (xtemp))
    a_rnad.append(xtemp)
    b_rnad.append(xtemp)
    c_rnad.append(xtemp)
    d_rnad.append(xtemp)


Yfit=[-7,-5,-3,-1,1,3,5,7]
Xfit=[8.077448e-07,6.221196e-07,4.231292e-07,1.710039e-07,-4.313762e-05,-8.248818e-05,-1.017410e-04,-1.087409e-04]
# placeholder for the parameters and the minimun sum squared
#[alpha1,alpha2,alpha3,alpha4,min]
minparam = [0,0,0,0,99999999999.0]
for j in range(len(a_rnad)):
    for i in range(len(b_rnad)):
        for k in range(len(c_rnad)):
            for m in range(len(d_rnad)):
                minsumsqdiff_temp =sumsquarecurr(Xfit,Yfit,a_rnad[j],b_rnad[i],c_rnad[k],d_rnad[m])
                print('alpha1: %.4f alpha2: %.4f alpha3: %.4f alpha4: %.4f min: %0.4f' % (a_rnad[j],b_rnad[i],c_rnad[k],d_rnad[m],minsumsqdiff_temp))
                if(minsumsqdiff_temp<minparam[4]):
                    minparam[0] = a_rnad[j]
                    minparam[1] = b_rnad[i]
                    minparam[2] = c_rnad[k]
                    minparam[3] = d_rnad[m]
                    minparam[4] = minsumsqdiff_temp

print('minimazation: alpha1: %.4f alpha2: %.4f alpha3: %.4f alpha4: %.4f min: %0.4f' % (minparam[0],minparam[1],minparam[2],minparam[3],minparam[4]))

Question:

  1. is there a way to make this algorithm run faster (either by cutting the search/phase space down)?

  2. I feel I am reinventing the wheel. please does anyone know a python module that can do what I am trying to do?

Thanks in advance for your help

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
kayT
  • 11
  • 3
  • 1
    an off-topic bit of code review: `for i in is: for j in js: for k in ks: for l in ls: foo(i, j, k, l)` is better-written using `itertools.product`. `for i, j, k, l in itertools.product(is, js, ks, ls): foo(i, j, k, l)`. Additionally, string interpolation (`"doing %s using the percent sign and a %s" % ("this", "tuple")`) has long been abandoned for `str.format` (`"Which {0} more like {1} instead. {1} {0} more explicit anyway.".format("looks", "this")`) – Adam Smith Jul 12 '18 at 22:07
  • [A quick course on Monte Carlo in Python, step by step](https://pythonprogramming.net/monte-carlo-simulator-python/) – Mast Jul 13 '18 at 05:36

0 Answers0