I have a code that creates random Datapoints. Is there a way to remove outliers by density? In my example this would be all points outside of the red line (picture). I also created a pareto frontier so the the pairs from Xs and Ys would need to stay pairs by Index.
Code:
Mean = ff.mean()
Vol = ff.cov()
nPort = 1000000
weight = np.zeros((nPort,11))
myList = []
Xs = []
Ys = []
for k in range(nPort):
# generate random weight vector
w = np.array(np.random.random(11))
w = w/np.sum(w)
weight[k,:] = w
# expected returns
eR = np.sum(Mean * w)
Ys.append(eR)
# expected Vol
eV = np.sqrt(np.dot(w.T,np.dot(Vol,w)))
Xs.append(eV)
# pareto frontier
def pareto_frontier(Xs, Ys, maxX = True, maxY = True):
# Sort the list in either ascending or descending order of X
myList = sorted([[Xs[i], Ys[i]] for i in range(len(Xs))], reverse=maxX)
# Start the Pareto frontier with the first value in the sorted list
p_front = [myList[0]]
# Loop through the sorted list
for pair in myList[1:]:
if maxY:
if pair[1] >= p_front[-1][1]: # Look for higher values of Y…
p_front.append(pair) # … and add them to the Pareto frontier
else:
if pair[1] <= p_front[-1][1]: # Look for lower values of Y…
p_front.append(pair) # … and add them to the Pareto frontier
# Turn resulting pairs back into a list of Xs and Ys
p_frontX = [pair[0] for pair in p_front]
p_frontY = [pair[1] for pair in p_front]
return p_frontX, p_frontY
# Plotting
# Find lowest values for cost and highest for savings
p_front = pareto_frontier(Xs, Ys, maxX = False, maxY = True)
# Plot a scatter graph of all results
plt.scatter(Xs, Ys, alpha=0.1, color='lightblue')
# Then plot the Pareto frontier on top
#plt.plot(p_front[0], p_front[1], color='#003A57')
plt.show()
[1]: https://i.stack.imgur.com/Lw2WM .png