exam2 + remaining files of the semester
This commit is contained in:
1688
Problemsets/Solutions/PS01_Basics_Solution.ipynb
Normal file
1688
Problemsets/Solutions/PS01_Basics_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1742
Problemsets/Solutions/PS02_Stats_Solution.ipynb
Normal file
1742
Problemsets/Solutions/PS02_Stats_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
5438
Problemsets/Solutions/PS03_CLT_Solution.ipynb
Normal file
5438
Problemsets/Solutions/PS03_CLT_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
522
Problemsets/Solutions/PS04_YieldToMaturity_Solution.ipynb
Normal file
522
Problemsets/Solutions/PS04_YieldToMaturity_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1839
Problemsets/Solutions/PS05_TradingStrategy_Solution.ipynb
Normal file
1839
Problemsets/Solutions/PS05_TradingStrategy_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1193
Problemsets/Solutions/PS07_Optimisation_Solution.ipynb
Normal file
1193
Problemsets/Solutions/PS07_Optimisation_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
6294
Problemsets/Solutions/PS08_Volatility_Solution.ipynb
Normal file
6294
Problemsets/Solutions/PS08_Volatility_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
369
Problemsets/Solutions/PS09a_PyCall_Solution.ipynb
Normal file
369
Problemsets/Solutions/PS09a_PyCall_Solution.ipynb
Normal file
@@ -0,0 +1,369 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Python\n",
|
||||
"\n",
|
||||
"using the [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) package."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"CovNWFn"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||
"\n",
|
||||
"include(\"jlFiles/printmat.jl\")\n",
|
||||
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sample size: (388,)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||
"\n",
|
||||
" #yearmonth, market, small minus big, high minus low\n",
|
||||
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||
"x = nothing\n",
|
||||
"\n",
|
||||
"printlnPs(\"Sample size:\",size(Rme))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Do OLS (in Julia)\n",
|
||||
"\n",
|
||||
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||
"\n",
|
||||
" b std_iid\n",
|
||||
"c 0.007 0.002\n",
|
||||
"SMB 0.217 0.073\n",
|
||||
"HML -0.429 0.074\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Y = Rme\n",
|
||||
"T = size(Y,1)\n",
|
||||
"X = [ones(T) RSMB RHML]\n",
|
||||
"\n",
|
||||
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||
"std_iid = sqrt.(diag(V))\n",
|
||||
"\n",
|
||||
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Getting Started with PyCall"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using PyCall\n",
|
||||
"sm = pyimport(\"statsmodels.api\"); #activate this package"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||
"\"\"\"\n",
|
||||
" OLS Regression Results \n",
|
||||
"==============================================================================\n",
|
||||
"Dep. Variable: y R-squared: 0.134\n",
|
||||
"Model: OLS Adj. R-squared: 0.130\n",
|
||||
"Method: Least Squares F-statistic: 29.85\n",
|
||||
"Date: Thu, 09 Dec 2021 Prob (F-statistic): 8.88e-13\n",
|
||||
"Time: 10:00:28 Log-Likelihood: 672.28\n",
|
||||
"No. Observations: 388 AIC: -1339.\n",
|
||||
"Df Residuals: 385 BIC: -1327.\n",
|
||||
"Df Model: 2 \n",
|
||||
"Covariance Type: nonrobust \n",
|
||||
"==============================================================================\n",
|
||||
" coef std err t P>|t| [0.025 0.975]\n",
|
||||
"------------------------------------------------------------------------------\n",
|
||||
"const 0.0070 0.002 3.167 0.002 0.003 0.011\n",
|
||||
"x1 0.2170 0.074 2.949 0.003 0.072 0.362\n",
|
||||
"x2 -0.4291 0.074 -5.821 0.000 -0.574 -0.284\n",
|
||||
"==============================================================================\n",
|
||||
"Omnibus: 58.863 Durbin-Watson: 1.849\n",
|
||||
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 146.539\n",
|
||||
"Skew: -0.749 Prob(JB): 1.51e-32\n",
|
||||
"Kurtosis: 5.612 Cond. No. 38.8\n",
|
||||
"==============================================================================\n",
|
||||
"\n",
|
||||
"Notes:\n",
|
||||
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
|
||||
"\"\"\"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"┌ Warning: `vendor()` is deprecated, use `BLAS.get_config()` and inspect the output instead\n",
|
||||
"│ caller = npyinitialize() at numpy.jl:67\n",
|
||||
"└ @ PyCall C:\\Users\\psoderlind\\.julia\\packages\\PyCall\\3fwVL\\src\\numpy.jl:67\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"resultsP = sm.OLS(Y, X).fit() #can use Python functions directly\n",
|
||||
"\n",
|
||||
"println(resultsP.summary())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[:HC0_se, :HC1_se, :HC2_se, :HC3_se, :_HCCM, :__class__, :__delattr__, :__dict__, :__dir__, :__doc__, :__eq__, :__format__, :__ge__, :__getattribute__, :__gt__, :__hash__, :__init__, :__init_subclass__, :__le__, :__lt__, :__module__, :__ne__, :__new__, :__reduce__, :__reduce_ex__, :__repr__, :__setattr__, :__sizeof__, :__str__, :__subclasshook__, :__weakref__, :_abat_diagonal, :_cache, :_data_attr, :_data_in_cache, :_get_robustcov_results, :_is_nested, :_use_t, :_wexog_singular_values, :aic, :bic, :bse, :centered_tss, :compare_f_test, :compare_lm_test, :compare_lr_test, :condition_number, :conf_int, :conf_int_el, :cov_HC0, :cov_HC1, :cov_HC2, :cov_HC3, :cov_kwds, :cov_params, :cov_type, :df_model, :df_resid, :diagn, :eigenvals, :el_test, :ess, :f_pvalue, :f_test, :fittedvalues, :fvalue, :get_influence, :get_prediction, :get_robustcov_results, :info_criteria, :initialize, :k_constant, :llf, :load, :model, :mse_model, :mse_resid, :mse_total, :nobs, :normalized_cov_params, :outlier_test, :params, :predict, :pvalues, :remove_data, :resid, :resid_pearson, :rsquared, :rsquared_adj, :save, :scale, :ssr, :summary, :summary2, :t_test, :t_test_pairwise, :tvalues, :uncentered_tss, :use_t, :wald_test, :wald_test_terms, :wresid]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(keys(resultsP)) #print all keys (field names)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 1\n",
|
||||
"\n",
|
||||
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Comparing the estimates in Julia and Python\n",
|
||||
" 0.007 0.007\n",
|
||||
" 0.217 0.217\n",
|
||||
" -0.429 -0.429\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"b_P = resultsP.params #the numerical results are now a Julia vector\n",
|
||||
"\n",
|
||||
"println(\"Comparing the estimates in Julia and Python\")\n",
|
||||
"printmat([b b_P])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 2\n",
|
||||
"\n",
|
||||
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(-2.7755575615628914e-17, 4.163336342344337e-17)\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"printmat(extrema(resultsP.resid - u))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||
"\n",
|
||||
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||
"\n",
|
||||
" b std_nw\n",
|
||||
"c 0.007 0.002\n",
|
||||
"SMB 0.217 0.129\n",
|
||||
"HML -0.429 0.118\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||
"std_nw = sqrt.(diag(V))\n",
|
||||
"\n",
|
||||
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 3 \n",
|
||||
"\n",
|
||||
"Now redo the Python estimation with the same sort of robust standard errors. Hint: `resultsP.get_robustcov_results()`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||
"\"\"\"\n",
|
||||
" OLS Regression Results \n",
|
||||
"==============================================================================\n",
|
||||
"Dep. Variable: y R-squared: 0.134\n",
|
||||
"Model: OLS Adj. R-squared: 0.130\n",
|
||||
"Method: Least Squares F-statistic: 11.87\n",
|
||||
"Date: Thu, 09 Dec 2021 Prob (F-statistic): 9.94e-06\n",
|
||||
"Time: 10:00:32 Log-Likelihood: 672.28\n",
|
||||
"No. Observations: 388 AIC: -1339.\n",
|
||||
"Df Residuals: 385 BIC: -1327.\n",
|
||||
"Df Model: 2 \n",
|
||||
"Covariance Type: HAC \n",
|
||||
"==============================================================================\n",
|
||||
" coef std err t P>|t| [0.025 0.975]\n",
|
||||
"------------------------------------------------------------------------------\n",
|
||||
"const 0.0070 0.002 2.850 0.005 0.002 0.012\n",
|
||||
"x1 0.2170 0.129 1.688 0.092 -0.036 0.470\n",
|
||||
"x2 -0.4291 0.118 -3.649 0.000 -0.660 -0.198\n",
|
||||
"==============================================================================\n",
|
||||
"Omnibus: 58.863 Durbin-Watson: 1.849\n",
|
||||
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 146.539\n",
|
||||
"Skew: -0.749 Prob(JB): 1.51e-32\n",
|
||||
"Kurtosis: 5.612 Cond. No. 38.8\n",
|
||||
"==============================================================================\n",
|
||||
"\n",
|
||||
"Notes:\n",
|
||||
"[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 2 lags and without small sample correction\n",
|
||||
"\"\"\"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"resultsP2 = resultsP.get_robustcov_results(cov_type=\"HAC\",maxlags=2)\n",
|
||||
"\n",
|
||||
"println(resultsP2.summary())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"@webio": {
|
||||
"lastCommId": null,
|
||||
"lastKernelId": null
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.7.0",
|
||||
"language": "julia",
|
||||
"name": "julia-1.7"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.7.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
363
Problemsets/Solutions/PS09b_RCall_Solution.ipynb
Normal file
363
Problemsets/Solutions/PS09b_RCall_Solution.ipynb
Normal file
@@ -0,0 +1,363 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# R\n",
|
||||
"\n",
|
||||
"using the [RCall.jl](https://juliainterop.github.io/RCall.jl/stable/) package."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"CovNWFn"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||
"\n",
|
||||
"include(\"jlFiles/printmat.jl\")\n",
|
||||
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Sample size: (388,)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||
"\n",
|
||||
" #yearmonth, market, small minus big, high minus low\n",
|
||||
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||
"x = nothing\n",
|
||||
"\n",
|
||||
"printlnPs(\"Sample size:\",size(Rme))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Do OLS (in Julia)\n",
|
||||
"\n",
|
||||
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||
"\n",
|
||||
" b std_iid\n",
|
||||
"c 0.007 0.002\n",
|
||||
"SMB 0.217 0.073\n",
|
||||
"HML -0.429 0.074\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Y = Rme\n",
|
||||
"T = size(Y,1)\n",
|
||||
"X = [ones(T) RSMB RHML]\n",
|
||||
"\n",
|
||||
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||
"std_iid = sqrt.(diag(V))\n",
|
||||
"\n",
|
||||
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Getting Started with RCall"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using RCall"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"RObject{VecSxp}\n",
|
||||
"\n",
|
||||
"Call:\n",
|
||||
"lm(formula = Y ~ X + 0)\n",
|
||||
"\n",
|
||||
"Residuals:\n",
|
||||
" Min 1Q Median 3Q Max \n",
|
||||
"-0.20224 -0.02477 0.00335 0.02663 0.11840 \n",
|
||||
"\n",
|
||||
"Coefficients:\n",
|
||||
" Estimate Std. Error t value Pr(>|t|) \n",
|
||||
"X1 0.006983 0.002205 3.167 0.00166 ** \n",
|
||||
"X2 0.216968 0.073565 2.949 0.00338 ** \n",
|
||||
"X3 -0.429088 0.073710 -5.821 1.23e-08 ***\n",
|
||||
"---\n",
|
||||
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
|
||||
"\n",
|
||||
"Residual standard error: 0.04295 on 385 degrees of freedom\n",
|
||||
"Multiple R-squared: 0.1488,\tAdjusted R-squared: 0.1422 \n",
|
||||
"F-statistic: 22.44 on 3 and 385 DF, p-value: 2.07e-13\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RObject{VecSxp}\n",
|
||||
"\n",
|
||||
"Call:\n",
|
||||
"lm(formula = Y ~ X + 0)\n",
|
||||
"\n",
|
||||
"Coefficients:\n",
|
||||
" X1 X2 X3 \n",
|
||||
" 0.006983 0.216968 -0.429088 \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"@rput X Y\n",
|
||||
"\n",
|
||||
"ResultsR = reval(\"summary(mod <- lm(Y ~ X+0))\") #print summary of regression\n",
|
||||
"println(ResultsR)\n",
|
||||
"\n",
|
||||
"resultsR = reval(\"mod <- lm(Y ~ X+0)\") #get all output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[:coefficients, :residuals, :effects, :rank, Symbol(\"fitted.values\"), :assign, :qr, Symbol(\"df.residual\"), :xlevels, :call, :terms, :model]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(names(resultsR)) #print all keys (field names)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 1\n",
|
||||
"\n",
|
||||
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Comparing the estimates in Julia and R\n",
|
||||
" 0.007 0.007\n",
|
||||
" 0.217 0.217\n",
|
||||
" -0.429 -0.429\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"b_R = rcopy(resultsR[:coefficients]) #the numerical results are now a Julia vector\n",
|
||||
"\n",
|
||||
"println(\"Comparing the estimates in Julia and R\")\n",
|
||||
"printmat([b b_R])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 2\n",
|
||||
"\n",
|
||||
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(-1.942890293094024e-16, 6.453171330633722e-16)\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"resid_R = rcopy(resultsR[:residuals]) \n",
|
||||
"\n",
|
||||
"printmat(extrema(resid_R - u))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||
"\n",
|
||||
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||
"\n",
|
||||
" b std_nw\n",
|
||||
"c 0.007 0.002\n",
|
||||
"SMB 0.217 0.129\n",
|
||||
"HML -0.429 0.118\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||
"std_nw = sqrt.(diag(V))\n",
|
||||
"\n",
|
||||
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 3 \n",
|
||||
"\n",
|
||||
"Now redo the R estimation with the same sort of robust standard errors. Hint: the `NeweyWest` in the `sandwich`package."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"@rlibrary sandwich"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" bstd_nw (R)\n",
|
||||
"c 0.007 0.002\n",
|
||||
"SMB 0.217 0.129\n",
|
||||
"HML -0.429 0.118\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"reval(\"mod <- lm(Y ~ X+0)\")\n",
|
||||
"\n",
|
||||
"VCV_nwR = reval(NeweyWest(resultsR,lag=2,prewhite=0))\n",
|
||||
"std_nwR = sqrt.(diag(rcopy(VCV_nwR)))\n",
|
||||
"\n",
|
||||
"printmat([b std_nwR],colNames=[\"b\",\"std_nw (R)\"],rowNames=xNames)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"@webio": {
|
||||
"lastCommId": null,
|
||||
"lastKernelId": null
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.7.0",
|
||||
"language": "julia",
|
||||
"name": "julia-1.7"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.7.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
389
Problemsets/Solutions/PS09c_WarAndPeace_Solution.ipynb
Normal file
389
Problemsets/Solutions/PS09c_WarAndPeace_Solution.ipynb
Normal file
@@ -0,0 +1,389 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using Downloads"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Download a Book from Internet\n",
|
||||
"\n",
|
||||
"and read it into a string in Julia. Then report the number of letters etc (see below)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"File to download: http://www.gutenberg.org/files/2600/2600-0.txt\n",
|
||||
"\n",
|
||||
"check the subfolder Results\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"if !isdir(\"Results\")\n",
|
||||
" error(\"create the subfolder Results before running this program\")\n",
|
||||
"end\n",
|
||||
"\n",
|
||||
"http = \"http://www.gutenberg.org/files/2600/2600-0.txt\"\n",
|
||||
"\n",
|
||||
"println(\"File to download: \",http)\n",
|
||||
"Downloads.download(http,\"Results/WarAndPeace.txt\")\n",
|
||||
"\n",
|
||||
"println(\"\\ncheck the subfolder Results\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fh = open(\"Results/WarAndPeace.txt\")\n",
|
||||
"str = read(fh,String) \n",
|
||||
"close(fh)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 1\n",
|
||||
"\n",
|
||||
"1. Count the number of letters and the unique letters in `str`. Hint: `length() and `unique()`\n",
|
||||
"\n",
|
||||
"2. Count the number of word and lines. Hint: `split(str)` and `split(str,\"\\n\")`\n",
|
||||
"\n",
|
||||
"3. Count the number of unique words."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"number of letters and unique letters in W&P: 3293555 113\n",
|
||||
"\n",
|
||||
"unique letters: ['\\ufeff', 'T', 'h', 'e', ' ', 'P', 'r', 'o', 'j', 'c', 't', 'G', 'u', 'n', 'b', 'g', 'B', 'k', 'f', 'W', 'a', 'd', ',', 'y', 'L', 'l', 's', '\\r', '\\n', 'i', 'w', 'U', 'S', 'm', 'p', 'v', '.', 'Y', '-', 'I', ':', 'A', 'M', 'R', 'D', '2', '0', '1', '[', '#', '6', ']', 'J', '9', 'E', 'C', 'F', '8', 'V', '*', 'O', 'H', 'N', 'K', '/', '5', 'X', '7', '3', '“', '’', '—', '‘', '!', '?', '”', 'á', 'é', 'ë', 'í', ';', 'x', '(', ')', 'z', 'q', 'À', 'ó', 'ú', 'è', 'î', 'ô', 'à', 'ç', 'Q', 'â', 'ê', 'ï', 'Z', '4', 'ý', 'ö', 'ä', 'ü', 'Á', 'œ', 'É', '=', 'æ', '\"', '%', '\\'', '$']\n",
|
||||
"\n",
|
||||
"ASCII letters: ['T', 'h', 'e', ' ', 'P', 'r', 'o', 'j', 'c', 't', 'G', 'u', 'n', 'b', 'g', 'B', 'k', 'f', 'W', 'a', 'd', ',', 'y', 'L', 'l', 's', '\\r', '\\n', 'i', 'w', 'U', 'S', 'm', 'p', 'v', '.', 'Y', '-', 'I', ':', 'A', 'M', 'R', 'D', '2', '0', '1', '[', '#', '6', ']', 'J', '9', 'E', 'C', 'F', '8', 'V', '*', 'O', 'H', 'N', 'K', '/', '5', 'X', '7', '3', '!', '?', ';', 'x', '(', ')', 'z', 'q', 'Q', 'Z', '4', '=', '\"', '%', '\\'', '$']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"n = length(str)\n",
|
||||
"letters = unique(str)\n",
|
||||
"println(\"number of letters and unique letters in W&P: \", n,\" \",length(letters))\n",
|
||||
"\n",
|
||||
"println(\"\\nunique letters: \",letters)\n",
|
||||
"\n",
|
||||
"vv = isascii.(letters)\n",
|
||||
"println(\"\\nASCII letters: \",letters[vv])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"number of words in W&P, including pre-amble, etc: 566334\n",
|
||||
"number of lines in W&P, including pre-amble, etc: 66033\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"words = split(str)\n",
|
||||
"println(\"number of words in W&P, including pre-amble, etc: \",length(words))\n",
|
||||
"\n",
|
||||
"lines = split(str,\"\\n\")\n",
|
||||
"println(\"number of lines in W&P, including pre-amble, etc: \",length(lines))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"How many unique words are there in the file?\n",
|
||||
"Number of unique words: 41971\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(\"How many unique words are there in the file?\")\n",
|
||||
"UniqueWords = unique(words)\n",
|
||||
"\n",
|
||||
"println(\"Number of unique words: \",length(UniqueWords))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 2\n",
|
||||
"\n",
|
||||
"1. How often is Borodinó mentioned. Hint: `occursin.(,words)`\n",
|
||||
"\n",
|
||||
"2. Print all lines that contain the word Borodinó. Hint: `occursin(,line)`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"How often is Borodinó mentioned?\n",
|
||||
"108\n",
|
||||
"108\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(\"How often is Borodinó mentioned?\") \n",
|
||||
"\n",
|
||||
"println(sum(occursin.(\"Borodinó\",words))) #\"Borodinó\" or \"Borodinó. or similarly\n",
|
||||
"\n",
|
||||
"println(sum(z->occursin(\"Borodinó\",z),words)) #quicker approach"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"print the line numbers and the lines that contain the word Borodinó:\n",
|
||||
"\n",
|
||||
"38971 they reached Borodinó, seventy miles from Moscow. From Vyázma Napoleon\n",
|
||||
"41375 the twenty-sixth the battle of Borodinó itself took place.\n",
|
||||
"41377 Why and how were the battles of Shevárdino and Borodinó given and\n",
|
||||
"41378 accepted? Why was the battle of Borodinó fought? There was not the least\n",
|
||||
"41399 Before the battle of Borodinó our strength in proportion to the French\n",
|
||||
"41416 In giving and accepting battle at Borodinó, Kutúzov acted involuntarily\n",
|
||||
"41427 On the other question, how the battle of Borodinó and the preceding\n",
|
||||
"41434 position at Borodinó.\n",
|
||||
"41438 to it, from Borodinó to Utítsa, at the very place where the battle was\n",
|
||||
"41445 the field of Borodinó.\n",
|
||||
"41451 during the retreat passed many positions better than Borodinó. They did\n",
|
||||
"41457 and that the position at Borodinó (the one where the battle was fought),\n",
|
||||
"41463 Borodinó to the left of, and at a right angle to, the highroad (that\n",
|
||||
"41481 later, when reports on the battle of Borodinó were written at leisure,\n",
|
||||
"41486 the battle of Borodinó was fought by us on an entrenched position\n",
|
||||
"41493 village of Nóvoe, and the center at Borodinó at the confluence of the\n",
|
||||
"41496 To anyone who looks at the field of Borodinó without thinking of how\n",
|
||||
"41503 to Borodinó (he could not have seen that position because it did not\n",
|
||||
"41514 Borodinó—a plain no more advantageous as a position than any other plain\n",
|
||||
"41531 and chief action of the battle of Borodinó was already lost on the\n",
|
||||
"41551 distinct from the main course of the battle.) So the battle of Borodinó\n",
|
||||
"41554 army and people) it has been described. The battle of Borodinó was not\n",
|
||||
"41557 Shevárdino Redoubt, the Russians fought the battle of Borodinó on an\n",
|
||||
"41749 hundred paces in front of the knoll and below it. This was Borodinó.\n",
|
||||
"41780 “Borodinó,” the other corrected him.\n",
|
||||
"41814 entrenchments. There, you see? There’s our center, at Borodinó, just\n",
|
||||
"41855 A church procession was coming up the hill from Borodinó. First along\n",
|
||||
"42128 Borodinó and thence turned to the left, passing an enormous number of\n",
|
||||
"42133 him than any other spot on the plain of Borodinó.\n",
|
||||
"42643 On August 25, the eve of the battle of Borodinó, M. de Beausset, prefect\n",
|
||||
"42949 The fourth order was: The vice-King will occupy the village (Borodinó)\n",
|
||||
"42957 given him, he was to advance from the left through Borodinó to the\n",
|
||||
"42962 not be executed. After passing through Borodinó the vice-King was driven\n",
|
||||
"42982 Many historians say that the French did not win the battle of Borodinó\n",
|
||||
"42993 battle of Borodinó, and if this or that other arrangement depended on\n",
|
||||
"43015 men at Borodinó was not due to Napoleon’s will, though he ordered the\n",
|
||||
"43022 At the battle of Borodinó Napoleon shot at no one and killed no one.\n",
|
||||
"43026 The French soldiers went to kill and be killed at the battle of Borodinó\n",
|
||||
"43065 than previous ones because the battle of Borodinó was the first Napoleon\n",
|
||||
"43078 Napoleon at the battle of Borodinó fulfilled his office as\n",
|
||||
"43286 Pierre most of all was the view of the battlefield itself, of Borodinó\n",
|
||||
"43289 Above the Kolochá, in Borodinó and on both sides of it, especially to\n",
|
||||
"43297 riverbanks and in Borodinó. A white church could be seen through the\n",
|
||||
"43298 mist, and here and there the roofs of huts in Borodinó as well as dense\n",
|
||||
"43301 the whole space. Just as in the mist-enveloped hollow near Borodinó, so\n",
|
||||
"43386 bridge across the Kolochá between Górki and Borodinó, which the French\n",
|
||||
"43387 (having occupied Borodinó) were attacking in the first phase of the\n",
|
||||
"43819 The chief action of the battle of Borodinó was fought within the seven\n",
|
||||
"43820 thousand feet between Borodinó and Bagratión’s flèches. Beyond that\n",
|
||||
"43825 battlefield. On the field between Borodinó and the flèches, beside the\n",
|
||||
"43834 troops advanced on Borodinó from their left.\n",
|
||||
"43838 to Borodinó, so that Napoleon could not see what was happening there,\n",
|
||||
"43886 Borodinó had been occupied and the bridge over the Kolochá was in the\n",
|
||||
"43890 as soon in fact as the adjutant had left Borodinó—the bridge had been\n",
|
||||
"44222 times repulsed. In the center the French had not got beyond Borodinó,\n",
|
||||
"44301 of the field of Borodinó.\n",
|
||||
"44821 hundreds of years the peasants of Borodinó, Górki, Shevárdino, and\n",
|
||||
"44903 Russians at Borodinó. The French invaders, like an infuriated animal\n",
|
||||
"44909 wound it had received at Borodinó. The direct consequence of the battle\n",
|
||||
"44910 of Borodinó was Napoleon’s senseless flight from Moscow, his retreat\n",
|
||||
"44913 which at Borodinó for the first time the hand of an opponent of stronger\n",
|
||||
"45069 from Smolénsk to Borodinó. The French army pushed on to Moscow, its\n",
|
||||
"45078 consolidated. At Borodinó a collision took place. Neither army was\n",
|
||||
"45097 Russian army were convinced that the battle of Borodinó was a victory.\n",
|
||||
"45185 the twenty-sixth at Borodinó, and each day and hour and minute of the\n",
|
||||
"45186 retreat from Borodinó to Filí.\n",
|
||||
"45449 After the battle of Borodinó the abandonment and burning of Moscow was\n",
|
||||
"45491 that could happen. They went away even before the battle of Borodinó and\n",
|
||||
"45873 Borodinó.\n",
|
||||
"45881 Toward the end of the battle of Borodinó, Pierre, having run down\n",
|
||||
"46434 and commotion. Every day thousands of men wounded at Borodinó were\n",
|
||||
"46442 Some said there had been another battle after Borodinó at which the\n",
|
||||
"47598 to the second of September, that is from the battle of Borodinó to the\n",
|
||||
"48315 ever since the battle of Borodinó, for all the generals who came to\n",
|
||||
"48353 if after the battle of Borodinó, when the surrender of Moscow became\n",
|
||||
"49092 particularly of the battle of Borodinó and of that vague sense of his\n",
|
||||
"50087 ambulance station on the field of Borodinó. His feverish state and the\n",
|
||||
"50105 Borodinó. They were accompanied by a doctor, Prince Andrew’s valet, his\n",
|
||||
"50835 battle of Borodinó, there was a soiree, the chief feature of which was\n",
|
||||
"51280 A few days before the battle of Borodinó, Nicholas received the\n",
|
||||
"51740 The dreadful news of the battle of Borodinó, of our losses in killed and\n",
|
||||
"51747 When he received the news of the battle of Borodinó and the abandonment\n",
|
||||
"53596 The historians consider that, next to the battle of Borodinó and the\n",
|
||||
"53703 whole campaign and by the battle of Borodinó, the Russian army—when\n",
|
||||
"53711 Borodinó had been a victory, he alone—who as commander in chief might\n",
|
||||
"53715 The beast wounded at Borodinó was lying where the fleeing hunter had\n",
|
||||
"54244 or deliberately deceive themselves. No battle—Tarútino, Borodinó, or\n",
|
||||
"54858 of Borodinó. He had sought it in philanthropy, in Freemasonry, in the\n",
|
||||
"55314 day long. At the battle of Borodinó, when Bagratión was killed and nine\n",
|
||||
"55319 And the quiet little Dokhtúrov rode thither, and Borodinó became the\n",
|
||||
"55543 The undecided question as to whether the wound inflicted at Borodinó was\n",
|
||||
"55658 That army could not recover anywhere. Since the battle of Borodinó\n",
|
||||
"55805 The Battle of Borodinó, with the occupation of Moscow that followed it\n",
|
||||
"55840 history: to say that the field of battle at Borodinó remained in the\n",
|
||||
"55844 After the French victory at Borodinó there was no general engagement nor\n",
|
||||
"55855 The period of the campaign of 1812 from the battle of Borodinó to the\n",
|
||||
"55895 retreats after battles, the blow dealt at Borodinó and the renewed\n",
|
||||
"57692 done at Mozháysk after the battle of Borodinó.\n",
|
||||
"58038 French had given battle at Borodinó, did not achieve its purpose when it\n",
|
||||
"58063 enemy in full strength at Borodinó—defeated at Krásnoe and the Berëzina\n",
|
||||
"58772 activity in 1812, never once swerving by word or deed from Borodinó to\n",
|
||||
"58819 Beginning with the battle of Borodinó, from which time his disagreement\n",
|
||||
"58820 with those about him began, he alone said that the battle of Borodinó\n",
|
||||
"58845 this enemy of decisive action, gave battle at Borodinó, investing the\n",
|
||||
"58848 contradiction to everyone else, declared till his death that Borodinó\n",
|
||||
"59762 Borodinó for more than a month had recently died in the Rostóvs’ house\n",
|
||||
"60187 one at Borodinó.\n",
|
||||
"61413 the cold in his head at Borodinó to the sparks which set Moscow on\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(\"print the line numbers and the lines that contain the word Borodinó:\\n\")\n",
|
||||
"for (i,line) in enumerate(lines)\n",
|
||||
" if occursin(\"Borodinó\",line)\n",
|
||||
" println(i,\" \",line)\n",
|
||||
" end\n",
|
||||
"end"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Task 3\n",
|
||||
"\n",
|
||||
"1. Change Borodinó everywhere to Berëzina and then count the occurances"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Change Borodinó everywhere to Berëzina and then count the occurances\n",
|
||||
"125\n",
|
||||
"125\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"println(\"Change Borodinó everywhere to Berëzina and then count the occurances\")\n",
|
||||
"str2 = replace(str,\"Borodinó\"=>\"Berëzina\");\n",
|
||||
"words2 = split(str2)\n",
|
||||
"println(sum(occursin.(\"Berëzina\",words2)))\n",
|
||||
"\n",
|
||||
"println(sum(z->occursin(\"Berëzina\",z),words2)) #quicker approach"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"@webio": {
|
||||
"lastCommId": null,
|
||||
"lastKernelId": null
|
||||
},
|
||||
"anaconda-cloud": {},
|
||||
"kernel_info": {
|
||||
"name": "julia-1.2"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.7.0",
|
||||
"language": "julia",
|
||||
"name": "julia-1.7"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.7.0"
|
||||
},
|
||||
"nteract": {
|
||||
"version": "0.24.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
559
Problemsets/Solutions/PS10_DataFrames_Solution.ipynb
Normal file
559
Problemsets/Solutions/PS10_DataFrames_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
645
Problemsets/Solutions/PS10b_OptionDeltas_Solution.ipynb
Normal file
645
Problemsets/Solutions/PS10b_OptionDeltas_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user