Question: Coding up the Schelling model In class we're going to try to turn the Schelling model into code. To make sure you're in a good


Coding up the Schelling model In class we're going to try to turn the Schelling model into code. To make sure you're in a good position to implement the model in class your job is to create one function that initializes your population and review/test another one that figures out which people are "unhappy. Function 1: Creating a game board Write a function that creates a one-dimensional game board composed of agents of two different types (0 and 1,>X and O, stars and pluses whatever you want), where the agents are assigned to spots randomly with a 50% chance of being either type. Define the function so that it takes as inputs: 1. The number of spots in the game board. Make it so that the default is set to 32. 2. A random seed that you will use to initialize the board (this will make it possible to test the reproducibility of your model). Also set a default seed value. Make sure your function returns your game board. (Something to think about: which makes more sense to describe the game board, a list or a Numpy array? What are the tradeoffs?) Show that your function is behaving correctly by printing out the returned game board. Hint: There is more than one way to write this code. If you're having trouble coming up with your own method, you could consider trying to use np.random.choice(), but you'll have to read the documentation to figure out how to get the output you want.) t def initialize_board(...): # make sure you define the right inputs for the function # and put the rest of your code here Function 2: Deciding if an agent is happy Review this function that takes the game board generated by the function you wrote above and determines whether an agent at position i in the game board is happy for a game board of any size and a neighborhood of size N (i.e., from position i-N to i+N), and returns that information. The code has to make sure to check that position i is actually inside the game board (i.e., make sure the request makes sense), and ensure that it behaves correctly for agents near the edges of the game board Show that this function is behaving correctly by having it check every position in a game board that you generate using your function that you just created. Verify by eye that it's behaving correctly (Something to think about: How could you use this function to figure out where to put an agent? Would the function need to be written differently? Or could you use it as is? In order to move agents around, you'd eventually need to figure this out.) In [1]: def is-happy (my-list, my-index, tol-prob 0.5, neighbor-N 4): = - This function take a game board (my_list) and checks to see if the agent at the given index (my_index) is "happy" or unhappy" based on a provided tolerance (tol prob) and a given neighborhood size (neighbor N It returns 'True' if happy and 'False' if unhappy under those circumstances # do some error-checking (is the index within the allowed range ?) if my_index len (my list): print("you've made an indexing error!", myindex) return # store the value we will compare neighbors to (e.g. 0 or 1) my valuemy list my index start -max (my index - neighbor N, 0) # start 4 to the left end-min (my-index + neighbor-N, len(my-list)-1) # end 4 to the right # counter for keeping track of the neighbors that are like me neighbors_like_me-0 # counter for keeping track of total neighbors total_neighbors0 # loop over the specified range for i in range (start,end+1): if i-my-index: continue # don't count myself if my-list[1]-my-value : # if this neighbor is like me, keep track of that neighbors-like me += 1 total-neighbors+-1 # also keep track of total neighbors # happy if at least half are like me, unhappy otherwise note: it's at least half because we 're not double-counting our # own value if neighbors_like me/total neighborstol_prob: else: return True return False In [2]: # Put your testing code here. Coding up the Schelling model In class we're going to try to turn the Schelling model into code. To make sure you're in a good position to implement the model in class your job is to create one function that initializes your population and review/test another one that figures out which people are "unhappy. Function 1: Creating a game board Write a function that creates a one-dimensional game board composed of agents of two different types (0 and 1,>X and O, stars and pluses whatever you want), where the agents are assigned to spots randomly with a 50% chance of being either type. Define the function so that it takes as inputs: 1. The number of spots in the game board. Make it so that the default is set to 32. 2. A random seed that you will use to initialize the board (this will make it possible to test the reproducibility of your model). Also set a default seed value. Make sure your function returns your game board. (Something to think about: which makes more sense to describe the game board, a list or a Numpy array? What are the tradeoffs?) Show that your function is behaving correctly by printing out the returned game board. Hint: There is more than one way to write this code. If you're having trouble coming up with your own method, you could consider trying to use np.random.choice(), but you'll have to read the documentation to figure out how to get the output you want.) t def initialize_board(...): # make sure you define the right inputs for the function # and put the rest of your code here Function 2: Deciding if an agent is happy Review this function that takes the game board generated by the function you wrote above and determines whether an agent at position i in the game board is happy for a game board of any size and a neighborhood of size N (i.e., from position i-N to i+N), and returns that information. The code has to make sure to check that position i is actually inside the game board (i.e., make sure the request makes sense), and ensure that it behaves correctly for agents near the edges of the game board Show that this function is behaving correctly by having it check every position in a game board that you generate using your function that you just created. Verify by eye that it's behaving correctly (Something to think about: How could you use this function to figure out where to put an agent? Would the function need to be written differently? Or could you use it as is? In order to move agents around, you'd eventually need to figure this out.) In [1]: def is-happy (my-list, my-index, tol-prob 0.5, neighbor-N 4): = - This function take a game board (my_list) and checks to see if the agent at the given index (my_index) is "happy" or unhappy" based on a provided tolerance (tol prob) and a given neighborhood size (neighbor N It returns 'True' if happy and 'False' if unhappy under those circumstances # do some error-checking (is the index within the allowed range ?) if my_index len (my list): print("you've made an indexing error!", myindex) return # store the value we will compare neighbors to (e.g. 0 or 1) my valuemy list my index start -max (my index - neighbor N, 0) # start 4 to the left end-min (my-index + neighbor-N, len(my-list)-1) # end 4 to the right # counter for keeping track of the neighbors that are like me neighbors_like_me-0 # counter for keeping track of total neighbors total_neighbors0 # loop over the specified range for i in range (start,end+1): if i-my-index: continue # don't count myself if my-list[1]-my-value : # if this neighbor is like me, keep track of that neighbors-like me += 1 total-neighbors+-1 # also keep track of total neighbors # happy if at least half are like me, unhappy otherwise note: it's at least half because we 're not double-counting our # own value if neighbors_like me/total neighborstol_prob: else: return True return False In [2]: # Put your testing code here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
