





import numpy

# List of input, output pairs
UpperCAmelCase__   : List[str]         =       (
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
UpperCAmelCase__   : Optional[int]         =       (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
UpperCAmelCase__   : str         =       [2, 4, 1, 5]
UpperCAmelCase__   : Dict         =       len(train_data)
UpperCAmelCase__   : Union[str, Any]         =       0.009


def  A							(			snake_case__   :						Optional[int] ,				snake_case__   :						Dict="train"				)					->				Dict:
 '''simple docstring'''
 return calculate_hypothesis_value(lowerCamelCase_ ,				lowerCamelCase_				) - output(
     lowerCamelCase_ ,				lowerCamelCase_				)


def  A							(			snake_case__   :						List[Any]				)					->				Optional[int]:
 '''simple docstring'''
 __snake_case         =			0
 for i in range(len(lowerCamelCase_				) - 1				):
  hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
 hyp_val += parameter_vector[0]
 return hyp_val


def  A							(			snake_case__   :						str ,				snake_case__   :						Any				)					->				Any:
 '''simple docstring'''






 if data_set == "train":
  return train_data[example_no][1]
 elif data_set == "test":
  return test_data[example_no][1]
 return None


def  A							(			snake_case__   :						Optional[Any] ,				snake_case__   :						str				)					->				List[str]:
 '''simple docstring'''






 if data_set == "train":
  return _hypothesis_value(train_data[example_no][0]				)
 elif data_set == "test":
  return _hypothesis_value(test_data[example_no][0]				)
 return None


def  A							(			snake_case__   :						int ,				snake_case__   :						Any=m				)					->				str:
 '''simple docstring'''
 __snake_case         =			0
 for i in range(lowerCamelCase_				):
  if index == -1:
   summation_value += _error(lowerCamelCase_				)
  else:
   summation_value += _error(lowerCamelCase_				) * train_data[i][0][index]
 return summation_value


def  A							(			snake_case__   :						int				)					->				str:
 '''simple docstring'''
 __snake_case         =			summation_of_cost_derivative(lowerCamelCase_ ,				lowerCamelCase_				) / m
 return cost_derivative_value


def  A							(			)					->				Optional[int]:
 '''simple docstring'''
 global parameter_vector
 # Tune these values to set a tolerance value for predicted output
 __snake_case         =			0.000_002
 __snake_case         =			0
 __snake_case         =			0
 while True:
  j += 1
  __snake_case         =			[0, 0, 0, 0]
  for i in range(0 ,				len(lowerCamelCase_				)				):
   __snake_case         =			get_cost_derivative(i - 1				)
   __snake_case         =			(
       parameter_vector[i] - LEARNING_RATE * cost_derivative
   )
  if numpy.allclose(
      lowerCamelCase_ ,				lowerCamelCase_ ,				atol=lowerCamelCase_ ,				rtol=lowerCamelCase_ ,				):
   break
  __snake_case         =			temp_parameter_vector
 print(('Number of iterations:', j)				)


def  A							(			)					->				List[Any]:
 '''simple docstring'''






 for i in range(len(lowerCamelCase_				)				):
  print(('Actual output value:', output(lowerCamelCase_ ,				'test'				))				)
  print(('Hypothesis output:', calculate_hypothesis_value(lowerCamelCase_ ,				'test'				))				)


if __name__ == "__main__":
  run_gradient_descent()
  print("\nTesting gradient descent for a linear hypothesis function.\n")
  test_gradient_descent()






"""simple docstring"""







from random import randint, random

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							bool = False     ,		lowerCamelCase_:							bool = False     ,		lowerCamelCase_:							int = 5     ,		):
						"""simple docstring"""






						snake_case   :			str						    = [[-1] * number_of_cells]  # Create a highway without any car
						snake_case   :			str						    = 0
						snake_case   :			Any						    = max(lowerCamelCase_     ,		0 )
						while i < number_of_cells:
												snake_case   :			Optional[int]						    = (
												    randint(0     ,		lowerCamelCase_ ) if random_speed else initial_speed
												)  # Place the cars
												i += (
												    randint(1     ,		max_speed * 2 ) if random_frequency else frequency
												)  # Arbitrary number, may need tuning
						return highway

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			Any						    = 0
						snake_case   :			int						    = highway_now[car_index + 1 :]
						for cell in range(len(lowerCamelCase_ ) ):  # May need a better name for this
												if cells[cell] != -1:  # If the cell is not empty then
																		return distance  # we have the distance we wanted
												distance += 1
						# Here if the car is near the end of the highway
						return distance + get_distance(lowerCamelCase_     ,		-1 )

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							float     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			List[str]						    = len(lowerCamelCase_ )
						# Beforce calculations, the highway is empty
						snake_case   :			Dict						    = [-1] * number_of_cells

						for car_index in range(lowerCamelCase_ ):
												if highway_now[car_index] != -1:
																		# Add 1 to the current speed of the car and cap the speed
																		snake_case   :			Tuple						    = min(highway_now[car_index] + 1     ,		lowerCamelCase_ )
																		# Number of empty cell before the next car
																		snake_case   :			int						    = get_distance(lowerCamelCase_     ,		lowerCamelCase_ ) - 1
																		# We can't have the car causing an accident
																		snake_case   :			str						    = min(next_highway[car_index]     ,		lowerCamelCase_ )
																		if random() < probability:
																								# Randomly, a driver will slow down
																								snake_case   :			Optional[Any]						    = max(next_highway[car_index] - 1     ,		0 )
						return next_highway

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							list     ,		lowerCamelCase_:							int     ,		lowerCamelCase_:							float     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						snake_case   :			Optional[Any]						    = len(highway[0] )

						for i in range(lowerCamelCase_ ):
												snake_case   :			Union[str, Any]						    = update(highway[i]     ,		lowerCamelCase_     ,		lowerCamelCase_ )
												snake_case   :			Optional[int]						    = [-1] * number_of_cells

												for car_index in range(lowerCamelCase_ ):
																		snake_case   :			List[Any]						    = next_speeds_calculated[car_index]
																		if speed != -1:
																								# Change the position based on the speed (with % to create the loop)
																								snake_case   :			Optional[Any]						    = (car_index + speed) % number_of_cells
																								# Commit the change of position
																								snake_case   :			Tuple						    = speed
												highway.append(lowerCamelCase_ )

						return highway


if __name__ == "__main__":
			import doctest

			doctest.testmod()



