I want to generate a toy example to illustrate a convex piecewise linear function in python, but I couldn't figure out the best way to do this. What I want to do is to indicate the number of lines and generate the function randomly.
A convex piecewise-linear function is defined as:
For instance, if I want to have four linear lines, then I want to generate something as shown below.
Since there are four lines. I need to generate four increasing random integers to determine the intervals in x-axis.
import random
import numpy as np
random.seed(1)
x_points = np.array(random.sample(range(1, 20), 4))
x_points.sort()
x_points = np.append(0, x_points)
x_points
[0 3 4 5 9]
I can now use the first two points and create a random linear function, but I don't know how I should continue from there to preserve the convexity. Note that a function is called convex if the line segment between any two points on the graph of the function does not lie below the graph between the two points.
The slope increases monotonously by a random value from the range [0,1), starting from 0. The first y value is also zero, see the comments.
import numpy as np
np.random.seed(0)
x_points = np.random.randint(low=1, high=20, size=4)
x_points.sort()
x_points = np.append(0, x_points) # the first 0 point is 0
slopes = np.add.accumulate(np.random.random(size=3))
slopes = np.append(0,slopes) # the first slope is 0
y_incr = np.ediff1d(x_points)*slopes
y_points = np.add.accumulate(y_incr)
y_points = np.append(0,y_points) # the first y values is 0
A possible output looks like this:
print(x_points)
print(y_points)
# [ 0 1 4 13 16]
# [ 0. 0. 2.57383685 17.92061306 24.90689622]
To print this figure:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x_points,y_points, '-o', label="convex piecewise-linear function")
ax.legend()
fig.patch.set_facecolor('white')
plt.show()
make sure the gradient (=dx/dy) is increasing.
Pseudocode:
s = 1;
x = 0;
y = 0;
n = 4;
while(--n>0)
{
//increase x randomly
dx = rand(3);
dy = dx * s;
x += dx;
y += dy;
//increase gradient randomly
s += rand(3);
print x + "/" +y;
}
Related
For the following code whose job is to perform Monte Carlo integration for a function f, I was wondering what would happen if I define f as y = sqrt(1-x^2), which is the equation for a unit quarter circle, and specify an endpoint that is greater than 1, since we know that f is only defined for 0<x<1.
import numpy as np
import matplotlib.pyplot as plt
def definite_integral_show(f, x0, x1, N):
"""Approximate the definite integral of f(x)dx between x0 and x1 using
N random points
Arguments:
f -- a function of one real variable, must be nonnegative on [x0, x1]
N -- the number of random points to use
"""
#First, let's compute fmax. We do that by evaluating f(x) on a grid
#of points between x0 and x1
#This assumes that f is generally smooth. If it's not, we're in trouble!
x = np.arange(x0, x1, 0.01)
y = f(x)
print(y)
f_max = max(y)
#Now, let's generate the random points. The x's should be between
#x0 and x1, so we first create points beterrm 0 and (x1-x0), and
#then add x0
#The y's should be between 0 and fmax
#
# 0...(x1-x0)
x_rand = x0 + np.random.random(N)*(x1-x0)
print(x_rand)
y_rand = 0 + np.random.random(N)*f_max
#Now, let's find the indices of the poitns above and below
#the curve. That is, for points below the curve, let's find
# i s.t. y_rand[i] < f(x_rand)[i]
#And for points above the curve, find
# i s.t. y_rand[i] >= f(x_rand)[i]
ind_below = np.where(y_rand < f(x_rand))
ind_above = np.where(y_rand >= f(x_rand))
#Finally, let's display the results
plt.plot(x, y, color = "red")
pts_below = plt.scatter(x_rand[ind_below[0]], y_rand[ind_below[0]], color = "green")
pts_above = plt.scatter(x_rand[ind_above[0]], y_rand[ind_above[0]], color = "blue")
plt.legend((pts_below, pts_above),
('Pts below the curve', 'Pts above the curve'),
loc='lower left',
ncol=3,
fontsize=8)
def f1(x):
return np.sqrt(1-x**2)
definite_integral_show(f1, 0, 6, 200)
To my surprise, the program still works and gives me the following picture.
I suspect that it works because in NumPy, nan's in an array are just ignored when performing operations on the array. However, I don't understand why the picture only contains points whose x and y coordinates are both between 0 to 1. Where are the points that aren't within this range, but whose values are computed by
x_rand = x0 + np.random.random(N)*(x1-x0)
y_rand = 0 + np.random.random(N)*f_max
You can just print out the arrays (for example by generating only one random point) and see that they go into neither ind_below nor ind_above...
That's because all comparisons that involves nan returns False. (See also: What is the rationale for all comparisons returning false for IEEE754 NaN values?). (so y_rand < nan and y_rand >= nan both evaluates to False)
The easiest way to change the code is
ind_below = np.where(y_rand < f(x_rand))
ind_above = np.where(~(y_rand < f(x_rand)))
(optionally only compute the array once)
Question
Given a plotting window, how does one generate random points at the perimeter of a square (perimeter of the plotting window)?
Background and attempt
I found a similar question with regards to a rectangle in javascript.
I managed to write a program to generate random points within limits but the question is regarding how one could find random points with the condition that they are at the edge of the plot (either x is equal to 5 or -5 ,or y is equal to 5 or -5 in this case).
import numpy as np
import matplotlib.pyplot as plt
# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound
# Random coordinates [b,a) uniform distributed
coordy = (b - a) * np.random.random_sample((n,)) + a # generate random y
coordx = (b - a) * np.random.random_sample((n,)) + a # generate random x
# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))
# Plot points
for i in range(n):
plt.plot(coordx[i],coordy[i],'ro')
plt.show()
Summary
So to summarize, my question is how do I generate random coordinates given that they are at the edge of the plot/canvas. Any advice or help will be appreciated.
Here is what you can do:
from random import choice
import matplotlib.pyplot as plt
from numpy.random import random_sample
n = 6
a = 5
b = -5
plt.xlim((b,a))
plt.ylim((b,a))
for i in range(n):
r = (b - a) * random_sample() + a
random_point = choice([(choice([a,b]), r),(r, choice([a,b]))])
plt.scatter(random_point[0],random_point[1])
plt.show()
Output:
One possible approach (despite not very elegant) is the following: divide horizontal and vertical points Suppose you want to draw a point at the top or at the bottom of the window. Then,
Select randomly the y coordinate as b or -b
Select randomly (uniform distribution) the x coordinate
Similar approach for right and left edges of the window.
Hope that helps.
You could use this, but this is assuming you want to discard them when it is found they aren't on the edge.
for x in coordx:
if x != a:
coordx.pop(x)
else:
continue
And then do the same for y.
Geometrically speaking, being on the edge requires that a point satisfy certain conditions. Assuming that we are talking about a grid whose dimensions are defined by x ~ [0, a] and y ~ [0, b]:
The y-coordinate is either 0 or b, with the x-coordinate within [0, a], or
The x-coordinate is either 0 or a, with the y-coordinate within [0, b]
There are obviously more than one way to go about this, but here is a simple method to get you started.
def plot_edges(n_points, x_max, y_max, x_min=0, y_min=0):
# if x_max - x_min = y_max - y_min, plot a square
# otherwise, plot a rectangle
vertical_edge_x = np.random.uniform(x_min, x_max, n_points)
vertical_edige_y = np.asarray([y_min, y_max])[
np.random.randint(2, size=n_points)
]
horizontal_edge_x = np.asarray([x_min, x_max])[
np.random.randint(2, size=n_points)
]
horizontal_edge_y = np.random.uniform(x_min, x_max, n_points)
# plot generated points
plt.scatter(vertical_edge_x, vertical_edige_y)
plt.scatter(horizontal_edge_x, horizontal_edge_y)
plt.show()
Can you try this out?
import numpy as np
import matplotlib.pyplot as plt
# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound
import random
coordx,coordy=[],[]
for i in range(n):
xy = random.choice(['x','y'])
if xy=='x':
coordx.append(random.choice([b,a])) # generate random x
coordy.append(random.random()) # generate random y
if xy=='y':
coordx.append(random.random()) # generate random x
coordy.append(random.choice([b,a])) # generate random y
# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))
# Plot points
for i in range(n):
plt.plot(coordx[i],coordy[i],'ro')
plt.show()
Here is a sample output:
Here's a way to do that:
import numpy as np
import matplotlib.pyplot as plt
# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound
# Random coordinates [b,a) uniform distributed
coordy = (b - a) * np.random.random_sample((n,)) + a # generate random y
coordx = (b - a) * np.random.random_sample((n,)) + a # generate random x
# This is the new code
reset_axis = np.random.choice([True, False], n) # select which axis to reset
reset_direction = np.random.choice([a,b], n) # select to go up / right or down / left
coordx[reset_axis] = reset_direction[reset_axis]
coordy[~reset_axis] = reset_direction[~reset_axis]
# end of new code.
# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))
# Plot points
for i in range(n):
plt.plot(coordx[i],coordy[i],'ro')
plt.show()
The result is:
I am currently trying to identify peaks on a randomly generated plot that I have created.
My code is as follows:
x_range = np.arange(0,100,0.5) #my x values
for i in len(ys): #ys is my range of y values on the chart
for j in range(start,len(ys)): #Brute forcing peak detection
temp.append(ys[j])
check = int(classtest.isPeak(temp)[0])
if check == 1:
xval = temp.index(max(temp)) #getting the index
xlist = x_range.tolist()
plt.plot(xlist[xval],max(temp),"ro")
start = start + 1
temp = []
However when plotting the values on the graph, it seems to plot the correct y position, but not x. Here is an example of what is happening:
I am really not sure what is causing this problem, and I would love some help.
Thanks
Remember that temp is getting shorter and shorter as start increases.
So the index, xval, corresponding to a max in temp is not in itself the correct index into x_range. Rather, you have to increase xval by start to find the corresponding index in x_range:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
N = 100
ys = (np.random.random(N)-0.5).cumsum()
xs = np.linspace(0, 100, len(ys))
plt.plot(xs, ys)
start = 0
temp = []
for i in range(len(ys)): #ys is my range of y values on the chart
for j in range(start,len(ys)): #Brute forcing peak detection
temp.append(ys[j])
xval = temp.index(max(temp)) #getting the index
plt.plot(xs[xval+start], max(temp),"ro")
start = start + 1
temp = []
plt.show()
While that does manage to place the red dots at points on the graph, as you can
see the algorithm is placing a dot at every point on the graph, not just at local
maxima. Part of the problem is that when temp contains only one point, it is
of course the max. And the double for-loop ensures that every point gets
considered, so at some point temp contains each point on the graph alone as a
single point.
A different algorithm is required. A local max can be identified as any
point which is bigger than its neighbors:
ys[i-1] <= ys[i] >= ys[i+1]
therefore, you could use:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
N = 100
ys = (np.random.random(N)-0.5).cumsum()
xs = np.linspace(0, 100, len(ys))
plt.plot(xs, ys)
idx = []
for i in range(1, len(ys)-1):
if ys[i-1] <= ys[i] >= ys[i+1]:
idx.append(i)
plt.plot(xs[idx], ys[idx], 'ro')
plt.show()
Note that scipy.signal.argrelextrema or scipy.signal.argrelmax can also be used to find local maximums:
from scipy import signal
idx = signal.argrelextrema(ys, np.greater)
plt.plot(xs[idx], ys[idx], 'ro')
produces the same result.
I want to know how the contours levels are chosen in pyplot.contour. What I mean by this is, given a function f(x, y), the level curves are usually chosen by evaluating the points where f(x, y) = c, c=0,1,2,... etc. However if f(x, y) is an array A of nxn points, how do the level points get chosen? I don't mean how do the points get connected, just simply the points that correspond to A = c
Looking at the source code (contour.py), it's something like this
import numpy as np
import matplotlib.pylab as pl
import matplotlib.ticker as ticker
x = np.arange(10)
y = np.arange(10)
z = np.random.random((10,10))
pl.figure()
pl.contour(x, y, z)
pl.colorbar()
# manually calculate levels:
zmin = np.amin(z)
zmax = np.amax(z)
N = 7 # default
locator = ticker.MaxNLocator(N+1)
lev = locator.tick_values(zmin, zmax)
# Clip
levels = lev[(lev > zmin) & (lev < zmax)]
print(levels)
[ 0.15 0.3 0.45 0.6 0.75 0.9 ]
The function is evaluated at every grid node, and compared to the iso-level. When there is a change of sign along a cell edge, a point is computed by linear interpolation between the two nodes. Points are joined in pairs by line segments. This is an acceptable approximation when the grid is dense enough.
linspace generates a linear space. How can I generate a grid using an arbitrary density function?
Say, I would like to have a grid from 0 to 1, with 100 grid points, and where the density of points is given by (x - 0.5)**2 - how would I create such a grid in Python?
That is, I want many grid-points where the function (x - 0.5)**2) is large, and few points where the function is small. I do not want a grid that has values according to this function.
For example like this:
x = (np.linspace(0.5,1.5,100)-0.5)**2
The start and end values have to be chosen so that f(start) = 0 and f(end)=1.
In that case the following solution should work. Be sure that func is positive throughout the range...
import numpy as np
from matplotlib import pyplot as plt
def func(x):
return (x-0.5)**2
start = 0
end = 1
npoints = 100
x = np.linspace(start,end,npoints)
fx = func(x)
# take density (or intervals) as inverse of fx
# g in [0,1] controls how much warping you want.
# g = 0: fully warped
# g = 1: linearly spaced
g = 0
density = (1+g*(fx-1))/fx
# sum the intervals to get new grid
x_density = np.cumsum(density)
# rescale to match old range
x_density -= x_density.min()
x_density/= x_density.max()
x_density *= (end-start)
x_density += start
fx_density = func(x_density)
plt.plot(x,fx,'ok',ms = 10,label = 'linear')
plt.plot(x_density,fx_density,'or',ms = 10,label = 'warped')
plt.legend(loc = 'upper center')
plt.show()