Immediate download Introduction to Python for Econometrics, Statistics and Data Analysis Kevin Sheppard ebooks 2024
Immediate download Introduction to Python for Econometrics, Statistics and Data Analysis Kevin Sheppard ebooks 2024
com
https://ebookmass.com/product/introduction-to-python-for-
econometrics-statistics-and-data-analysis-kevin-sheppard/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmass.com/product/introduction-to-python-for-econometrics-
statistics-and-data-analysis-5th-edition-kevin-sheppard/
ebookmass.com
https://ebookmass.com/product/introduction-to-statistics-and-data-
analysis-6th-edition-roxy-peck/
ebookmass.com
https://ebookmass.com/product/statistics-and-data-analysis-for-
nursing-research-2nd-edition-2nd-edition/
ebookmass.com
https://ebookmass.com/product/introduction-to-research-methods-and-
data-analysis-in-psychology-darren-langdridge/
ebookmass.com
Introduction to Econometrics 3rd Edition James H. Stock
https://ebookmass.com/product/introduction-to-econometrics-3rd-
edition-james-h-stock/
ebookmass.com
https://ebookmass.com/product/introduction-to-econometrics-global-
edition-james-h-stock/
ebookmass.com
https://ebookmass.com/product/introduction-to-linear-regression-
analysis-wiley-series-in-probability-and-statistics-6th-edition-
montgomery/
ebookmass.com
https://ebookmass.com/product/an-introduction-to-statistical-methods-
and-data-analysis-7th-edition-ebook-pdf/
ebookmass.com
https://ebookmass.com/product/enzymes-a-practical-introduction-to-
structure-mechanism-and-data-analysis-3rd-edition-robert-a-copeland/
ebookmass.com
Introduction to Python for
Econometrics, Statistics and Data Analysis
3rd Edition, 1st Revision
Kevin Sheppard
University of Oxford
• Verified that all code and examples work correctly against 2019 versions of modules. The notable
packages and their versions are:
• Python 2.7 support has been officially dropped, although most examples continue to work with 2.7.
Do not Python 2.7 in 2019 for numerical code.
• Fixed direct download of FRED data due to API changes, thanks to Jesper Termansen.
• Thanks for Bill Tubbs for a detailed read and multiple typo reports.
• Tested all code on Pyton 3.6. Code has been tested against the current set of modules installed by
conda as of February 2018. The notable packages and their versions are:
– NumPy: 1.13
– Pandas: 0.22
ii
Notes to the 3rd Edition
This edition includes the following changes from the second edition (August 2014):
• Python 3.5 is the default version of Python instead of 2.7. Python 3.5 (or newer) is well supported by
the Python packages required to analyze data and perform statistical analysis, and bring some new
useful features, such as a new operator for matrix multiplication (@).
• Removed distinction between integers and longs in built-in data types chapter. This distinction is
only relevant for Python 2.7.
• dot has been removed from most examples and replaced with @ to produce more readable code.
• Split Cython and Numba into separate chapters to highlight the improved capabilities of Numba.
• Verified all code working on current versions of core libraries using Python 3.5.
• pandas
• New chapter introducing statsmodels, a package that facilitates statistical analysis of data. statsmod-
els includes regression analysis, Generalized Linear Models (GLM) and time-series analysis using
ARIMA models.
iv
Changes since the Second Edition
• Added diagnostic tools and a simple method to use external code in the Cython section.
• Added examples of joblib and IPython’s cluster to the chapter on running code in parallel.
• New chapter introducing object-oriented programming as a method to provide structure and orga-
nization to related code.
• Added seaborn to the recommended package list, and have included it be default in the graphics
chapter.
• Based on experience teaching Python to economics students, the recommended installation has
been simplified by removing the suggestion to use virtual environment. The discussion of virtual
environments as been moved to the appendix.
• Changed the Anaconda install to use both create and install, which shows how to install additional
packages.
This edition includes the following changes from the first edition (March 2012):
• The preferred installation method is now Continuum Analytics’ Anaconda. Anaconda is a complete
scientific stack and is available for all major platforms.
• New chapter on pandas. pandas provides a simple but powerful tool to manage data and perform
preliminary analysis. It also greatly simplifies importing and exporting data.
• Numba provides just-in-time compilation for numeric Python code which often produces large per-
formance gains when pure NumPy solutions are not available (e.g. looping code).
• Numerous typos
1 Introduction 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Important Components of the Python Scientific Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.A Additional Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5 Basic Math 57
5.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.3 Addition (+) and Subtraction (-) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.4 Multiplication (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.5 Matrix Multiplication (@) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.6 Array and Matrix Division (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.7 Exponentiation (**) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.8 Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.9 Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.10 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
7 Special Arrays 77
7.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
15 Graphics 141
15.1 seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
15.2 2D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
15.3 Advanced 2D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
15.4 3D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
15.5 General Plotting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
15.6 Exporting Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
xii CONTENTS
16 pandas 161
16.1 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
16.2 Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
16.3 Time-series Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
16.4 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
16.5 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
16.6 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
T HE structure, economy and polity of our time do not incline the meek
and lowly to a particular regard for persons of condition. Nor is the
patronage of princes and the favour of lords solicited to any noticeable
degree by the poets and scientists of the day. The most superficial survey of
history will discover that the condescension of a gentleman of the haut ton
was once regarded as almost an essential of a poet’s success: while the
craftsman, was he never so cunning and exquisite, must rely for his fame on
the caprice of the young men of fashion, who were, it is to be presumed, not
the less generous because they were invariably in debt and had not the worse
taste because they were nearly always in wine.
In our generation, however, we have progressed so far in the liberal arts
that, should a man of letters so mask himself with the impertinence of
fashion as to be remarked at Ascot in clothes which, with a deplorable want
of faith in the dignity of letters, have been cut to fit his person, he shall at
once be convicted by all really intelligent people of a lack of feeling for all
that is genuine in art and literature. That cannot be altogether just. An
effeminate manner and unusual habits should not, on the other hand,
invariably be taken for sure signs of genius in the mental sciences; and
laymen should be warned against regarding soiled linen as an essential of the
successful ascent of Parnassus.
In the face of this illiberal attitude towards the upper sort, the popular
interest in the young Duke of Mall is the more surprising; and to that
gentleman’s familiars and dependents it has for long been a source of
gratification to observe how the esteem in which he is held by the people of
England is rivalled only by the interest shown in the table-manners of the
most famous pugilists and the respect extended to the tireless energies of the
most beloved prince in Christendom.
Nor was the young Duke’s greatness unheralded, his birth without good
omen: historians the world over will know the legend of the Dukedom of
Mall, how it was prophesied by a sibyl of the Restoration that on the birth of
the greatest of that house the golden cock on the weather-vane of St. James’s
tower would crow thrice, and on his death it would also crow thrice. And
only those most steeped in the modern vice of scepticism will disbelieve the
unanimous evidence of every club servant in St. James’s Street, that this
miracle attended the birth of the seventeenth Duke; while we vulgar lovers
of England’s might and enemies to the Socialist tyranny can only pray that
the second manifestation of that miracle be averted for the longest span of
God’s mercy to the most gallant of His creatures.
There follow, then, some sidelights on the recent life of the young Duke
of Mall and his splendid lady. Than these two, history will say, history must
say, there never was a more comely pair; for such is the unknowable wisdom
of the All-Wise, that opposites will discover the sweetest harmony. The
differences referred to are, of course, those of breeding and nationality, for
the lady was an American out of Chicago, in the State of Illinois. But to
attempt to describe Miss Lamb were to challenge contempt and defy the
limitations set by the gods upon human speech. Let it suffice that she was
beautiful: the quality of her colour comparable only to that of a garden in
tempered sunlight, the texture of her complexion the envy of silkworms,
while the glory of her hair has been described by a minor poet as a cap of
beaten gold and autumn leaves. As for the lady’s eyes, shall a phrase attempt
where a thousand photographs have failed?
The Duke, then, was tender of this lady: he wooed her, was mocked, he
entreated, was beguiled, he pleaded, was provoked, he stormed, was
dismissed, he worshipped, was accepted. The wedding paralysed the traffic
of London for several hours and the newspapers of England and America for
several days. The happy pair spent their honeymoon at the Trianon at
Versailles, lent to the young Duke by the French Government in recognition
of his gallant services as a liaison officer during the war.
It should be noted that the wedding-present of the bride’s father to the
young Duke was an ocean-going yacht of gratifying tonnage. White and
graceful, the yacht Camelot rode the seas like a bird. The Duke, who liked
birds, was very impressed.
II
That, however, was some time ago. Now, alas, not the most kindly
observer of society can but have remarked that the recent life of the young
Duke and his Duchess has been as conspicuous for its private dolour as for
its public splendour. There have been rumours, there has been chatter. This
has been said, and that, and the other. Gossip, in fact, has been rife. But it is
the austere part of the historian to deal only in facts. The facts are as follows:
South of the lands of the old troubadours, between the heights of the
Southern Alps and the languor of the Mediterranean, lies the pretty town of
Cannes. The year we tell of was in its first youth. The flower and chivalry of
England and America were promenading in the sunlight of the pretty town or
commenting at their ease on the brilliant tourneys of tennis and polo. Here
and there about the links the sun lit up the brilliant Fair Isle sweaters of
Jews, Greeks and Argentines where they were playing a friendly match for
the empiry of the world. The mimosa was at its full glory of fresh-powdered
gold. Brilliant sun-shades lit the walks. From the gardens of white villas
could be heard the laughter of children and millionaires. The beach was
strewn with jewels, and ladies walked in beauty. Great automobiles loitered
between the Casino and the Carlton Hotel, while youth in swift Bugatti or
Bentley challenged time to a race from Cannes to Monte Carlo. The waters
slept profoundly in the full kiss of the afternoon sun. There, as a dove on a
spacious lawn, rode a fair white yacht. From its stern hung a cluster of
golden cherries, for such was the pretty nautical device of the young Duke of
Mall.
It must be granted by the most fastidious that the scene was set for
enchantment. The sea slept under the sun, the sun upon the mountains, the
chauffeurs at their driving-wheels, the croupiers in the Casino, the diplomats
at a conference, the demi-mondaines near the diplomats. Yet in the yacht
raged a storm: the Duke of Mall was having a row with his lady.
It will be incredible that it was not their first. It must be incredible that it
looked like being their last. At the moment of our intrusion, my Lord Duke,
in point of fact, was saying:
“By Heaven, Leonora, I am sick and tired of it!”
That small, lovely head, those wide, deep, gentle eyes! Yet stern Juno
herself did sometimes walk the earth in those very eyes. She was not more
than twenty-four, this lady, yet with what proud calm and disdain she could
at one glance enwrap her husband! Not, however, that it always advantaged
her case, for sometimes it might be he was too sleepy to notice or maybe he
would be too busily engaged in disdaining her, which on occasions he could
do very handsomely.
Gently said she: “You say you are sick and tired of ‘it.’ ‘It,’ my dear, my
well-beloved? Am I, by ‘it,’ to understand that you mean me?”
The young Duke pointed his indifference with the application of a match
to a rough surface and the application of the match to a cigar. “You may,”
said he, “understand what you like. I said what I said.”
Tenderness was never yet so fitly clothed as by this lady’s voice. “Shall I,
then,” said she, “tell you all that I understand by what you said?”
The Duke need not have waved a hand skyward, need not have smiled,
have yawned, and said: “Am I God, to stop you talking! But maybe it is not
necessary for me to add that I wish I were, if only for that purpose.”
The Duchess said: “However, I will not be provoked. It is too hot. I will
content myself merely with remarking that in my considered opinion the
ancient Dukedom of Mall does at present grace one with the manners of a
boor and the habits of a stable-boy.”
“Leonora, you go too far!”
She sighed: “Dear, had I, before marrying you, gone even a little further,
how much more comfortably I had fared!”
For as long as it takes to say a forbidden word of one syllable the young
Duke’s fair features wore the air of a battlefield: thereon anger fought with
apathy: but was, by the grace of God and a public-school education,
repulsed.
“Not, mind you,” said the Duchess, “that I can blame the pretty dolls
whom you encourage to pursue you under my very nose.”
The Duke remarked that she had a very beautiful nose, a very small nose.
The Duchess thanked him.
“But,” said the Duke, “by the number of things which you accuse me of
doing under it, any one would think it cast as long a shadow as Lord
Nelson’s column. For the sake of your own beauty,” he pleaded earnestly,
“may I beg you to leave your nose, much as I admire it, out of my supposed
infidelities?”
The Duchess remarked that she could quite well understand why women
pursued him with their attentions. Yet, as she spoke, no spark of bitterness
pointed her low light voice, no trace of jealousy marred her urbanity. She
remarked that he was very rich. His rank was second only to his King’s. He
was very handsome. He was charming.
The Duke thanked her.
“However,” said the Duchess.
“Ah, that’s not too good,” sighed the Duke. “I knew there was a catch
somewhere.”
“However,” said the Duchess, “the beauty that you most admire in any
woman is the beauty of her not being a woman you already know: the only
charm of which you never are tired is the charm of novelty.”
“One likes a change,” sighed the Duke. “If that’s what you are talking
about.”
“It certainly is,” said the Duchess.
“Well, don’t let me hinder you,” said the Duke. He was rude. “I am all
attention. But should I interrupt you, sweet, you must forgive me, for I am
apt to talk in my sleep.”
“Oh, but haven’t I made quite a collection of names like Dolly and Lucy
and Maudie!”
The Duke said one word. It expressed all the volumes that could be
written by the men who, alas, cannot write. But the Duchess had now been
in England for four years and knew that the facility with which an
Englishman can swear at his wife does not detract in the least from his deep
respect for Womanhood, else would England be what England undoubtedly
is?
She said: “Maximilian, I want to tell you that you are a most
extraordinary man. In public, for instance, you are all that is charming; and
many who know of our private disagreements can’t but think the fault is
mine, since in public you are so very right and seem never for a moment
deficient in the manners, graces and consideration proper to a great
gentleman.”
The Duke expressed a hope that she would put that down in writing, so
that he could send it as a reference to any lady, or ladies, to whom he might
be paying his suit, or suits.
“However,” said the Duchess, “when we come to examine you in the
home, what a different picture do we find! Your manners are monstrous,
your graces those of a spoilt schoolboy, while your consideration for your
wife such that, far from concealing from me your preference for the
company of low women, you will actually,” said she, “bring them on board
this yacht and make love to them under my very——”
The Duke, he sighed.
“In,” snapped the Duchess, “my company. And now,” she added calmly,
“I will say good-bye.”
“Child,” said the Duke softly, “must you go? Must you really? Can’t I
tempt you to stay? Very well, then,” said he, “good-bye.”
“Captain Tupper!” the Duchess called.
“Captain Tupper,” the Duchess said, “I am going ashore. You will please
see to it at once. I think my maid has everything packed. Thank you.”
The Duke opened his eyes. It was an effort, for he was sleepy.
“Captain Tupper,” said he, “her Grace will take the fastest cutter to the
town to catch the Blue Train to Calais. Should a sleeper on the Blue Train be
unavailable, you will see to it that she is accommodated with one of a
suitable colour. We, in the cool of the evening, will make for Naples. Thank
you.”
The Duke closed his eyes again, for he was sleepy. The Duchess stared as
though into the heart of the still blue bay, and who shall say what it was that
she saw in that deep place, whether she saw the towers of her love torn down
by the winds of man’s discontent, the ruins of her marriage washed in the
infinite sea of man’s inconstancy? Her eyes darkened, and presently she said,
bemused: “I am going now. Adieu, Maximilian.”
“Leonora,” he said, with closed eyes, “I wish you all happiness and
content.”
“Content!” said she, and laughed.
“Good-bye, Leonora.”
She said: “Max, we were very happy once. We were lovers once. So
happy—once upon a time!”
He whispered:
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com