Experiment 1: Varying the Number of Points • Fix the uniform noise interval 8 = 1. • Vary the number of points n = : 2, 5, 7, 10, 20, 50, 75, 100. • For each value of n, generate data, fit the model, calculate Err(wo, w†), and plot Err(wt), w₁) as a function of n. • Generate 1 plot with 8 different subplots, one for each value of n, subplot each showing the true model, generated data points with noise, and the predicted model. Experiment 2: Varying the Noise Interval • Fix the number of points n = = 100. • Vary the uniform interval = 0.1, 1, 5, 10. • For each value of 8, generate data, fit the model, calculate Err(wo), w¼), and plot Err(w+), w¼) as a function of S. • Generate 1 plot with 4 subplots, one for each value of 8, each subplot such showing the true model, generated data points with noise, and the predicted model.

icon
Related questions
Question

import numpy as np
import matplotlib.pyplot as plt
alpha = np.random.uniform(1.0,5.0)
beta = np.random.uniform(0.0,10.0)
def generate_data(n):
    x = np.random.uniform(-10, 10, n)  # Generate 'n' random x values
    epsilon = np.random.uniform(-n, n, n)  # 'n' random noise values
    y = alpha * x + beta + epsilon
    return x, y

def find_optimal_slope(x,y):
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    numerator = np.sum((x - x_mean) * (y - y_mean))
    denominator = np.sum((x - x_mean)**2)
    if denominator == 0:
        slope = 9999999  
    else:
        slope = numerator / denominator
    return slope
def find_optimal_intercept(x,y):
    slope = find_optimal_slope(x, y)  
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    intercept = y_mean - slope * x_mean
    return intercept

def calculate_err(x,y):
    slope = find_optimal_slope(x, y)
    intercept = find_optimal_intercept(x,y)
    alpha = np.random.uniform(1.0,5.0)
    beta = np.random.uniform(0.0,10.0)
    estimation_error = (abs(slope - alpha) ** 2  + abs(slope - beta) ** 2) ** 0.5


def plot_fit(n):
    alpha = np.random.uniform(1.0, 5.0)
    beta = np.random.uniform(0.0, 10.0)
    
    # Generate data
    x, y = generate_data(n)
    
    # Find the optimal slope and intercept for the best-fit line
    slope = find_optimal_slope(x, y)
    intercept = find_optimal_intercept(x, y)
    
    # Calculate the fitted line and true line
    fitted_line = slope * x + intercept
    true_line = alpha * x + beta
    
    # Plotting
    plt.scatter(x, y, color='blue', label='Data points')
    plt.plot(x, fitted_line, color='red', label='Fitted line')
    plt.plot(x, true_line, color='green', label='True line', linestyle='--')
    plt.legend()
    plt.title('True Line vs Fitted Line')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()

Experiment 1: Varying the Number of Points
• Fix the uniform noise interval 8 = 1.
•
Vary the number of points n = : 2, 5, 7, 10, 20, 50, 75, 100.
• For each value of n, generate data, fit the model, calculate Err(wo, w†), and plot Err(wt), w₁) as a function
of n.
• Generate 1 plot with 8 different subplots, one for each value of n, subplot each showing the true model,
generated data points with noise, and the predicted model.
Experiment 2: Varying the Noise Interval
• Fix the number of points n = = 100.
• Vary the uniform interval = 0.1, 1, 5, 10.
• For each value of 8, generate data, fit the model, calculate Err(wo), w¼), and plot Err(w+), w¼) as a function
of S.
• Generate 1 plot with 4 subplots, one for each value of 8, each subplot such showing the true model,
generated data points with noise, and the predicted model.
Transcribed Image Text:Experiment 1: Varying the Number of Points • Fix the uniform noise interval 8 = 1. • Vary the number of points n = : 2, 5, 7, 10, 20, 50, 75, 100. • For each value of n, generate data, fit the model, calculate Err(wo, w†), and plot Err(wt), w₁) as a function of n. • Generate 1 plot with 8 different subplots, one for each value of n, subplot each showing the true model, generated data points with noise, and the predicted model. Experiment 2: Varying the Noise Interval • Fix the number of points n = = 100. • Vary the uniform interval = 0.1, 1, 5, 10. • For each value of 8, generate data, fit the model, calculate Err(wo), w¼), and plot Err(w+), w¼) as a function of S. • Generate 1 plot with 4 subplots, one for each value of 8, each subplot such showing the true model, generated data points with noise, and the predicted model.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer