ebook img

Introduction to Python for Econometrics, Statistics and Data Analysis PDF

405 Pages·2014·2.54 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Introduction to Python for Econometrics, Statistics and Data Analysis

Introduction to Python for Econometrics, Statistics and Data Analysis KevinSheppard UniversityofOxford Tuesday5th August,2014 - ©2012,2013,2014KevinSheppard 2 Changes since the Second Edition Version2.2.1(August2014) • Fixedtyposreportedbyareader–thankstoIlyaSorvachev Version2.2(July2014) • CodeverifiedagainstAnaconda2.0.1. • AddeddiagnostictoolsandasimplemethodtouseexternalcodeintheCythonsection. • UpdatedtheNumbasectiontoreflectrecentchanges. • FixedsometyposinthechapteronPerformanceandOptimization. • AddedexamplesofjoblibandIPython’sclustertothechapteronrunningcodeinparallel Version2.1(February2014) • Newchapterintroducingobjectorientedprogrammingasamethodtoprovidestructureandorga- nizationtorelatedcode. • Addedseaborntotherecommendedpackagelist, andhaveincludeditbedefaultinthegraphics chapter. • Based on experience teaching Python to economics students, the recommended installation has beensimplifiedbyremovingthesuggestiontousevirtualenvironment. Thediscussionofvirtual environmentsasbeenmovedtotheappendix. • Rewrotepartsofthepandaschapter. • CodeverifiedagainstAnaconda1.9.1. Version2.02(November2013) • ChangedtheAnacondainstalltousebothcreateandinstall,whichshowshowtoinstalladditional packages. • Fixedsomemissingpackagesinthedirectinstall. • ChangedtheconfigurationofIPythontoreflectbestpractices. • AddedsubsectioncoveringIPythonprofiles. i Version2.01(October2013) • UpdatedAnacondato1.8andaddedsomeadditionalpackagestotheinstallationforSpyder. • SmallsectionaboutSpyderasagoodstartingIDE. ii nd Notes to the 2 Edition Thiseditionincludesthefollowingchangesfromthefirstedition(March2012): • ThepreferredinstallationmethodisnowContinuumAnalytics’Anaconda.Anacondaisacomplete scientificstackandisavailableforallmajorplatforms. • Newchapteronpandas. pandasprovidesasimplebutpowerfultooltomanagedataandperform basicanalysis. Italsogreatlysimplifiesimportingandexportingdata. • Newchapteronadvancedselectionofelementsfromanarray. • Numbaprovidesjust-in-timecompilationfornumericPythoncodewhichoftenproduceslargeper- formancegainswhenpureNumPysolutionsarenotavailable(e.g. loopingcode). • Dictionary,setandtuplecomprehensions • Numeroustypos • AllcodehasbeenverifiedworkingagainstAnaconda1.7.0. iii iv Contents 1 Introduction 1 1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 ImportantComponentsofthePythonScientificStack . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 UsingPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.A FrequentlyEncounteredProblems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.B register_python.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.C AdvancedSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 Python2.7vs. 3(andtherest) 27 2.1 Python2.7vs. 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2 IntelMathKernelLibraryandAMDCoreMathLibrary . . . . . . . . . . . . . . . . . . . . . . . . 27 2.3 OtherVariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.A RelevantDifferencesbetweenPython2.7and3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3 Built-inDataTypes 31 3.1 VariableNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.2 CoreNativeDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.3 PythonandMemoryManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4 ArraysandMatrices 47 4.1 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.2 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.3 1-dimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.4 2-dimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.5 MultidimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.6 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.7 AccessingElementsofanArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.8 SlicingandMemoryManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 v 4.9 importandModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.10 CallingFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5 BasicMath 63 5.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 5.2 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 5.3 ArrayandMatrixAddition(+)andSubtraction(-) . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.4 ArrayMultiplication(*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 5.5 MatrixMultiplication(*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 5.6 ArrayandMatrixDivision(/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 5.7 ArrayExponentiation(**) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 5.8 MatrixExponentiation(**) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.9 Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.10 Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.11 OperatorPrecedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6 BasicFunctionsandNumericalIndexing 71 6.1 GeneratingArraysandMatrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.2 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.3 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 6.4 ComplexValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6.5 SetFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6.6 SortingandExtremeValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.7 NanFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 6.8 FunctionsandMethods/Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 7 SpecialArrays 83 7.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 8 ArrayandMatrixFunctions 85 8.1 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 8.2 ShapeInformationandTransformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8.3 LinearAlgebraFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 8.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 9 ImportingandExportingData 99 9.1 ImportingDatausingpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 9.2 ImportingDatawithoutpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 9.3 SavingorExportingDatausingpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 vi 9.4 SavingorExportingDatawithoutpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 9.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 10 Inf,NaNandNumericLimits 109 10.1 infandNaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 10.2 Floatingpointprecision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 10.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 11 LogicalOperatorsandFind 113 11.1 >,>=,<,<=,==,!= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 11.2 and,or,notandxor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 11.3 Multipletests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 11.4 is* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 11.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 12 AdvancedSelectionandAssignment 119 12.1 NumericalIndexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 12.2 LogicalIndexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 12.3 PerformanceConsiderationsandMemoryManagement . . . . . . . . . . . . . . . . . . . . . . .128 12.4 AssignmentwithBroadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 13 FlowControl,LoopsandExceptionHandling 133 13.1 WhitespaceandFlowControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 13.2 if...elif...else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 13.3 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 13.4 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 13.5 try...except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 13.6 ListComprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 13.7 Tuple,DictionaryandSetComprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 13.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 14 DatesandTimes 143 14.1 CreatingDatesandTimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 14.2 DatesMathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 14.3 Numpydatetime64 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 15 Graphics 147 15.1 seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 15.2 2DPlotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 15.3 Advanced2DPlotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153 15.4 3DPlotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 vii 15.5 GeneralPlottingFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 15.6 ExportingPlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166 16 StructuredArrays 167 16.1 MixedArrayswithColumnNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 16.2 RecordArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 17 pandas 171 17.1 DataStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171 17.2 StatisticalFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 17.3 Time-seriesData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192 17.4 ImportingandExportingData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196 17.5 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 17.6 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 18 CustomFunctionandModules 207 18.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 18.2 VariableScope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 18.3 Example: LeastSquareswithNewey-WestCovariance . . . . . . . . . . . . . . . . . . . . . . .215 18.4 AnonymousFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 18.5 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 18.6 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 18.7 PYTHONPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 18.8 PythonCodingConventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 18.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 18.A Listingofeconometrics.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 19 ProbabilityandStatisticsFunctions 225 19.1 SimulatingRandomVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 19.2 SimulationandRandomNumberGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 19.3 StatisticsFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 19.4 ContinuousRandomVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 19.5 SelectStatisticsFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 19.6 SelectStatisticalTests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 19.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241 20 Non-linearFunctionOptimization 243 20.1 UnconstrainedOptimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 20.2 Derivative-freeOptimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 20.3 ConstrainedOptimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 20.4 ScalarFunctionMinimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252 viii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.