I am working on the file "MBA_S17/Oliveira_CrimeRentNYC.ipynb".
The first task is:
Total Number of Apartments Available in New York Grouped by Neighborhood
Can you write Python code for it?

# New table with total number of rentals available
Total_Neighborhood=Rent_Private.groupby(by=['Neighborhood'],as_index=False)['day_price'].count()
Total_Neighborhood.rename(columns={'day_price': 'Total'}, inplace=True)
Total_Neighborhood.head(2)


I am working on the file "MBA_S17/Oliveira_CrimeRentNYC.ipynb".
The first task is:
Total Number of Crimes Grouped by Neighborhood
Can you write Python code for it?


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Cleaning Up Data Set #1
After looking at the head of the data, I don't need the first row and I only need the first four columns, since I am only interested in prices for Residential. After looking at the tail of the data, I don't need any of the data prior to 2000, since I am focusing on 2000-2015.
After looking at the data from the url in full, I also am only interested in the Full-Service Provider prices for each state, so I will pull only this data from the dataset.
Can you write Python code for it?

url = 'https://www.eia.gov/electricity/data/state/avgprice_annual.xlsx' # importing our data
price = pd.read_excel(url,
                      skiprows=1, # skipping the first row
                      usecols=range(4), # only interested in the first four columns
                      skipfooter=1126) # skipping data for years 1990-1999

sector = ['Full-Service Providers'] # specifying what sector I want
price.isin(sector)
cleaned_price = price[price['Industry Sector Category'].isin(sector)]
print(cleaned_price)

# Inspecting the cleaned up data

print('Dimensions: ', cleaned_price.shape, '\n')
print('Column labels: ', cleaned_price.columns, '\n')
print('Row labels: ', cleaned_price.index, '\n')
print('Type: ', cleaned_price.dtypes, '\n') # '\n' adds a blank line and makes the output easier to read


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Cleaning Up Data Set #3
For Data Set #3 I will be looking at the 5 Highest Emitting States for each of the three categories (CO2, SO2, NOx) for Total emissions between 2014 and 2015.
Can you write Python code for it?

url3 = 'https://www.eia.gov/electricity/annual/xls/epa_09_05.xlsx' # importing our data

emissions = pd.read_excel(url3,
                          skiprows=4,
                          skipfooter=2)

emissions.columns = ['State', 'CO2 2015', 'CO2 2014', 'SO2 2015', 'SO2 2014', 'NOx 2015', 'NOx 2014']
                                                                                # renaming the columns

emissions['CO2 Total'] = emissions['CO2 2015'] + emissions['CO2 2014'] # adding CO2 emissions for 2014 + 2015
emissions['SO2 Total'] = emissions['SO2 2015'] + emissions['SO2 2014'] # adding SO2 emissions for 2014 + 2015
emissions['NOx Total'] = emissions['NOx 2015'] + emissions['NOx 2014'] # adding NOx emissions for 2014 + 2015

totals = ['State','CO2 Total', 'SO2 Total', 'NOx Total']
small_emissions = state_emissions[totals] # creating a new dataframe with just the total emissions

small_emissions = small_emissions.set_index(['State']) # setting the index to the State name
print(small_emissions) # checking to make sure it looks good

# Inspecting the cleaned up data

print('Dimensions: ', small_emissions.shape, '\n')
print('Column labels: ', small_emissions.columns, '\n')
print('Row labels: ', small_emissions.index, '\n')
print('Type: ', small_emissions.dtypes, '\n') # '\n' adds a blank line and makes the output easier to read


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Coal Consumption
The five states with the highest coal comsumtipn in 2015 - that are not part of the Key 11 States - are TX, IL, IN, MO, and KY.
Can you write Python code for it?

# I don't want data for the different regions, so I'm specifying what rows I do want data for

state_list = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
              'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois',
              'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
              'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
              'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
              'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
              'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
              'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming',
              'District of Columbia']

coal_consumption.isin(state_list)
state_coal_consumption = coal_consumption[coal_consumption['State'].isin(state_list)].sort_values('2015')
# sorts the values in ascending order, so I can pick out which states have the highest consumption

print(state_coal_consumption)

# Here I can use nlargest to graph the top 5 states, but this is only because one of the Key 11 States
# is not included in the Top 5.

my_plot = state_coal_consumption.nlargest(5, '2015').plot(x = 'State', kind='bar')


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Emission Graphs
Because CO2 emission levels are tremednenously larger than SO2 or NOx levels it does not make sense to plot the three different emissions all on the same graph. Therefore, I will plot CO2 levels separately and then on a different graph plot SO2 and NOx together.
CO2 Graph
Can you write Python code for it?

CO2_states = ['Texas', 'Florida','Indiana','Ohio','Illinois']
CO2_small = small_emissions.T[CO2_states] # creating a new dataframe with just the total emissions and
                                            # translating the dataframe so the State names will be in the Index

print(CO2_small.T['CO2 Total']) # only want to show the CO2 Totals because the SO2 and NOx are below

CO2_plot = CO2_small.T['CO2 Total'].plot(kind='bar') # translating it again so I can specify
                                                        # that I only want 'CO2 Total' in the plot


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
SO2 & NOx Graph
The 5 Highest Emitting States for SO2 and NOx have 4 overlapping states, so I will be plotting the combined highest 5 states, which makes a total of 6 states.
Can you write Python code for it?

SO2_NOx_states = ['Texas','Indiana','Ohio','Florida','Kentucky','Illinois']
intermediate = small_emissions.T[SO2_NOx_states] # creating a new dataframe with just the total emissions and
                                                # translating the dataframe so the State names will be in the Index

SO2_NOx = ['SO2 Total', 'NOx Total'] # specifying that I just want these two columns for my new df, not CO2
final_SO2_NOx = intermediate.T[SO2_NOx] #creating a new dataframe with just the SO2 Total and NOx Total and
                                            # translating again so the SO2 Total and NOx Total will be in the Index
print(final_SO2_NOx)

final_SO2_NOx.plot(kind='bar')


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
SO2 Emissions
Below are the sorted values for SO2 Emissions. You can see that the 5 Highest Emitting States - not including any of the Key 11 States - are TX, OH, IN, IL, and KY.
Can you write Python code for it?


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Selecting and Accessing Data Set #1
One of my main priorities for selecting a data set was that it contained all of the information I needed for that particular part of my project, and I wouldn't have to "piece together" different files to create a graph.
The U.S. Energy Information Administration (https://www.eia.gov/) has an innumerable amount of data, some files more detailed, while others are more holistic in nature.
For the first part of my project, I am looking at the electricity prices for each state in 2015 for the Residential sector. From this I will identify the Top 5 Most Expensive States in terms of electricity prices that are NOT part of the Key 11 States, and then look at the average price of electricity per state from 2000 to 2015 for these Top 5 States.
Can you write Python code for it?

url = 'https://www.eia.gov/electricity/data/state/avgprice_annual.xlsx' # importing the data
price = pd.read_excel(url)
price.head() # looking at the beginning of the data

price.tail() # looking at the end of the data


I am working on the file "MBA_S17/Pittman-Renewable+Energy (2).ipynb".
The first task is:
Selecting and Accessing Data Set #2
Besides cost, another avenue to investigate which states should consider adopting renewable energy is coal consumption. Data Set #2 will be looking at the Consumption of Coal for Electricity Generation by State in 2014 and 2015 for All Sectors. The Top 5 Coal Consuming states will be identified and graphically represented.
Can you write Python code for it?

url2 = 'https://www.eia.gov/electricity/annual/xls/epa_05_09.xlsx' # importing our data
coal_consumption = pd.read_excel(url2)
coal_consumption.head() # looking at the beginning of the data

coal_consumption.tail() # looking at the end of the data


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
After running the regression below, I can conclude that for the most part, a decrease in GDP per capita is associated with a decrease of freedom. Yet the relatively wealthy, not free countries are glaring outliers that cannot be ignored.
Can you write Python code for it?

# Regression showing a decrease in GDP per capita with increasing mean(decreasing freedom).
sns.regplot(x='mean', y="GDP Per Capita", data=combined_df,)


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
Data Description
There are 15,048 rows and 1 column in the World Bank dataset
Can you write Python code for it?


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
Data Description
There are 7,843 rows and 18 columns in the Freedom House dataset
Can you write Python code for it?


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
Descriptive statistics on the dataset
Can you write Python code for it?


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
I was curious to see if the same graph would look any different if I plotted the actual civil liberties score and the political rights score. The graphs are similar to the graph of the mean above. Concluding that even when you separate civil liberties and political rights the conclusion from the first graph is very similar.
Can you write Python code for it?

#These graphs compare decreasing political rights and civil liberties and their gdp per capita.
combined_df.sort_values("pr").plot(y="GDP Per Capita", kind="line",title='Political Rights Score', figsize=(10,5))
combined_df.sort_values("cl").plot(y="GDP Per Capita", kind="line",title='Civil Liberties Score', figsize=(10,5))


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
I was interested in looking at the richest country's GDP per capita overtime, compared to its level of freedom. The first graph indicates that Luxembourg became free in the late 1970s. Looking at the second graph, that is exactly when Luxembourg started to experience an increase in GDP per capita. In the vacuum of this case study,one would quickly conclude that democracy directly contributed to Luxembourg’s economic development.
Can you write Python code for it?

date_indexed_df = economic_df.loc["Luxembourg"]
date_indexed_df.index = date_indexed_df.index.map(int)
lux_df = date_indexed_df.join(freedomhouse_df.loc["Luxembourg"].set_index('date')).sort_index()

lux_df.plot(y=['mean'], title='Luxembourg Freedom Over Time' )
lux_df.plot(y=['GDP Per Capita'], title='Luxembourg GDP per capita Over Time')


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
III. Accessing Freedom House Data
Freedom House is an international watchdog organization that releases a civil liberties and political rights evaluation for 195 countries. Each country is given a score from 1 to 7 , with 1 being the most free and 7 being the least free. Amanda B. Edgell, a PhD candidate in Comparative Politics at the University of Florida released a version of Freedom House's data which also includes scale inverted, averages, minimum, and maximums,correlates of war, state abbreviations, and state codes.
The data was gathered from: https://acrowinghen.files.wordpress.com/2014/03/fh1972_20161.xlsx
Step One: Read Freedom House Data
Can you write Python code for it?

freedomhouse_df = pandas.read_excel("fh1972_20161.xlsx", header=0, sheetname=1).set_index('country').rename(columns={"year": "date"})


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
IV. Analytics Prep
In order to run the necessary analysis, I combined the World Bank Data and Freedom House datasets. To reflect the most up to date information I will only use data from 2015.
Can you write Python code for it?

combined_df = economic_df.xs('2015', level='date').join(freedomhouse_df[freedomhouse_df["date"] == 2015]).dropna()

combined_df

# Descriptive statistics on the merged World Bank Data and Freedom House data in 2015
combined_df.describe()


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
Step Two: View Freedom House Data
Can you write Python code for it?


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
The graph below depicts the densest GDP per capita’s divided by their freedom status. It seems that only "free" countries have GDP's over the one hundred thousand mark, but the range in the dataset is largest in free countries. Furthermore, with a mean GDP per capita of around $ 12,851, it makes sense that the density of all the voilins is highest in that range. Notably, partly free countries experience the lowest levels of economic development, even lower than those of not free countries. Moreover, partly free countries have the smallest range in GDP per capita, mostly concentrated in the zero to ten thousand range.
Can you write Python code for it?

f, ax = plt.subplots(figsize=(15, 10))
sns.violinplot(x="status", y="GDP Per Capita", data=combined_df, palette="Set3")


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
The table below provides a data dictionary of the aforementioned variables
Can you write Python code for it?

freedomhouse_dd = pandas.read_excel("fh1972_20161.xlsx", header=0, sheetname=0)

freedomhouse_dd.head(30)


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
The variables included in this dataset are:
Can you write Python code for it?


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
These two density plots show something very similar, more free countries have outliers that are extremely rich, something you don't see in the less free countries. The first graph divides the density of the dataset by the average freedom score. Further indicating that the largest range of GDP per capita is in the free countries. Evidently, extreme wealth is only found in democratic countries even though low levels of economic development can occur in democracies as well.
Can you write Python code for it?

f, ax = plt.subplots(figsize=(15, 10))
sns.violinplot(x="mean", y="GDP Per Capita", data=combined_df, palette="Set3")

sns.jointplot(x='GDP Per Capita', y="mean", data=combined_df, kind='kde', )


I am working on the file "MBA_S17/Sabbagh_Final_Project_Data_Bootcamp (1).ipynb".
The first task is:
V. Case Study: How Does the Wealthiest Country, Luxembourg, Change Over Time?
Can you write Python code for it?

#I sorted the data by the highest level of GDP per Capita. Luxembourg had the highest GDP per capita in 2015
combined_df.sort_values('GDP Per Capita', ascending=False)


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Average Price per sq. ft
Average price per sq.ft = Average price/finished square foot.
Can you write Python code for it?

z1['Avg. price per sq. ft'] = z1['Avg. Price']/z1['finished \n(SqFt)']

z1['Avg. price per sq. ft'].head()


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Calculating Average price of the property
Average price of the property = Zillow Estimate + Tax asssesment value + Average comps of similar property
Can you write Python code for it?

z1['Avg. Price'] = z1[['Zillow Estimate', 'taxAssessment', 'Average comps']].mean(axis=1)

list(z1)

z1['Avg. Price'].head()


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Calculating rank based on School rating
School ratings has been biffercated into various buckets based on the min, median and max. Please refer the below code for the buckets allocated.
Can you write Python code for it?

Schoolrank = []

for row in z1[' School Score  ']:
 if row>57.308:
  Schoolrank.append(1)
 elif row>43.816 and row<=57.308:
  Schoolrank.append(2)
 elif row>30.324 and row<=43.816:
  Schoolrank.append(3)
 elif row>16.832 and row<=30.324:
  Schoolrank.append(4)
 else:
  Schoolrank.append(5)

z1['School Rank'] = Schoolrank

zschool = z1.groupby(['School Rank'])['Avg. Price'].mean()
zschool


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Crime rate, Walks and School score in each postal code
Following are the observation based on analyzing average House price by Crime rate, walkscore and school score against postal code. The data that affected the price change most was crime for the city of Philadelphia. School rating also influenced the price of the property. Walkscore did not have a strong impact it was overridden by crime score but it had combined effect at the city center.
Can you write Python code for it?

plt.scatter(z['Postal Code'], z[' Violent Crime Rate '])
plt.show()

plt.scatter(z['Postal Code'], z[' Avg Walk&Transit score  '])
plt.show()

plt.scatter(z['Postal Code'], z[' School Score  '])
plt.show()


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Data Soure
Downloaded the Philadelphia real estate dataset from Kaggle and import using Pandas lib. Please refer the following link.
https://www.kaggle.com/harry007/philly-real-estate-data-set-sample/downloads/philly-real-estate-data-set-sample.zip
Can you write Python code for it?

Zillow = pd.ExcelFile("Properties_philly_Kraggle_v2.xlsx")
zz = Zillow.parse('Properties_philly_Kraggle_v2')
zz

print('Dimensions: ', zz.shape)  # looking at the categories I can work with

print('Column labels: ', zz.columns)   #Listing out the Column headings

print('Row labels: ', zz.index)

z = zz.dropna()       #Dropped empty rows that does have any data
print('Dimensions: ', z.shape)


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
House sales price by zipcode
Below analyis shows that most of the house price are below $150,000 in the city of Philadelphia.
Can you write Python code for it?

plt.scatter(z['Postal Code'], z['Sale Price/bid price'])
plt.show()

z1 = pd.DataFrame(z, columns = ['Address',
 'Zillow Address',
 'Sale Date',
 'Opening Bid',
 'Sale Price/bid price',
 'Book/Writ',
 'OPA',
 'Postal Code',
 'Attorney',
 'Ward',
 'Seller',
 'Buyer',
 'Sheriff Cost',
 'Advertising',
 'Other',
 'Record Deed',
 'Water',
 'PGW',
 ' Avg Walk&Transit score  ',
 ' Violent Crime Rate ',
 ' School Score  ',
 'Zillow Estimate',
 'Rent Estimate',
 'taxAssessment',
 'yearBuilt',
 'finished \n(SqFt)',
 ' bathrooms ',
 ' bedrooms ',
 'PropType',
 'Average comps'])


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
Impact of School score over sales price
School rating is also influenced the price of the property. Better school score also improves the demand of the property.
Can you write Python code for it?

plt.figure(figsize = (16,7))  # plotting our data
plt.plot(zschool, color = 'blue', marker= '*')
plt.suptitle('Average price by schoolrank', fontsize=18)
plt.xlabel('School Rank', fontsize=12)
plt.ylabel('Average price of houses', fontsize=12)
plt.show()

z1.head()


I am working on the file "MBA_S17/Sasidharan.ipynb".
The first task is:
z1['Avg. price per sq. ft'] = z1['Avg. Price']/z1['finished \n(SqFt)']
Can you write Python code for it?

z1['Closing Cost'] = z1['Avg. Price']*.085

 z1['Rehab Cost'] = z1['finished \n(SqFt)']*25

 z1['Estimated Max Bid Price']= z1['Avg. Price']-z1['Rehab Cost']

 z1.head()


I am working on the file "MBTR.ipynb".
The first task is:
Atom description
We'll make an ase.Atoms class for NaCl
Can you write Python code for it?


I am working on the file "MBTR.ipynb".
The first task is:
Setting MBTR hyper-parameters
Next we set-up hyper-parameters:
1. species, the chemical elements to include in the MBTR, helps comparing two structures with missing elements
2. k, list/set of K's to be computed
3. grid: dictionary for K1, K2, K3 with
    min, max: are the min and max values for each distribution
    sigma, the exponent coefficient for smearing
    n, number of bins.
4. weights: dictionary of weighting functions to be used.
Note: The dscribe package has implementation of MBTR up to K3
Can you write Python code for it?

# Create the MBTR desciptor for the system
mbtr = MBTR(
    species=['Na', 'Cl'], # Na and Cl
    periodic=True,
    k1={
        "geometry": { "function": "atomic_number" },
        "grid": { "min": 10, "max": 18, "sigma": 0.1, "n": 200 },
    },
    k2={
        "geometry": {"function": "inverse_distance"},
        "grid": { "min": 0, "max": 0.7, "sigma": 0.01, "n": 200 },
        "weighting": {"function": "exp", "scale": 0.75, "cutoff": 1e-3}
    },
    k3={
        "geometry": {"function": "cosine"},
        "grid": { "min": -1.0, "max": 1.0, "sigma": 0.05, "n": 200 },
        "weighting": {"function": "exp", "scale": 0.5, "cutoff": 1e-3}
    },

    flatten=False,
    sparse=False)
print("Number of features: {}".format(mbtr.get_number_of_features()))


I am working on the file "MC/ss_os_training.ipynb".
The first task is:
Calibration to probability to be SS
Can you write Python code for it?


I am working on the file "MC/ss_os_training.ipynb".
The first task is:
Import
Can you write Python code for it?

import pandas
import root_numpy
from folding_group import FoldingGroupClassifier
from decisiontrain import DecisionTrainClassifier
from rep.estimators import SklearnClassifier


I am working on the file "MC/ss_os_training.ipynb".
The first task is:
Read $B^\pm \to J\psi K^\pm$ MC samples
Can you write Python code for it?

data = pandas.DataFrame(root_numpy.root2array('../datasets/MC/csv/WG/Bu_JPsiK/2012/Tracks.root'))

from utils import data_tracks_preprocessing
data = data_tracks_preprocessing(data)

for group in range(-1, 2, 1):
    print group, 1. * numpy.sum(data.OS_SS.values == group) / len(data)

len(data)

features = ['cos_diff_phi', 'diff_pt', 'partPt', 'partP', 'nnkrec', 'diff_eta', 'EOverP',
            'ptB', 'sum_PID_mu_k', 'proj', 'PIDNNe', 'sum_PID_k_e', 'PIDNNk', 'sum_PID_mu_e', 'PIDNNm',
            'phi', 'IP', 'IPerr', 'IPs', 'veloch', 'max_PID_k_e', 'ghostProb',
            'IPPU', 'eta', 'max_PID_mu_e', 'max_PID_mu_k', 'partlcs']


I am working on the file "MC/ss_os_training.ipynb".
The first task is:
before calibration
Can you write Python code for it?

plot_calibration(prob, data_os_ss.OS_SS.values >= 0, weight=data_os_ss.weight.values)


I am working on the file "MC/ss_os_training.ipynb".
The first task is:
distributions for same side vs opposide side tracks
Can you write Python code for it?

kw = {'bins': 100, 'alpha': 0.4, 'normed': True}
figure(figsize=(20, 35))
for n, f in enumerate(features):
    subplot(10, 4, n + 1)
    r = (numpy.min(data.loc[data.OS_SS == -1, f].values), numpy.max(data.loc[data.OS_SS == -1, f].values))
    hist(data.loc[data.OS_SS == -1, f].values, label='OS', range=r, **kw)
    hist(data.loc[data.OS_SS == 0, f].values, label='NAN', range=r, **kw)
    hist(data.loc[data.OS_SS == 1, f].values, label='SS', range=r, **kw)
    title(f)
    legend()


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
As before, edit your implementation until the following test passes.
Can you write Python code for it?

p_1, p_2 = (1000.0, -1.), (1500., -2.)
ll_1, ll_2 = lnlike_poisson(p_1), lnlike_poisson(p_2)
if not np.allclose(ll_2 - ll_1, 337.039175916):
    raise ValueError("It looks like your implementation is wrong!")
print("☺")


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
As before, here's a simple test for this function. When you run the following cell it will either print a smile or throw an exception. Since the algorithm is random, it might occasionally fail this test so if it fails once, try running it again. If it fails a second time, edit your implementation until the test consistently passes.
Can you write Python code for it?

lptest = lambda x: -0.5 * np.sum(x**2)
th = np.array([0.0])
lp = 0.0
chain = np.array([th for th, lp in (metropolis_step(lptest, th, lp, [[0.3]])
                                    for _ in range(10000))])
if np.abs(np.mean(chain)) > 0.1 or np.abs(np.std(chain) - 1.0) > 0.1:
    raise ValueError("It looks like your implementation is wrong!")
print("☺")


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
If this works, the output should greet you without throwing any errors. If so, that's pretty much all we need so let's get started with some MCMC!
Dataset 1: Fitting a line to data
Today, we're going to implement the simplest possible MCMC algorithm but before we do that, we'll need some data to test our method with.
Load the data
I've generated a simulated dataset generated from a linear model with no uncertainties in the $x$ dimension and known Gaussian uncertainties in the $y$ dimension. These data are saved in the CSV file linear.csv included with this notebook.
First we'll need numpy and matplotlib so let's import them:
Can you write Python code for it?

%matplotlib inline
from matplotlib import rcParams
rcParams["savefig.dpi"] = 100  # This makes all the plots a little bigger.
import numpy as np
import matplotlib.pyplot as plt


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
If you don't get a smile here, that could mean a few things:

you didn't run for long enough (try increasing nstep),
your choice of step scale was not good (try playing around with the definition of step), or
there's a bug in your code.

Try out all of these tuning parameters until you have a good intuition for what's going on and figure out which settings pass this test and which don't.
Plotting the results
In this section, we'll make two plots that are very useful for checking your results after you run an MCMC:

corner plot or scatterplot matrix — a plot of all the 2- and 1-D projections of the MCMC samples. To make this plot, we'll use triangle.py, a Python module specifically designed for this purpose. For simplicity, I've included the module with this notebook so you won't have to install it separately.

predictive distribution — a plot of the "posterior predicted data" overplotted on the observed data. This kind of plot can be used as a qualitative model check.

First, the corner plot:
Can you write Python code for it?

import triangle
triangle.corner(chain[nburn:, :], labels=["m", "b"], truths=w);


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
If you don't get the ☺, go back and try to debug your model. Iterate until your result is correct.
Once you get that, we'll use this to implement the full model (Remember: we haven't added in the prior yet). For the purposes of this demonstration, we'll assume broad uniform priors on both $m$ and $b$. This isn't generally a good idea... instead, you should normally use a prior that actually represents your prior beliefs. But this a discussion for another day.
I've chosen to set the bounds on each parameter to be (-10, 10) but you should feel free to change these numbers. Since this is the log-prior, we'll return -np.inf from lnprior_linear when the parameter is outside of the allowed range. And then, since we only need to compute the probability up to a constant, we will return 0.0 (an arbitrary constant) when the parameters are valid.
Finally, the function lnpost_linear sums the output of lnprior_linear and lnlike_linear to compute the log-posterior probability up to a constant.
Can you write Python code for it?

def lnprior_linear((m, b)):
    if not (-10 < m < 10):
        return -np.inf
    if not (-10 < b < 10):
        return -np.inf
    return 0.0

def lnpost_linear(theta):
    return lnprior_linear(theta) + lnlike_linear(theta)


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
In the following cell, you need to implement the log-likelihood function for the problem (same as above):
$$\ln p({x_k}\,|\,\alpha,\,\beta) = \frac{\alpha}{\beta+1}\,\left[a^{\beta+1} - b^{\beta+1}\right] + K\,\ln\alpha + \sum_{k=1}^K \beta\,\ln x_k + \mathrm{const}$$
Note that this is only valid for $\beta \ne -1$. In practice you shouldn't ever hit this boundary but, just in case,  you should special case beta == 1.0 where
$$\ln p({x_k}\,|\,\alpha,\,\beta=-1) = \alpha\,\left[\ln a - \ln b\right] + K\,\ln\alpha - \sum_{k=1}^K \ln x_k + \mathrm{const}$$
Can you write Python code for it?

sme = np.sum(np.log(events))
K = len(events)
def lnlike_poisson((alpha, beta)):
    if beta == -1.0:
        norm = alpha * (np.log(a) - np.log(b)) + K * np.log(alpha)
    else:
        bp1 = beta+1
        norm = alpha / bp1 * (a**bp1-b**bp1) + K * np.log(alpha)
    return norm + beta * sme


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
In the next cell, we'll start from this initial guess for the slope and intercept and walk through parameter space (using the transition probability from above) to generate a Markov Chain of samples from the posterior probability.
There are a few tuning parameters for the method. The first and most important choice has already been covered: initialization. The practical performance of an MCMC sampler depends sensitively on the initial position so it's worth spending some time choosing a good initialization.
The second big tuning parameter is the scale of the proposal distribution. We must specify the covariance matrix for the proposal Gaussian. This proposal is currently set to a very bad value. Your job is to run the sampler, look at the output, and try to tune the proposal until you find a "good" value. You will judge this based on a few things. First, you can check the acceptance fraction (the fraction of accepted proposals). For this (easy!) problem, the target is around about 50% but for harder problems in higher dimensions, a good target is around 20%. Another useful diagnostic is a plot of the parameter values as a function of step number. For example, if this looks like a random walk then your proposal scale is probably too small. Once you reach a good proposal, this plot should "look converged".
The final tuning parameter is the number of steps to take. In theory, you need to take an infitite number of steps but we don't (ever) have time for that so instead you'll want to take a large enough number of samples so that the sampler has sufficiently explored parameter space and converged to a stationary distribution. This is, of course, unknowable so for today you'll just have to go with your intuition.
You can also change the number of steps that are discarded as burn-in but (in this problem) your results shouldn't be very sensitive to this number.
Take some time now to adjust these tuning parameters and get a sense of what happens to the sampling when you change different things.
Can you write Python code for it?

# Edit this line to specify the proposal covariance:
step = np.diag([1e-4, 1e-4])

# Edit this line to choose the number of steps you want to take:
nstep = 50000

# Edit this line to set the number steps to discard as burn-in.
nburn = 1000

# You shouldn't need to change any of the lines below here.
p0 = np.array([m_initial, b_initial])
lp0 = lnpost_linear(p0)
chain = np.empty((nstep, len(p0)))
for i in range(len(chain)):
    p0, lp0 = metropolis_step(lnpost_linear, p0, lp0, step)
    chain[i] = p0

# Compute the acceptance fraction.
acc = float(np.any(np.diff(chain, axis=0), axis=1).sum()) / (len(chain)-1)
print("The acceptance fraction was: {0:.3f}".format(acc))

# Plot the traces.
fig, axes = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
axes[0].plot(chain[:, 0], "k")
axes[0].axhline(w[0], color="g", lw=1.5)
axes[0].set_ylabel("m")
axes[0].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].plot(chain[:, 1], "k")
axes[1].axhline(w[1], color="g", lw=1.5)
axes[1].set_ylabel("b")
axes[1].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].set_xlabel("step number")
axes[0].set_title("acceptance: {0:.3f}".format(acc));


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
Now let's run the MCMC for this model. As before, you should tune the parameters of the algorithm until you get a reasonable acceptance fraction ($\sim 25- 40\%$) and the chains seem converged.
Can you write Python code for it?

# Edit this line to specify the proposal covariance:
step = np.diag([100., 1e-1])

# Edit this line to choose the number of steps you want to take:
nstep = 50000

# Edit this line to set the number steps to discard as burn-in.
nburn = 1000

# You shouldn't need to change any of the lines below here.
p0 = np.array([alpha_initial, beta_initial])
lp0 = lnpost_poisson(p0)
chain = np.empty((nstep, len(p0)))
for i in range(len(chain)):
    p0, lp0 = metropolis_step(lnpost_poisson, p0, lp0, step)
    chain[i] = p0

# Compute the acceptance fraction.
acc = float(np.any(np.diff(chain, axis=0), axis=1).sum()) / (len(chain)-1)
print("The acceptance fraction was: {0:.3f}".format(acc))

# Plot the traces.
fig, axes = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
axes[0].plot(chain[:, 0], "k")
axes[0].set_ylabel("alpha")
axes[0].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].plot(chain[:, 1], "k")
axes[1].set_ylabel("beta")
axes[1].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].set_xlabel("step number")
axes[0].set_title("acceptance: {0:.3f}".format(acc));


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
Now we'll load the datapoints and plot them. When you execute the following cell, you should see a plot of the data. If not, make sure that you run the import cell from above first.
Can you write Python code for it?

# Load the data from the CSV file.
x, y, yerr = np.loadtxt("linear.csv", delimiter=",", unpack=True)

# Plot the data with error bars.
plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
plt.xlim(0, 5);


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
The results of the MCMC run are stored in the array called chain with dimensions (nstep, 2). These are samples from the posterior probability density for the parameters. We know from above that this should be a Gaussian with mean $\mathbf{w}$ and covariance $\mathbf{V}$ so let's compare the sample mean and covariance to the analytic result that we computed above:
Can you write Python code for it?

if np.any(np.abs(np.mean(chain, axis=0)-w)>0.01) or np.any(np.abs(np.cov(chain, rowvar=0)-V)>1e-4):
    raise ValueError("It looks like your implementation is wrong!")
print("☺")


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
This plot is a representation of our contraints on the posterior probability for the slope and intercept conditioned on the data. The 2-D plot shows the full posterior and the two 1-D plots show the constraints for each parameter marginalized over the other.
The second plot that we want to make is a represnetation of the posterior predictive distribution for the data. To do this we will plot a few (50) randomly selected samples from the chain and overplot the resulting line on the data.
Can you write Python code for it?

plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
for m, b in chain[nburn+np.random.randint(len(chain)-nburn, size=50)]:
    plt.plot(x, m*x + b, "g", alpha=0.1)
plt.xlim(0, 5);


I am working on the file "MCMC tutorial (solutions).ipynb".
The first task is:
This plot is a visualization of our posterior expectations for the true underlying line that generated these data. We'll reuse this plot a few times later to test the results of our code.
The probabilistic model
In order use MCMC to perform posterior inference on a model and dataset, we need a function that computes the value of the posterior probability given a proposed setting of the parameters of the model. For reasons that will become clear below, we actually only need to return a value that is proportional to the probability.
As discussed in a previous tutorial, the posterior probability for parameters $\mathbf{w} = (m,\,b)$ conditioned on a dataset $\mathbf{y}$ is given by
$$p(\mathbf{w} \,|\, \mathbf{y}) = \frac{p(\mathbf{y} \,|\, \mathbf{w}) \, p(\mathbf{w})}{p(\mathbf{y})}$$
where $p(\mathbf{y} \,|\, \mathbf{w})$ is the likelihood and $p(\mathbf{w})$ is the prior. For this example, we're modeling the likelihood by assuming that the datapoints are independent with known Gaussian uncertainties $\sigma_n$. This specifies a likelihood function:
$$p(\mathbf{y} \,|\, \mathbf{w}) = \prod_{n=1}^N \frac{1}{\sqrt{2\,\pi\,\sigma_n^2}} \,
\exp \left(-\frac{[y_n - f_\mathbf{w}(x_n)]^2}{2\,\sigma_n^2}\right)$$
where $f_\mathbf{w}(x) = m\,x + b$ is the linear model.
For numerical reasons, we will acutally want to compute the logarithm of the likelihood. In this case, this becomes:
$$\ln p(\mathbf{y} \,|\, \mathbf{w}) = -\frac{1}{2}\sum_{n=1}^N \frac{[y_n - f_\mathbf{w}(x_n)]^2}{\sigma_n^2} + \mathrm{constant} \quad.$$
In the following cell, replace the contents of the lnlike_linear function to implement this model. The function takes two values (m and b) as input and it should return the log likelihood (a single number) up to a constant. In this function, you can just use the globaly defined dataset x, y and yerr. For performance, I recommend using vectorized numpy operations (the key function will be np.sum).
Can you write Python code for it?

def lnlike_linear((m, b)):
    # CODE HERE
    f = m*x + b
    return -0.5 * np.sum(((y-f) / yerr) ** 2)


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
As I mentioned previously, it is pretty silly to use MCMC to solve this problem because the maximum likelihood and full posterior probability distribution (under infinitely broad priors) for the slope and intercept of the line are known analytically. Therefore, let's compute what the right answer should be before we even start. The analytic result for the posterior probability distribution is a 2-d Gaussian with mean
$$\mathbf{w} = \left(\begin{array}{c}
    m \ b
\end{array}\right) = (\mathbf{A}^\mathrm{T}\,C^{-1}\mathbf{A})^{-1} \, \mathbf{A}^\mathrm{T}\,C^{-1}\,\mathbf{y}$$
and covariance matrix
$$\mathbf{V} = (\mathbf{A}^\mathrm{T}\,C^{-1}\mathbf{A})^{-1}$$
where
$$\mathbf{y} = \left(\begin{array}{c}
y_1 \ y_2 \ \vdots \ y_N
\end{array}\right) \quad , \quad \mathbf{A} = \left(\begin{array}{cc}
x_1 & 1 \ x_2 & 1 \ \vdots & \vdots \ x_N & 1
\end{array}\right) \quad ,\, \mathrm{and} \quad
\mathbf{C} = \left(\begin{array}{cccc}
\sigma_1^2 & 0 & \cdots & 0 \
0 & \sigma_2^2 & \cdots & 0 \
&&\ddots& \
0 & 0 & \cdots & \sigma_N^2
\end{array}\right)$$
There are various functions in Python for computing this but I prefer to do it myself (it only takes a few lines of code!) and here it is:
Can you write Python code for it?

A = np.vander(x, 2)  # Take a look at the documentation to see what this function does!
ATA = np.dot(A.T, A / yerr[:, None]**2)
w = np.linalg.solve(ATA, np.dot(A.T, y / yerr**2))
V = np.linalg.inv(ATA)


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
As before, edit your implementation until the following test passes.
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
As before, here's a simple test for this function. When you run the following cell it will either print a smile or throw an exception. Since the algorithm is random, it might occasionally fail this test so if it fails once, try running it again. If it fails a second time, edit your implementation until the test consistently passes.
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
If this works, the output should greet you without throwing any errors. If so, that's pretty much all we need so let's get started with some MCMC!
Dataset 1: Fitting a line to data
Today, we're going to implement the simplest possible MCMC algorithm but before we do that, we'll need some data to test our method with.
Load the data
I've generated a simulated dataset generated from a linear model with no uncertainties in the $x$ dimension and known Gaussian uncertainties in the $y$ dimension. These data are saved in the CSV file linear.csv included with this notebook.
First we'll need numpy and matplotlib so let's import them:
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
In the following cell, you need to implement the log-likelihood function for the problem (same as above):
$$\ln p({x_k}\,|\,\alpha,\,\beta) = \frac{\alpha}{\beta+1}\,\left[a^{\beta+1} - b^{\beta+1}\right] + K\,\ln\alpha + \sum_{k=1}^K \beta\,\ln x_k + \mathrm{const}$$
Note that this is only valid for $\beta \ne -1$. In practice you shouldn't ever hit this boundary but, just in case,  you should special case beta == 1.0 where
$$\ln p({x_k}\,|\,\alpha,\,\beta=-1) = \alpha\,\left[\ln a - \ln b\right] + K\,\ln\alpha - \sum_{k=1}^K \ln x_k + \mathrm{const}$$
Can you write Python code for it?

def lnlike_poisson((alpha, beta)):
    raise NotImplementedError("Delete this placeholder and implement this function")


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
In the next cell, we'll start from this initial guess for the slope and intercept and walk through parameter space (using the transition probability from above) to generate a Markov Chain of samples from the posterior probability.
There are a few tuning parameters for the method. The first and most important choice has already been covered: initialization. The practical performance of an MCMC sampler depends sensitively on the initial position so it's worth spending some time choosing a good initialization.
The second big tuning parameter is the scale of the proposal distribution. We must specify the covariance matrix for the proposal Gaussian. This proposal is currently set to a very bad value. Your job is to run the sampler, look at the output, and try to tune the proposal until you find a "good" value. You will judge this based on a few things. First, you can check the acceptance fraction (the fraction of accepted proposals). For this (easy!) problem, the target is around about 50% but for harder problems in higher dimensions, a good target is around 20%. Another useful diagnostic is a plot of the parameter values as a function of step number. For example, if this looks like a random walk then your proposal scale is probably too small. Once you reach a good proposal, this plot should "look converged".
The final tuning parameter is the number of steps to take. In theory, you need to take an infitite number of steps but we don't (ever) have time for that so instead you'll want to take a large enough number of samples so that the sampler has sufficiently explored parameter space and converged to a stationary distribution. This is, of course, unknowable so for today you'll just have to go with your intuition.
You can also change the number of steps that are discarded as burn-in but (in this problem) your results shouldn't be very sensitive to this number.
Take some time now to adjust these tuning parameters and get a sense of what happens to the sampling when you change different things.
Can you write Python code for it?

# Edit this line to specify the proposal covariance:
step = np.diag([1e-6, 1e-6])

# Edit this line to choose the number of steps you want to take:
nstep = 50000

# Edit this line to set the number steps to discard as burn-in.
nburn = 1000

# You shouldn't need to change any of the lines below here.
p0 = np.array([m_initial, b_initial])
lp0 = lnpost_linear(p0)
chain = np.empty((nstep, len(p0)))
for i in range(len(chain)):
    p0, lp0 = metropolis_step(lnpost_linear, p0, lp0, step)
    chain[i] = p0

# Compute the acceptance fraction.
acc = float(np.any(np.diff(chain, axis=0), axis=1).sum()) / (len(chain)-1)
print("The acceptance fraction was: {0:.3f}".format(acc))

# Plot the traces.
fig, axes = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
axes[0].plot(chain[:, 0], "k")
axes[0].axhline(w[0], color="g", lw=1.5)
axes[0].set_ylabel("m")
axes[0].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].plot(chain[:, 1], "k")
axes[1].axhline(w[1], color="g", lw=1.5)
axes[1].set_ylabel("b")
axes[1].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].set_xlabel("step number")
axes[0].set_title("acceptance: {0:.3f}".format(acc));


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
Metropolis(–Hastings) MCMC
The simplest MCMC algorithm is generally referred to as the Metropolis method. All MCMC algorithms work by specifying a "step" that moves from one position in parameter space to another with some probability. The Metropolis step takes a position $\theta_t$ (a vector containing the slope and intercept at step $t$) to the position $\theta_{t+1}$ using the following steps:

propose a new position $\mathbf{q}$ drawn from a Gaussian centered on the current position $\theta_t$
compute the probability of the new position $p(\mathbf{q}\,|\,\mathbf{y})$
draw a random number $r$ between 0 and 1 and if
$$r < \frac{p(\mathbf{q}\,|\,\mathbf{y})}{p(\mathbf{x}t\,|\,\mathbf{y})}$$
return $\mathbf{q}$ as $\theta{t+1}$ and, otherwise, return $\theta_t$ as $\theta_{t+1}$.

In the following cell, you'll implement this step. The function will take 4 arguments:

a function that computes the ln-probability (for this demo, it'll be lnpost_linear from above),
the current position $\theta_t$,
the ln-probability at the current point $p(\theta_t\,|\,\mathbf{y})$, and
the covariance matrix of the Gaussian proposal distribution.

It should return two values, the new coordinate $\theta_{t+1}$ and the ln-probability at that point $p(\theta_{t+1}\,|\,\mathbf{y})$. The syntax for returning multiple values is return a, b.
This function is really the key to this whole tutorial so spend some time getting it right! It is hard to robustly test functions with a random component so chat with other people around you to check your method. We'll also try to test it below but it's worth spending some time now.
There are a few functions that will come in handy here but the two most important ones are:

np.random.multivariate_normal(theta_t, step_cov) - draws a vector sample from the multivariate Gaussian centered on theta_t with covariance matrix step_cov.
np.random.rand() - draws a random number between 0 and 1.
Can you write Python code for it?

def metropolis_step(lnpost_function, theta_t, lnpost_t, step_cov):
    raise NotImplementedError("Delete this placeholder and implement this function")


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
Now let's run the MCMC for this model. As before, you should tune the parameters of the algorithm until you get a reasonable acceptance fraction ($\sim 25- 40\%$) and the chains seem converged.
Can you write Python code for it?

# Edit this line to specify the proposal covariance:
step = np.diag([1000., 4.])

# Edit this line to choose the number of steps you want to take:
nstep = 50000

# Edit this line to set the number steps to discard as burn-in.
nburn = 1000

# You shouldn't need to change any of the lines below here.
p0 = np.array([alpha_initial, beta_initial])
lp0 = lnpost_poisson(p0)
chain = np.empty((nstep, len(p0)))
for i in range(len(chain)):
    p0, lp0 = metropolis_step(lnpost_poisson, p0, lp0, step)
    chain[i] = p0

# Compute the acceptance fraction.
acc = float(np.any(np.diff(chain, axis=0), axis=1).sum()) / (len(chain)-1)
print("The acceptance fraction was: {0:.3f}".format(acc))

# Plot the traces.
fig, axes = plt.subplots(2, 1, figsize=(8, 5), sharex=True)
axes[0].plot(chain[:, 0], "k")
axes[0].set_ylabel("alpha")
axes[0].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].plot(chain[:, 1], "k")
axes[1].set_ylabel("beta")
axes[1].axvline(nburn, color="g", alpha=0.5, lw=2)
axes[1].set_xlabel("step number")
axes[0].set_title("acceptance: {0:.3f}".format(acc));


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
Now we'll load the datapoints and plot them. When you execute the following cell, you should see a plot of the data. If not, make sure that you run the import cell from above first.
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
Once you're happy with to convergence of your chain, plot the results as a corner plot (compared to the values that I used to generate the dataset; $\alpha = 500$ and $\beta = -2$) and plot the posterior predictive distribution.
Can you write Python code for it?

triangle.corner(chain[nburn:], labels=["alpha", "beta"], truths=[500, -2]);

plt.hist(events, bins, range=(a, b), histtype="step", color="k", lw=2, weights=weights)

# Plot the guess at the rate.
xx = np.linspace(a, b, 500)
for alpha, beta in chain[nburn+np.random.randint(len(chain)-nburn, size=50)]:
    plt.plot(xx, alpha * xx ** beta, "g", alpha=0.1)

# Format the figure.
plt.ylabel("number")
plt.xlabel("x");


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
Running the Markov Chain
Now that we have an implementation of the Metropolis step, we can go on to sample from the posterior probability density that we implemented above. To start, we need to initialize the sampler somewhere in parameter space. In the following cell, edit your guess for the slope and intercept of the line until it looks like a reasonably good fit to the data.
Can you write Python code for it?

# Edit these guesses.
m_initial = 2.
b_initial = 0.45

# You shouldn't need to change this plotting code.
plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
for m, b in np.random.multivariate_normal(w, V, size=24):
    plt.plot(x, m_initial*x + b_initial, "g", alpha=0.1)
plt.xlim(0, 5);


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
The results of the MCMC run are stored in the array called chain with dimensions (nstep, 2). These are samples from the posterior probability density for the parameters. We know from above that this should be a Gaussian with mean $\mathbf{w}$ and covariance $\mathbf{V}$ so let's compare the sample mean and covariance to the analytic result that we computed above:
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
This plot is a representation of our contraints on the posterior probability for the slope and intercept conditioned on the data. The 2-D plot shows the full posterior and the two 1-D plots show the constraints for each parameter marginalized over the other.
The second plot that we want to make is a represnetation of the posterior predictive distribution for the data. To do this we will plot a few (50) randomly selected samples from the chain and overplot the resulting line on the data.
Can you write Python code for it?


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
This plot is a visualization of our posterior expectations for the true underlying line that generated these data. We'll reuse this plot a few times later to test the results of our code.
The probabilistic model
In order use MCMC to perform posterior inference on a model and dataset, we need a function that computes the value of the posterior probability given a proposed setting of the parameters of the model. For reasons that will become clear below, we actually only need to return a value that is proportional to the probability.
As discussed in a previous tutorial, the posterior probability for parameters $\mathbf{w} = (m,\,b)$ conditioned on a dataset $\mathbf{y}$ is given by
$$p(\mathbf{w} \,|\, \mathbf{y}) = \frac{p(\mathbf{y} \,|\, \mathbf{w}) \, p(\mathbf{w})}{p(\mathbf{y})}$$
where $p(\mathbf{y} \,|\, \mathbf{w})$ is the likelihood and $p(\mathbf{w})$ is the prior. For this example, we're modeling the likelihood by assuming that the datapoints are independent with known Gaussian uncertainties $\sigma_n$. This specifies a likelihood function:
$$p(\mathbf{y} \,|\, \mathbf{w}) = \prod_{n=1}^N \frac{1}{\sqrt{2\,\pi\,\sigma_n^2}} \,
\exp \left(-\frac{[y_n - f_\mathbf{w}(x_n)]^2}{2\,\sigma_n^2}\right)$$
where $f_\mathbf{w}(x) = m\,x + b$ is the linear model.
For numerical reasons, we will acutally want to compute the logarithm of the likelihood. In this case, this becomes:
$$\ln p(\mathbf{y} \,|\, \mathbf{w}) = -\frac{1}{2}\sum_{n=1}^N \frac{[y_n - f_\mathbf{w}(x_n)]^2}{\sigma_n^2} + \mathrm{constant} \quad.$$
In the following cell, replace the contents of the lnlike_linear function to implement this model. The function takes two values (m and b) as input and it should return the log likelihood (a single number) up to a constant. In this function, you can just use the globaly defined dataset x, y and yerr. For performance, I recommend using vectorized numpy operations (the key function will be np.sum).
Can you write Python code for it?

def lnlike_linear((m, b)):
    raise NotImplementedError("Delete this placeholder and implement this function")


I am working on the file "MCMC tutorial (worksheet).ipynb".
The first task is:
We'll save these results for later to compare them to the result computed using MCMC but for now, it's nice to take a look and see what this prediction looks like. To do this, we'll sample 24 slopes and intercepts from this 2d Gaussian and overplot them on the data.
Can you write Python code for it?

plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
for m, b in np.random.multivariate_normal(w, V, size=50):
    plt.plot(x, m*x + b, "g", alpha=0.1)
plt.xlim(0, 5);


I am working on the file "MCMC-exemplo.ipynb".
The first task is:
Para resolver este problema precisamos definir algumas funções para simplificar a codificação. Para realizar a amostragem vamos usar o esquema básico de Metropolis–Hastings (https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm). Neste esquema precisamos definir a função verosemelhança do problema e a função de probabilidade que será usada para as amostragens propostas.
Can you write Python code for it?

def likelihood(x, y,yerr,a,b):
    # definição do problema de ajuste de uma reta y=ax+b

    L = 1.
    for i in range(x.size):
        L *= np.exp(-0.5*(y[i]-(a*x[i]+b))**2/yerr[i]**2)
        #print(np.exp(-0.5*(y[i]-(a*x[i]+b))**2/yerr[i]**2))

#    if (np.isfinite(L) == False):
#        L = 0.
    if (b<3. or b>5.):
        L=1.e-300

    return L

def proposal(a,b):
    P = 1.
    return P


I am working on the file "MCMC_varios.ipynb".
The first task is:
Ejercicio 2
Markov cahain. reinicia en el borde
Can you write Python code for it?

def MCpi_1(N,delta):
    count = 0
    tries = N
    puntos = np.zeros((2,N))
    puntos[:,0] = np.random.uniform(-1.0,1.0,size=2)
    for i in range (1,N):
        prop_x = puntos[0,i-1] + delta*np.random.uniform(-1.0, 1.0)
        prop_y = puntos[1,i-1] + delta*np.random.uniform(-1.0, 1.0)
        if abs(prop_x) <= 1.0 and abs(prop_y) <= 1.0:
            puntos[0,i] = prop_x
            puntos[1,i] = prop_y
            if (puntos[0,i]**2 + puntos[1,i]**2 <= 1.0): count += 1
        else:
            puntos[:,i] = np.random.uniform(-1.0,1.0,size=2)
            tries -= 1
    return 4.0*count/float(tries)


I am working on the file "MCMC_varios.ipynb".
The first task is:
Ejercicio 3
Aquí no estoy seguro que la bolita se mueva con las probabilidades que ponen en las reglas, pero al parecer si tiende a una dist. uniforme
Can you write Python code for it?

def bolita(T):
    grid = np.zeros((3,3))
    fil, col = np.random.randint(0,3), np.random.randint(0,3)
    grid[fil,col] += 1
    t=0
    while t < T:
        n_fil = fil + np.random.randint(-1,2)
        n_col = col + np.random.randint(-1,2)
        if (0 <= n_fil <= 2):
            if (0 <= n_col <= 2):
                fil = n_fil
                col = n_col
        grid[fil,col] += 1
        t+= 1
    return grid


I am working on the file "MCMC_varios.ipynb".
The first task is:
Grafica del rms en funcion de n_trials. Escala log log
Can you write Python code for it?

fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(n_trials, rms)
ax.plot(n_trials, rms, '--')
ax.set_yscale('log')
ax.set_xscale('log')


I am working on the file "MCMC_varios.ipynb".
The first task is:
Histogramas de freq absoluta de la pos de la bolita. cada casilla del tablero corresponde a un número del 1 al 9. Y de una vez calculamos la divergencia Kullback–Leibler
Can you write Python code for it?

tes = [1000, 500, 200, 100, 50, 10]
DKL = []
pos = np.arange(1,10)
fig = plt.figure(figsize=(15,7))
i = 1
for t in tes:
    rrr = bolita(t)
    aaa = rrr.flatten()
    dist = aaa/t
    DKL.append(np.sum(dist*np.log(dist/(1./9.))))
    ax = fig.add_subplot(2,3,i)
    ax.bar(pos,aaa)
    ax.set_title("t= {}".format(t))
    i += 1


I am working on the file "MCMC_varios.ipynb".
The first task is:
Markov cahain. se queda donde está en el borde
Can you write Python code for it?

def MCpi_2(N,delta):
    count = 0
    puntos = np.zeros((2,N))
    puntos[:,0] = np.random.uniform(-1.0,1.0,size=2)
    for i in range (1,N):
        prop_x = puntos[0,i-1] + delta*np.random.uniform(-1.0, 1.0)
        prop_y = puntos[1,i-1] + delta*np.random.uniform(-1.0, 1.0)
        if abs(prop_x) <= 1.0 and abs(prop_y) <= 1.0:
            puntos[0,i] = prop_x
            puntos[1,i] = prop_y
            if (puntos[0,i]**2 + puntos[1,i]**2 <= 1.0): count += 1
        else:
            puntos[:,i] = puntos[:,i-1]
            if (puntos[0,i]**2 + puntos[1,i]**2 <= 1.0): count += 1
    return 4.0*count/float(N)


I am working on the file "MCMC_varios.ipynb".
The first task is:
comparación valores de $\pi$
Can you write Python code for it?

N = 200
delta = 0.4
pi_1 = MCpi_1(N,delta)
pi_2 = MCpi_2(N,delta)
print(pi_1, pi_2)


I am working on the file "MCMC_varios.ipynb".
The first task is:
grafica de la divergencia Kullback–Leibler vs. n_pasos
Can you write Python code for it?


I am working on the file "MCMC_varios.ipynb".
The first task is:
rms vs. n_trials para diferentes $\delta$'s. 500 intentos para cada n_trial
Can you write Python code for it?

deltas = [0.062, 0.125, 0.25, 0.5, 1.0, 2.0, 4.0]

fig2 = plt.figure(figsize=(7,7))
ax = fig2.add_subplot(111)

for delta in deltas:
    rms = []
    for number in n_trials:
        pi_est = np.zeros(n_runs)
        for i in range(n_runs):
            pi_est[i] = MCpi_2(number,delta)

        rms.append(np.sqrt(np.mean((pi_est - np.pi)**2)))
    ax.scatter(n_trials, rms, label='$\delta\ =\ {}$'.format(delta))
    ax.set_yscale('log')
    ax.set_xscale('log')
ax.legend()
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
(3) Plot sample paths of $p_t$ and $d_t$.
Can you write Python code for it?

paths = [res.p, res.d]
labels = [r'$p_t$', r'$d_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
(5) Experiment with a different $G_S$ matrix.
Can you write Python code for it?

# Change G_s[0, 1] from -0.03 to -1
G_S_2 = [[-0.02, -1],
         [-0.01, -0.04]]

ap_2 = AssetPricingMultFiniteMarkov(mc, G_S_2, G_d)

ap_2.v

res_2 = ap_2.simulate(ts_length)

paths = [res_2.p, res_2.d]
labels = [r'$p_t$', r'$d_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Another class of examples
Let the elements of ${\sf D}$ (i.e., the multiplicative growth rates of the dividend or consumption process) be, for example,
$$ {\sf D} = \begin{bmatrix} .95 & .975 &  1 \cr
      .975 &  1 & 1.025 \cr
       1 & 1.025 & 1.05 \end{bmatrix}.$$
Here the realized growth rate depends on both $X_t$ and $X_{t+1}$ -- i.e., the value of the state last period (i) and this period (j).
Here we  have imposed symmetry to save parameters, but of course there is no reason to do that.
We can combine this specification with various specifications of $P$ matrices e.g., an "i.i.d." state evolution process would be represented with  $P$ in which all rows are identical.  Even that simple specification
can some interesting outcomes with the above ${\sf D}$.
We'll try this little  $3 \times 3$ example with a Lucas model below.
But first a word of caution.
We have to choose values for the consumption growth rate matrix $G_C$ and
the transition matrix $P$ so that pertinent  eigenvalues are smaller than one in modulus.
This check is implemented in the code.
Can you write Python code for it?

# Growth rate matrix
G_C = np.log([[.95  , .975, 1],
              [.975, 1 , 1.025],
              [1, 1.025, 1.05]])

# MarkovChain instance
P = [[0.1, 0.6, 0.3],
     [0.1, 0.5, 0.4],
     [0.1, 0.6, 0.3]]
mc = MarkovChain(P)

# Discount rate
delta = .01

# Coefficient of relative risk aversion
gamma = 20

lt = LucasTreeFiniteMarkov(mc, G_C, gamma, delta)

# Price-dividend ratios
lt.v

ts_length = 250
res = lt.simulate(ts_length)

paths = [res.S, res.d]
labels = [r'$S_t$', r'$C_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()

paths = [res.p, res.d]
labels = [r'$p_t$', r'$C_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Clearly, this Markov chain is irreducible:
Can you write Python code for it?

mc.is_irreducible

# Growth rate matrix
G = [[-1, 0],
     [0.5, 1]]


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Create a LucasTreeFiniteMarkov instance:
Can you write Python code for it?

lt = LucasTreeFiniteMarkov(mc, G_C, gamma, delta)

# Consumption growth rates
lt.G_C[0]

# Stochastic discount rates
lt.G_S[0]


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Let us simulate our MultFunctionalFiniteMarkov:
Can you write Python code for it?

ts_length = 10
res = mf.simulate(ts_length)


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Likelihood ratio processes
A likelihood ratio process is a multiplicative martingale with mean $1$.
A multiplicative martingale process ${\widetilde M_t }_{t=0}^\infty$ that starts from $\widetilde M_0 = 1$ is a likelihood ratio process.
Evidently, a likelihood ratio process satisfies
$$ E [\widetilde M_t \mid {\mathfrak F}_0] = 1 .$$
Hansen and Sargent (2017) point out that likelihood ratio processes have the following peculiar property:

Although  $E{\widetilde M}{j} = 1$ for each $j$, ${{\widetilde M}{j} : j=1,2,... }$ converges almost surely to zero.

The following graph, and also one at the end of this notebook, illustrate the peculiar property by reporting simulations of many sample paths of a ${\widetilde M_t }_{t=0}^\infty$ process.
Can you write Python code for it?

ts_length = 120
num_reps = 100
res = mf.simulate(ts_length, num_reps=num_reps)

ylim = (0, 50)
fig, ax = plt.subplots(figsize=(8,5))
for i in range(num_reps):
    ax.plot(res.M_tilde[i], color='k', alpha=0.5)
    ax.set_xlim(0, ts_length)
    ax.set_ylim(*ylim)
ax.set_title(r'{0} sample paths of $\widetilde M_t$'.format(num_reps))
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Plan of the notebook
In other quant-econ lectures
("Markov Asset Pricing" and
"The Lucas Asset Pricing Model"),
we have studied the celebrated Lucas asset pricing model (Lucas (1978)) that is cast in a setting in which the key objects of the theory,
namely, a stochastic discount factor process, an aggregate consumption process, and an
asset payout process, are all taken to be stationary.
In this notebook, we shall learn about some tools that allow us to extend asset pricing models to settings in which neither the stochastic discount factor process nor the asset's payout process is a stationary process.  The key tool is the class of multiplicative functionals from the stochastic process literature that Hansen and Scheinkman (2009) have adapted so that they can be applied to asset pricing and other interesting macroeconomic problems.
In this notebook, we confine ourselves to studying a special type of multiplicative functional, namely, multiplicative functionals driven by finite state Markov chains.  We'll learn about some  of their properties and applications.  Among other things, we'll
obtain Hansen and Scheinkman's more general multiplicative decomposition of our particular type of multiplicative functional into the following three primitive types of multiplicative functions:

a nonstochastic process displaying deterministic exponential growth;

a multiplicative martingale or likelihood ratio process; and

a stationary stochastic process that is the exponential of another stationary process.

The first two of these primitive types are nonstationary while the third is stationary.  The first is nonstochastic, while the second and third are stochastic.
After taking a look at the behavior of these three primitive components, we'll apply this structure to model

a stochastically, exponentially declining stochastic discount factor process;

a stochastically,  exponentially growing or declining asset payout or dividend process;

the prices of claims to exponentially growing or declining payout processes;...
Can you write Python code for it?

# Transition probability matrix
P = [[0.4, 0.6],
     [0.2, 0.8]]

# Instance of MarkovChain from quantecon.markov
mc = MarkovChain(P)


I am working on the file "MC_mult_functional.ipynb".
The first task is:
Simulate the model:
Can you write Python code for it?

ts_length = 250
res = lt.simulate(ts_length)

paths = [res.S, res.d]
labels = [r'$S_t$', r'$C_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()

print("price-dividend ratio in different states = ")
lt.v

paths = [res.p, res.d]
labels = [r'$p_t$', r'$C_t$']
titles = ['Sample path of ' + label for label in labels]

fig, axes = plt.subplots(2, 1, figsize=(8,10))
for ax, path, label, title in zip(axes, paths, labels, titles):
    ax.plot(path, label=label)
    ax.set_title(title)
    ax.legend(loc=loc)
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The (normalized) dominant eigenvector $e$ of $\widetilde P$ is
Can you write Python code for it?


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The dominant eigenvalue, denoted $\exp(\eta)$ above, of $\widetilde P$ is
Can you write Python code for it?


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The peculiar sample path property revisited
Consider again the multiplicative martingale that associated with the $5$ state Lucas model  studied earlier.
Remember that by construction, this is a likelihood ratio process.
Here we'll simulate a number of paths and build up histograms of $\widetilde M_t$ at various values of $t$.
These histograms should help us understand what is going on to generate the peculiar property mentioned above.
As $t \rightarrow +\infty$, notice that

more and more probability mass piles up near zero, $\ldots$ but

a longer and longer  thin right tail emerges.
Can you write Python code for it?

mf.P

T = 200
num_reps = 10**5
res = mf.simulate(T+1, num_reps=num_reps)

bins = np.linspace(0, 5, num=21)
bins_mid = (bins[:-1] + bins[1:]) / 2

nums_row_col = (3, 2)
xlim = (bins[0], bins[-1])
ylim = (0, 0.6)
width = (bins[0] + bins[-1]) / (len(bins)-1)

ts = [5, 10, 20, 50, 100, 200]
fig, axes = plt.subplots(*nums_row_col, figsize=(12,10))
for i, ax_idx in enumerate(itertools.product(*(range(n) for n in nums_row_col))):
    mean = res.M_tilde[:, ts[i]].mean()
    hist, _ = np.histogram(res.M_tilde[:, ts[i]], bins=bins)
    axes[ax_idx].bar(bins_mid, hist/num_reps, width, align='center')
    axes[ax_idx].vlines(mean, ax.get_ylim()[0], ax.get_ylim()[1], "k", ":")
    axes[ax_idx].set_xlim(*xlim)
    axes[ax_idx].set_ylim(*ylim)
    axes[ax_idx].set_title(r'$t = {}$'.format(ts[i]))
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The simulation results are contained in res.
Let's check that M and M_tilde satisfy the identity from their definition (up to numerical errors).
Can you write Python code for it?

exp_eta_geo_series = np.empty_like(res.M)
exp_eta_geo_series[0] = 1
exp_eta_geo_series[1:] = mf.exp_eta
np.cumprod(exp_eta_geo_series, out=exp_eta_geo_series)

M_2 = res.M[0] * res.M_tilde * mf.e[res.X[0]] * exp_eta_geo_series / mf.e[res.X]
M_2

M_2 - res.M


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The term structure of interest rates R is a sequence (of length J)
of vectors (of length n each).
Instead of plotting the whole R,
we plot the sequences for the "low", "middle", and "high" states.
Here we define those states as follows.
The vector $(p_{jt}|X_t = x)_{x \in S}$, if appropriately rescaled,
converges as $j \to \infty$
to an eigenvector of $\widehat P$ that corresponds to the dominant eigenvalue,
which equals mf.e times some constant.
Thus call the states that correspond to the smallest, largest, and middle values of mf.e
the high, low, and middle states.
Can you write Python code for it?

mf.e

hi = np.argsort(mf.e)[0]
lo = np.argsort(mf.e)[-1]
mid = np.argsort(mf.e)[mf.n//2]

states = [hi, mid, lo]
labels = [s + ' state' for s in ['high', 'middle', 'low']]

fig, ax = plt.subplots(figsize=(8,5))
for i, label in zip(states, labels):
    ax.plot(np.arange(1, J+1), R[:, i], label=label)
ax.set_xlim((1, J))
ax.legend()
plt.show()


I am working on the file "MC_mult_functional.ipynb".
The first task is:
The value $\eta$ is
Can you write Python code for it?


I am working on the file "MC_mult_functional.ipynb".
The first task is:
We revisit the peculiar sample path property at the end of this notebook.
Stochastic discount factor and exponentially changing asset payouts
Define   a matrix ${\sf S}$ whose $(x, y)$th element is ${\sf S}(x,y) = \exp(G_S(x,y))$, where $G_S(x,y)$ is a stochastic discount rate
for moving from state $x$ at time $t$ to state $y$ at time $t+1$.
A stochastic discount factor process ${S_t}_{t=0}^\infty$ is governed by the multiplicative functional:
$$
{\frac {S_{t+1}}{S_t}} = \exp[ G_S(X_t, X_{t+1} ) ] = {\sf S}(X_t, X_{t+1}).
$$
Define   a matrix ${\sf D}$ whose $(x,y)$th element is ${\sf D}(x,y) = \exp(G_d(x,y))$.
A non-negative payout or dividend  process ${d_t}_{t=0}^\infty$ is governed by the multiplicative functional:
$$
{\frac {d_{t+1}}{d_t}} = \exp\left[ G_d(X_t,X_{t+1}) \right] = {\sf D}(X_t, X_{t+1}).
$$
Let $p_t$ be the price at the beginning of period $t$ of a claim to the stochastically growing or shrinking stream of payouts
${d_{t+j}}_{j=0}^\infty$.
It satisfies
$$
p_t = E\left[\frac{S_{t+1}}{S_t} (d_t + p_{t+1}) \Bigm| {\mathfrak F}_t\right] ,
$$
or
$$
\frac{p_t}{d_t} =
E\left[\frac{S_{t+1}}{S_t}
       \left(1 + \frac{d_{t+1}}{d_t} \frac{p_{t+1}}{d_{t+1}}\right)
       \Bigm| {\mathfrak F}_t\right] ,
$$
where the time $t$ information set ${\mathfrak F}_t$ includes $X_t, S_t, d_t$.
Guessing that the price-dividend ratio $\frac{p_t}{d_t}$ is a function of the Markov state $X_t$ only, and letting
it equal $v(x)$ when $X_t = x$, write the preceding equation as
$$ v(x) =   \sum_{x \in S} P(x,y) \left[  {\sf S}(x,y) \mathbf{1}  +  {\sf S}(x,y) {\sf D}(x,y)  v(y)               \right] $$
or
$$
v = c + \widetilde{P} v ,
$$
where $c = \widehat{P} \mathbf{1}$ is by construction a nonnegative vector and we have defined
the nonnegative matrices $\widetilde{P} \in \mathbb{R}^{n \times n}$ and
$\widehat{P} \in \mathbb{R}^{n \times n}$ by
$$
\begin{aligned}
\widetilde{P}(x,y) &= P(x,y) {\sf S}(x,y) {\sf D}(x,y), \
\widehat{P}(x,y) &= P(x,y) {\sf S}(x,y).
\end{aligned}
$$
The equation $v = \widetilde{P} v + c$ ...
Can you write Python code for it?

# Transition probability matrix
P = [[0.4, 0.6],
     [0.2, 0.8]]

# Instance of MarkovChain from quantecon.markov
mc = MarkovChain(P)

# Stochastic discount rate matrix
G_S = [[-0.02, -0.03],
       [-0.01, -0.04]]

# Dividend growth rate matrix
G_d = [[0.01, 0.02],
       [0.005, 0.02]]

# AssetPricingMultFiniteMarkov instance
ap = AssetPricingMultFiniteMarkov(mc, G_S, G_d)


I am working on the file "ME237-Project-4.ipynb".
The first task is:

Derivation in Fourier Space

Going back to the Fourier series,
$$
u(x) = \sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}\frac{2\pi nx}{L_x}\right)=\sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}k_nx\right)
$$
with
$$
k_n=\frac{2\pi n}{L_x}\,
$$
it is obvious that any $m$ derivative of the real variable $u$ be:
$$
\frac{d^mu}{dx^m} = \sum_{n=-\infty}^{\infty}\left(\hat{\jmath}k_n\right)^ma_n\exp\left(\hat{\jmath}k_nx\right)
$$
In other words, if $u_n$ is defined as:
$$
u_n(x) = a_n\exp\left(\hat{\jmath}k_nx\right)
$$
then
$$
\frac{d^mu_n}{dx^m} = \left(\hat{\jmath}k_n\right)^m u_n\,.
$$
A $m$ derivativation in the Fourier space amounts to the multiplication of each Fourier coefficient $a_n$ by $\left(\hat{\jmath}k_n\right)^m$.
The following is a code for the first derivative of $u$:
Can you write Python code for it?

u_hat = np.fft.fft(u)
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
ik = 1j*k
v_hat = ik*u_hat

v = np.real(np.fft.ifft(v_hat))
plt.plot(x,v,'r-',lw=2,label='$FT^{-1}[\hat{\jmath}kFT[u]]$')
plt.plot(x,du,'b--',lw=2,label='exact derivative')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$du/dx$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(v-du))

mk2 = ik*ik
v_hat = mk2*u_hat

v = np.real(np.fft.ifft(v_hat))
plt.plot(x,v,'r-',lw=2,label='$FT^{-1}[-k^2FT[u]]$')
plt.plot(x,ddu,'b--',lw=2,label='exact derivative')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$d^2u/dx^2$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(v-ddu))


I am working on the file "ME237-Project-4.ipynb".
The first task is:

 High-Pass Filter

From the example below, develop a function for a high-pass filter.

Can you write Python code for it?

u_hat = np.fft.fft(u)
kfilter = Nwave/2
k = np.linspace(0,Nx-1,Nx)
filter_mask = np.where((k < kfilter) | (k > Nx-kfilter) )
u_hat[filter_mask] = 0.+0.j
v = np.real(np.fft.ifft(u_hat))
plt.plot(x,v,'r-',lw=2,label='Filtered (FT)')
plt.plot(x,np.sum(uwave[:,kfilter:Nwave+1],axis=1),'b--',lw=2,label='Filtered (exact)')
plt.plot(x,u,'g:',lw=2,label='original')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()

F = np.zeros(Nx/2+1,dtype='float64')
F_filter = np.zeros(Nx/2+1,dtype='float64')
u_hat = np.fft.fft(u)
F = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
v_hat = np.fft.fft(v)
F_filter = np.real(v_hat[0:Nx/2+1]*np.conj(v_hat[0:Nx/2+1]))
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
plt.loglog(k[0:Nx/2+1],F,'r-',lw=2,label=r'$\Phi_u$')
plt.loglog(k[0:Nx/2+1],F_filter,'b-',lw=2,label=r'$\Phi_v$')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$\Phi(k)$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(y_ticks,fontsize = 16)
plt.show()


I am working on the file "ME237-Project-4.ipynb".
The first task is:

Spectrum

For now we will define the spectrum $\Phi_u$ as

$$
\Phi_u(k_n) = \hat{u}_n.\hat{u}_n^*
$$

which can be interpreted as the energy contained in the $k_n$ wavenumber. This is helpful when searching for the most energetic scales or waves in our system. Thanks to the symmetries of the FFT, the spectrum is defined over $n=0$ to $N_x/2$

Low-Pass Filter

The following code filters the original signal by half the wavenumbers using FFT and compares to exact filtered function
Can you write Python code for it?

k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0))) #This is how the FFT stores the wavenumbers
uv = u[0,:]*u[1,:]
u_hat = np.fft.fft(u[0,:])
v_hat = np.fft.fft(u[1,:])
uv_hat_wrong = u_hat*v_hat
uv_hat = np.fft.fft(uv)
F = np.zeros((4,Nx/2+1),dtype='float64')
F[0,:] = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
F[1,:] = np.real(v_hat[0:Nx/2+1]*np.conj(v_hat[0:Nx/2+1]))
F[2,:] = np.real(uv_hat[0:Nx/2+1]*np.conj(uv_hat[0:Nx/2+1]))
F[3,:] = np.real(uv_hat_wrong[0:Nx/2+1]*np.conj(uv_hat_wrong[0:Nx/2+1]))

plt.loglog(k[0:Nx/2+1],F[0,:],'b-',lw=2,label='$\Phi_u$')
plt.loglog(k[0:Nx/2+1],F[1,:],'g-',lw=2,label='$\Phi_v$')
plt.loglog(k[0:Nx/2+1],F[2,:],'r-',lw=2,label='$\Phi_{uv}$')
#plt.loglog(k[0:Nx/2+1],F[3,:],'k-',lw=2,label='$\Phi_{uv}$ (wrong)')
#plt.xlim(24,70)
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$\Phi(k)$', fontdict = font)
plt.xticks(fontsize = 16)
y_ticks = np.logspace(1,6,6)
plt.ylim(1e-3,1e7)
plt.yticks(y_ticks,fontsize = 16) #Specify ticks, necessary when increasing font
plt.show()

Int_u = np.trapz(F[0,0:Nx/2+1],k[0:Nx/2+1])
print(Int_u)
print(np.var(u[0,:]))
print(Int_u/np.var(u[0,:]))

# filtering the smaller waves
def low_pass_filter_fourier(a,k,kcutoff):
    N = a.shape[0]
    a_hat = np.fft.fft(a)
    filter_mask = np.where(np.abs(k) > kcut)
    a_hat[filter_mask] = 0.0 + 0.0j
    a_filter = np.real(np.fft.ifft(a_hat))
    return a_filter
kcut=Nwave/2+1
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
v = low_pass_filter_fourier(u,k,kcut)
u_filter_exact = np.sum(uwave[:,0:kcut+1],axis=1)
plt.plot(x,v,'r-',lw=2,label='filtered with fft')
plt.plot(x,u_filter_exact,'b--',lw=2,label='filtered (exact)')
plt.plot(x,u,'g:',lw=2,label='original')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize =...


I am working on the file "ME237-Project-4.ipynb".
The first task is:

- Explain why $\omega\Delta x$ can be defined from $0$ to $\pi$.

- Write a code to illustrate the effects of the imaginary part and real part on the derivative on the following function
$$
f(x) = cos(nx)
$$
defined for $x\in[0,2\pi]$, discretized with $N$ points. Study a few $n$, ranging from large scales to small scales. Note the two effects we seek to identify are phase change and amplitude change.

- Derive the modified wavenumber for the second order central finite difference scheme
$$
\frac{\delta f}{\delta x}=\frac{f_{i+1}-f_{i-1}}{2\Delta x}
$$

- Create a second order upwind scheme and derive the modified wavenumber. Compare the performance of the first order and the second order schemes. For the second order upwind scheme, find $a$, $b$ and $c$ such that
$$
\frac{\delta f}{\delta x}=\frac{af_{i-2}+bf_{i-1}+cf_i}{\Delta x}
$$

Can you write Python code for it?

!ipython nbconvert --to html ME249-Lecture-3-YOURNAME.ipynb


I am working on the file "ME237-Project-4.ipynb".
The first task is:

Solve the questions in green blocks. Save the file as ME249-Lecture-3-YOURNAME.ipynb and change YOURNAME in the bottom cell. Send me and the grader the html file not the ipynb file.

Discrete Fourier Series

Consider a function $f$ periodic over a domain $0\leq x\leq 2\pi$, discretized by $N_x$ points. The longest wavelength wave that can be contained in the domain is $L_x$. A phyiscal understanding of Fourier series is the representation of a system as the sum of many waves fo wavelengths smaller or equal to $L_x$. In a discrete sense, the series of wave used to decompose the system is defined as:
$$
a_n\exp\left(\hat{\jmath}\frac{2\pi n}{Lx}\right)
$$
such that

$$
f(x) = \sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}\frac{2\pi nx}{Lx}\right)
$$

and

$$
a_n = \frac{1}{L_x}\int_Lf(x)\exp\left(-\hat{\jmath}\frac{2\pi nx}{Lx}\right)dx
$$

Here $\hat{\jmath}^2=-1$.Often the reduction to wavenumber is used, where

$$
k_n = \frac{2\pi n}{L_x}
$$

Note that if $x$ is time instead of distance, $L_x$ is a time $T$ and the smallest frequency contained in the domain is $f_0=1/T_0$ and the wavenumber $n$ is $k_n=2\pi f_0n=2\pi f_n$ with $f_n$ for $\vert n\vert >1$ are the higher frequencies.

Discrete Fourier Transform (DFT)

In scientific computing we are interested in applying Fourier series on vectors or matrices, containing a integer number of samples. The DFT is the fourier series for the number of samples. DFT functions available in python or any other language only care about the number of samples, therefore the wavenumber is

$$
k_n=\frac{2\pi n}{N_x}
$$

Consider a function $f$ periodic over a domain $0\leq x\leq 2\pi$, discretized by $N_x$ points. The nodal value is $f_i$ located at $x_i=(i+1)\Delta x$ with $\Delta x=L_x/Nx$. The DFT is defined as
<p class='alert...
Can you write Python code for it?

import matplotlib.pyplot as plt
import numpy as np

Lx = 2.*np.pi
Nx = 256
u = np.zeros((2,Nx),dtype='float64')
du = np.zeros((2,Nx),dtype='float64')
ddu = np.zeros((2,Nx),dtype='float64')
k_0 = 2.*np.pi/Lx
x = np.linspace(Lx/Nx,Lx,Nx)
Nwave = 60
uwave = np.zeros((2,Nx,Nwave),dtype='float64')
duwave = np.zeros((2,Nx,Nwave),dtype='float64')
dduwave = np.zeros((2,Nx,Nwave),dtype='float64')
ampwave = np.random.random((2,Nwave))
ampdamp = np.zeros(Nx)
kwave = np.zeros(Nx)
for i in range (Nx):
    kwave[i] = k_0*i
    if i == 0.:
        ampdamp[i] = 1.
    else:
        ampdamp[i] = kwave[i]**(-5./6.)
ampdamp[:] = 1.
phasewave = np.random.random((2,Nwave))*2*np.pi
for i in range(2):
    for iwave in range(Nwave):
        uwave[i,:,iwave] = ampdamp[iwave]*ampwave[i,iwave]*np.cos(k_0*iwave*x+phasewave[i,iwave])
        duwave[i,:,iwave] = -k_0*iwave*ampdamp[iwave]*ampwave[i,iwave]*np.sin(k_0*iwave*x+phasewave[i,iwave])
        dduwave[i,:,iwave] = -(k_0*iwave)**2*ampdamp[iwave]*ampwave[i,iwave]*np.cos(k_0*iwave*x+phasewave[i,iwave])
u = np.sum(uwave,axis=2)
du = np.sum(duwave,axis=2)
ddu = np.sum(dduwave,axis=2)
#print(u)
plt.plot(x,u[0,:],lw=2)
plt.plot(x,u[1,:],lw=2)
plt.xlim(0,Lx)
#plt.legend(loc=3, bbox_to_anchor=[0, 1],
#           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()


I am working on the file "ME237-Project-4.ipynb".
The first task is:

Which scales are the most affected by the finite difference scheme? What effect do you observe?

Modified Wavenumber

Starting from,
$$
f(x) = \sum_{k=-N/2+1}^{N/2}\hat{f}k\exp\left(\hat{\jmath}\frac{2\pi kx}{L_x}\right)
$$
and introducing the wavenumber
$$
\omega = \frac{2\pi k}{L_x}
$$
allows to reduce the Fourier expansion to
$$
f(x) = \sum{\omega=-\pi}^{\pi}\hat{f}\omega e^{\hat{\jmath}\omega x}
$$
The derivative of $f$ becomes
$$
f'(x) = \sum{\omega=-\pi}^{\pi}\hat{f'}\omega e^{\hat{\jmath}\omega x}
$$
where $\hat{f'}\omega$ is only a notation, not the derivative of the Fourier coefficient:
$$
\hat{f'}\omega=\hat{\jmath}\omega\hat{f}\omega
$$
Considering the symmetry of the FT, we restrict the study to $\omega\Delta x\in[0,\pi]$.
Now we want to express the first order finite difference scheme in terms of Fourier coefficients and wavenumbers. The scaled coordinates expression of the scheme is
$$
\frac{\delta f}{\delta x}=\frac{f(x)-f(x-\Delta x)}{\Delta x}=\sum_{\omega=-\pi}^{\pi}\hat{f}\omega\frac{e^{\hat{\jmath}\omega x}-e^{\hat{\jmath}\omega (x-\Delta x)}}{\Delta x}=\sum{\omega=-\pi}^{\pi}\frac{1-e^{-\hat{\jmath}\omega\Delta x}}{\Delta x}\hat{f}\omega e^{\hat{\jmath}\omega x}
$$
We now define a modified wavenumber for the first order upwind scheme:
$$
\hat{\jmath}\omega'=\frac{1-e^{-\hat{\jmath}\omega\Delta x}}{\Delta x}=\frac{1-\cos(-\omega\Delta x)-\hat{\jmath}\sin(-\omega\Delta x)}{\Delta x}
$$
which reduces to
$$
\hat{\jmath}\omega\text{mod}\Delta x= \hat{\jmath}\sin(\omega\Delta x)+\left(\cos(1-\omega\Delta x)\right)
$$
The modified wavenumber is no longer purely imaginary, and even the imaginary part is far from the exact wavenumber, as shown in the following plot.
Can you write Python code for it?

L = np.pi
N = 32
dx = L/N
omega = np.linspace(0,L,N)
omegap_exact = omega
omegap_modified = np.sin(omega)
plt.plot(omega,omegap_exact,'k-',lw=2,label='exact')
plt.plot(omega,omegap_modified,'r-',lw=2,label='1st order upwind')
plt.xlim(0,L)
plt.ylim(0,L)
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$\omega\Delta x$', fontdict = font)
plt.ylabel('$\omega_{mod}\Delta x$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()


I am working on the file "ME237-Project-4.ipynb".
The first task is:
First, let's verify that the derivation of the modified wavenumber is correct.
Can you write Python code for it?

u_hat = np.fft.fft(u)
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
dx = Lx/Nx
ikm = 1j*np.sin(2.*np.pi/Nx*k)/dx+(1-np.cos(2.*np.pi/Nx*k))/dx
v_hat = ikm*u_hat
dum = np.real(np.fft.ifft(v_hat))
plt.plot(x,dum,'r-',lw=2,label='$FT^{-1}[\hat{\jmath}\omega_{mod}FT[u]]$')
plt.plot(x,du_fd,'b--',lw=2,label='$\delta u/\delta x$')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$du/dx$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(du_fd-dum,np.inf))


I am working on the file "ME249-CFD-tutorial-1.ipynb".
The first task is:

Laminar Pipe Flow

In this first tutorial, you will simulate a laminar pipe flow using the cad file CAD>PipeD0.025L2.5VOF.iges which is the Volume of Fluid of the CAD below. The pipe inner diameter is 2.5cm and 250cm long.

The Junior course of Fluid Mechanics has taught you that:

 The entrance length for laminar flow is given by
$$
\frac{L_e}{D}=0.05Re=0.05\frac{U_{b}D}{\nu}
$$

 The pressure gradient is constant in the fully developed region ($x > L_e$)

 The velocity profile in the fully developed region is governed by the reduced streamwise momentum equation
$$
0=-\frac{dP}{dx}+\frac{\mu}{r}\frac{d}{dr}r\frac{dU}{dr}
$$

The solution of this equation is
$$
U(r) = \frac{R^2}{4\mu}\left(-\frac{dP}{dx}\right)\left(1-\frac{r^2}{R^2}\right)=2U_b\left(1-\frac{r^2}{R^2}\right)
$$

Hence,
$$
U_b=\frac{R^2}{8\mu}\left(-\frac{dP}{dx}\right)
$$

 Finally, the wall shear stress is proportional to the pressure drop:
$$
-\frac{dP}{dx}=\frac{2\tau_w}{R}
$$

Using https://www.simscale.com, Simulate a laminar flow. Verify and Validate (V & V) your simulation.

Can you write Python code for it?

D = 2.5e-2 # m
nu = 15.e-6 #m^2/s
rho = 1.2 #kg/m^3
mu = nu/rho

R = D/2

Re = 500.

Ub = Re * nu / D
print("Bulk velocity= %2.2f m/s" %Ub)
flowrate = Ub * np.pi * R**2
print("Flow rate = %1.4e m^3/s" %flowrate)

import numpy as np

n = 30
r = np.linspace(0,R,n)

U = 2 * Ub * (1 - np.power(r,2)/R**2)
import matplotlib.pyplot as plt

plt.plot(r,U,linewidth = 2)
plt.xlabel(r"$r$ (m)", fontdict = fontlabel)
plt.ylabel(r"$U(r)$ (m/s)", fontdict = fontlabel)
plt.xlim(0,R)
plt.show()


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:

 Q2: Search numpy function linspace and describe what x_phi and x_u define. Why are the dimensions different?

Step 3: Initialization

Now we define a function to initialize our variables. In python, indentation matters! A function is defined by the command def followed by the name of the function and the argument given to the function. The variables passed as argument in the function are local, meaning they may or may not have the same names as the variables in the core code. Any other variable used within the function needs to be defined in the function or before.
Note that python accepts implicit loops. Here phi and x_phi are two vectors of dimension $N$.
Can you write Python code for it?

def init_simulation(x_phi,N):
    phi = np.zeros(N)
    phi = 1.+np.cos(x_phi-L/2.)
    xmask = np.where(np.abs(x_phi-L/2.) > np.pi)
    phi[xmask] = 0.
    return phi
phi = init_simulation(x_phi,N)
plt.plot(x_phi,phi,lw=2)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$\phi$', fontdict = font)
plt.xlim(0,L)
plt.show()


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:

Q3: What do you observe?

Q4: Write a code to verify the divergence subroutine.

Can you write Python code for it?

def flux_divergence(f,N,dx):
    df = np.zeros(N)
    df[0:N] = (f[1:N+1]-f[0:N])/dx
    return df


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:

Step 5: Writing the simulation code

The first code solves:

$$
\phi_i^{n+1}=\phi_i^n-\frac{\Delta t}{\Delta x}\left(F^n_{i+\frac{1}{2}} - F^n_{i-\frac{1}{2}}\right)
$$

for whatever scheme you choose. Play with the different schemes. Consider that the analytical solution is:
$$
\phi(x,t)=\begin{cases}
1+\cos\left[x-\left(\frac{L}{2}+u_0t\right)\right]&,\text{ for }\left\vert x-\left(\frac{L}{2}+u_0t\right)\right\vert\leq\pi\
0&,\text{ for }\left\vert x-\left(\frac{L}{2}+u_0t\right)\right\vert>\pi
\end{cases}
$$
Can you write Python code for it?

N=200
Simulation_time = 5.
dx = L/N
x_phi = np.linspace(dx/2.,L-dx/2.,N)
x_u = np.linspace(0.,L,N+1)
u_0 = 1.
num_scheme = 'US2'
u = u_0*np.ones(N+1)
phi = np.zeros(N)
flux = np.zeros(N+1)
divflux = np.zeros(N)

phi = init_simulation(x_phi,N)
phi_init = phi.copy()
number_of_iterations = 100
dt = Simulation_time/number_of_iterations

t = 0.
for it in range (number_of_iterations):
    flux = compute_flux_advanced(phi,u,N,num_scheme)
    divflux = flux_divergence(flux,N,dx)
    phi -= dt*divflux
    t += dt
plt.plot(x_phi,phi,lw=2,label='simulated')
plt.plot(x_phi,phi_init,lw=2,label='initial')
plt.legend(loc=2, bbox_to_anchor=[0, 1],
           ncol=2, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$\phi$', fontdict = font)
plt.xlim(0,L)
plt.show()


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
A slower but easier to understand version of this function is shown below. The tag slow is explained shortly after.
Can you write Python code for it?

def init_simulation_slow(u,phi,x_phi,N):
    for i in range(N):
        if (np.abs(x_phi[i]-L/2.) > np.pi):
            phi[i] = 0.
        else:
            phi[i] = 1.+np.cos(x_phi[i]-L/2.)
    return phi
phi = init_simulation_slow(u,phi,x_phi,N)
plt.plot(x_phi,phi,lw=2)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$\phi$', fontdict = font)
plt.xlim(0,L)
plt.show()


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
Although the plot suggests that the interpolation works, a visual proof can be deceptive. It is best to calculate the error between the exact and interpolated solution. Here we use an $l^2$-norm:
$$
\Vert F\Vert_2=\sqrt{\sum_{i=0}^{N}\left(F_i-F_i^e\right)^2}
$$
where $F_e$ is the exact solution for the flux.
Can you write Python code for it?

N = 200
phi = np.zeros(N)
F_exact = np.zeros(N+1)
F = np.zeros(N+1)
u = u_0*np.ones(N+1)
x_phi = np.linspace(dx/2.,L-dx/2.,N)
x_u = np.linspace(0.,L,N+1)
phi = init_simulation(x_phi,N)
F_exact = init_simulation(x_u,N+1)
F = compute_flux(phi,u,N)
error = np.sqrt(np.sum(np.power(F-F_exact,2)))
errorx = np.power(F-F_exact,2)
plt.plot(x_u,errorx)
plt.show()
print('error norm L 2= %1.4e' %error)

Nerror = 3
Narray = np.array([10, 100, 200])
delta = L/Narray
error = np.zeros(Nerror)
order = np.zeros(Nerror)
for ierror in range(Nerror):
    N = Narray[ierror]
    phi = np.zeros(N)
    F_exact = np.zeros(N+1)
    F = np.zeros(N+1)
    u = u_0*np.ones(N+1)
    x_phi = np.linspace(dx/2.,L-dx/2.,N)
    x_u = np.linspace(0.,L,N+1)
    phi = init_simulation(x_phi,N)
    F_exact = init_simulation(x_u,N+1)
    F = compute_flux(phi,u,N)
    error[ierror] = np.linalg.norm(F-F_exact)
    #error[ierror] = np.sqrt(np.sum(np.power(F-F_exact,2)))
    print('error norm L 2= %1.4e' %error[ierror])
order = 0.1*delta**(2)
plt.loglog(delta,error,lw=2,label='interpolate')
plt.loglog(delta,order,lw=2,label='$\propto\Delta x^2$')
plt.legend(loc="upper left", bbox_to_anchor=[0, 1],
           ncol=1, shadow=True, fancybox=True)
plt.xlabel('$\Delta x$', fontdict = font)
plt.ylabel('$\Vert F\Vert_2$', fontdict = font)
plt.show


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
Figure 1. Sketch of a cell (top left) with the horizontal (red) and vertical (green) velocity nodes and the cell-centered node (blue). Definition of the normal vector to "surface" (segment) $S_{i+\frac{1}{2},j}$ and $S_{i,j+\frac{1}{2}}$ (top right). Sketch of uniform grid (bottom).

Derivation of 1D Transport Equation

1D Transport Without Diffusion

Consider a small control surface (cell) of dimensions $\Delta x\times\Delta y$ within which, we know the velocities on the surfaces $u_{i\pm\frac{1}{2},j}$ and $v_{i,j\pm\frac{1}{2}}$ and a quantity $\phi_{i,j}$ at the center of the cell. This quantity may be temperature, or the concentration of chemical specie. The variation in time of $\phi$ within the cell is equal to the amount of $\phi$ that is flowing in and out of the cell through the boundaries of cell. The velocity vector is defined as
$$
\vec{u}=u\vec{e}_x+v\vec{e}_y
$$
The fluxes of $\phi$ across the right-hand-side and left-hand-side vertical boundaries are, respectively:
$$
\int_{S_{i+1/2,j}}\phi(\vec{u}{i+\frac{1}{2},j}\cdot\vec{n}{i+\frac{1}{2},j})dy\text{ and }\int_{S_{i-1/2,j}}\phi(\vec{u}{i-\frac{1}{2},j}\cdot\vec{n}{i+\frac{1}{2},j})dy
$$
In the configuration depicted in Figure 1, the mass or heat variation is equal to the flux of $\phi$ entering the cell minus the flux exiting the cell, or:
$$
-\phi_{i+\frac{1}{2},j}u_{i+\frac{1}{2},j}\Delta y + \phi_{i-\frac{1}{2},j}u_{i-\frac{1}{2},j}\Delta y \text{, when $\Delta y\rightarrow 0$}
$$
Assuming that there is no vertical velocity ($v=0$), this sum is equal to the variation of $\phi$ within the cell,
$$
\frac{\partial}{\partial t}\iint_{V_{i,j}}\phi dxdy\approx\frac{\partial \phi_{i,j}}{\partial t}\Delta x\Delta y \text{, when $\Delta x\rightarrow 0$ and $\Delta y\rightarrow 0$}
$$
yielding
$$
\frac{\partial \phi_{i,j}}{\partial t}\Delta x\Delta y=-\phi_{i+\frac{1}{2},j}u_{i+\frac{1}{2},j}\Delta y + \phi_{i-\frac{1}{2},j}u_{i-\frac{1}{2},j}\Delta y\;,
$$
reducing to
$$
\frac{\partial \phi_{i,j}}{\partial...
Can you write Python code for it?

%matplotlib inline
# plots graphs within the notebook
%config InlineBackend.figure_format='svg' # not sure what this does, may be default images to svg format

import matplotlib.pyplot as plt #calls the plotting library hereafter referred as to plt

import numpy as np


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
For reasons that will become clearer later, we want to consider other interpolation schemes:
$$
\phi_{i+\frac{1}{2}}=g_1\phi_{i+1}-g_2\phi_{i-1}+(1-g_1+g_2)\phi_i
$$
The scheme CS is the interpolation scheme we have used so far. Let us test them all, however we have to modify the interpolation function.
Can you write Python code for it?

Nscheme = 4
Scheme = np.array(['CS','US1','US2','US3'])
g_1 = np.array([1./2.,0.,0.,3./8.])
g_2 = np.array([0.,0.,1./2.,1./8.])

def compute_flux_advanced(a,v,N,num_scheme):
    imask = np.where(Scheme == num_scheme)
    g1 = g_1[imask]
    g2 = g_2[imask]
    f=np.zeros(N+1)
    f[2:N] = ((1.-g1+g2)*a[1:N-1]+g1*a[2:N]-g2*a[0:N-2])*v[2:N]
    if (num_scheme == 'US2') or (num_scheme == 'US3'):
        f[1] = ((1.-g1)*a[0]+g1*a[1])*v[1]
    f[0] = f[1]
    f[N] = f[N-1]
    return f

table = ListTable()
table.append(['Scheme', '$g_1$', '$g_2$'])
for i in range(4):
    table.append([Scheme[i],g_1[i], g_2[i]])
table

Nerror = 3
Narray = np.array([10, 100, 200])
delta = L/Narray
error = np.zeros((Nerror,Nscheme))
order = np.zeros((Nerror,Nscheme))
for ischeme in range(Nscheme):
    num_scheme = Scheme[ischeme]
    for ierror in range(Nerror):
        N = Narray[ierror]
        dx = L/N
        phi = np.zeros(N)
        F_exact = np.zeros(N+1)
        F = np.zeros(N+1)
        u = u_0*np.ones(N+1)
        x_phi = np.linspace(dx/2.,L-dx/2.,N)
        x_u = np.linspace(0.,L,N+1)
        phi = init_simulation(x_phi,N)
        F_exact = init_simulation(x_u,N+1)
        F = compute_flux_advanced(phi,u,N,num_scheme)
        error[ierror,ischeme] = np.linalg.norm(F-F_exact)
        #print('error norm L 2= %1.4e' %error[ierror,ischeme])

for ischeme in range(Nscheme):
    plt.loglog(delta,error[:,ischeme],lw=2,label=Scheme[ischeme])
order = 1.0*(delta/delta[0])
plt.loglog(delta,order,'k:',lw=2,label='$\propto\Delta x$')
order = 1.0*(delta/delta[0])**(2)
plt.loglog(delta,order,'k-',lw=2,label='$\propto\Delta x^2$')
order = 1.0*(delta/delta[0])**(3)
plt.loglog(delta,order,'k--',lw=2,label='$\propto\Delta x^3$')
plt.legend(loc=2, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$\Delta x$', fontdict = font)
plt.ylabel('$\Vert F\Vert_2$', fontdict = font)
plt.xlim(L/300,L/9.)
plt.ylim(1e-5,1e2)
plt.show


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
The choice for the interpolation is obvious:
Can you write Python code for it?