# Noisy Functions

A function may have noise, meaning that each evaluation may have a stochastic component, which changes the output of the function slightly each time.

Any non-noisy function can be made noisy by adding small Gaussian random numbers to the input values.

The range for the function below is bounded to -5.0 and 5.0 and the optimal input value is 0.0.

# noisy optimization function

from numpy import arange
from numpy.random import randn
from matplotlib import pyplot

# objective function

def objective(x):
return (x + randn(len(x))*0.3)**2.0

# define range for input

r_min, r_max = -5.0, 5.0

# sample input range uniformly at 0.1 increments

inputs = arange(r_min, r_max, 0.1)

# compute targets

results = objective(inputs)

# create a line plot of input vs result

pyplot.plot(inputs, results)

x_optima = 0.0

# draw a vertical line at the optimal input

pyplot.axvline(x=x_optima, ls=’–’, color=‘red’)

pyplot.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# noisy optimization function

from numpy import arange
from numpy.random import randn
from matplotlib import pyplot

# objective function

def objective(x):
return (x + randn(len(x))*0.3)**2.0

# define range for input

r_min, r_max = -5.0, 5.0

# sample input range uniformly at 0.1 increments

inputs = arange(r_min, r_max, 0.1)

# compute targets

results = objective(inputs)

# create a line plot of input vs result

pyplot.plot(inputs, results)

x_optima = 0.0

# draw a vertical line at the optimal input

pyplot.axvline(x=x_optima, ls=’–’, color=‘red’)

# show the plot

pyplot.show()
Running the example creates a line plot of the function and marks the optima with a red line.

Line Plot of Noisy Optimization Function