Monday, 10 March 2025

Peak-to-Peak range

Peak-to-Peak range  is the difference between the highest and lowest values in a dataset. It measures total amount of variation in the dataset.

For example, following dataset represents the house prices in a neighborhood.

 

house_prices = [100000, 110000, 125000, 95000, 115000, 118000, 123000, 105000]

 

max_value : 125000

min_value : 95000

range_value : (max_value – min_value) = 30000

 

So, in this example, the range (P-P range) of the house prices is 30000 dollars. This means that the prices varied by 30000 dollars from its lowest point to its highest point.

 

Peak-to-Peak range is highly affected by outliers, and may not give a complete picture of the overall distribution. To get a better insights on measure of spread, the interquartile range (IQR) can be used because it focuses on the central 50% of the data and is less affected by outliers.

 

Using ‘np.ptp’ method, we can get the peak-to-peak range value.

 

range_value = np.ptp(house_prices)

 

Find the below working application.

 

ptp.py

import matplotlib.pyplot as plt
import numpy as np

# Generate some random data with a mean of 0 and standard deviation of 1
house_prices = [100000, 110000, 125000, 95000, 115000, 118000, 123000, 105000]

max_value = np.max(house_prices)
min_value = np.min(house_prices)

# Range (Peak-to-Peak)
range_value = np.ptp(house_prices)

print(f'max_value : {max_value}')
print(f'min_value : {min_value}')
print(f'range_value : {max_value-min_value}')
print(f'range_value : {range_value}')

# Create a histogram to visualize the data
plt.hist(house_prices, bins=10, edgecolor='k', alpha=0.7)
plt.axvline(min_value, color='red', linestyle='--', label=f'Min: {min_value}')
plt.axvline(max_value, color='blue', linestyle='--', label=f'Max: {max_value}')

# Add labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title(f'Peak-to-Peak Range: {range_value}')

# Add legend
plt.legend()

# Show the plot
plt.show()

 

Output


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment