Search code examples
pythongeometryprobabilityscipy.stats

How to calculate the radius of a circle so that 95 % of the dots are inside of it


I have measured 100 points and calculated the standard deviation in x and y-direction. I then plotted each of the 100 points in an graph and now do I want to encircle 95 % of these points.

The idea was that I could use the standard deviation to calculate the radius however tests with random data with similar standard deviation proved that this did not work.

I have tested using Protagoras theorem as well as the standard deviation function in numpy.

    cal_x_mean = 0
    cal_x_std = 0.5286
    cal_z_mean = 0
    cal_z_std = 0.4158
    
    cal_x_values = stats.truncnorm(-1, 1, loc=cal_x_mean, scale=cal_x_std)
    cal_z_values = stats.truncnorm(-1, 1, loc=cal_z_mean, scale=cal_z_std)

    cal_x = cal_x_values.rvs(100)
    cal_y = cal_z_values.rvs(100)

    # cal_r = 2*np.std([cal_x, cal_y])              # numpy
    cal_r = math.hypot(cal_x_std, cal_z_std)        # Protagoras

    print(f'Radius for calibrated {cal_r}')

    fig, ax = plt.subplots(1)
    plt.plot(0, 0, 'o', color='red')
    plt.plot(cal_x, cal_y, 'o', color='black')
    Drawing_uncolored_circle = plt.Circle((0, 0), cal_r, fill=False)

    ax.set_aspect(1)
    ax.add_artist(Drawing_uncolored_circle)
    plt.xlim(-1, 1)
    plt.ylim(-1, 1)
    plt.title(str("Calibrated"))
    plt.grid()
    plt.ylabel('Z (mm)')
    plt.xlabel('X (mm)')

Do the great minds in stackoverflow have a suggestion to how to solve this problem?

A turtle


Solution

  • Assuming you want an exact answer, you can rethink your problem as finding the 95th percentile of a list of values, where each value is the distance of the point to the origin.