I’ve finally got round to publishing some code and a vignette (a how-to) for our research paper “Optimal Design of Experiments on Connected Units with Application to Social Networks“.

Summary of Theory

Consider a simple network as shown in the image. Nodes 1,2,3, and 4 represent people in a network, and we wish to show an advert to each of them.

 

The idea behind the model is that if a treatment is given to subject 2, connections of subject 2 might be affected by the treatment, and their response might be altered because of the treatment I gave to subject 1.

The total effect on subject 1 is determined by whatever treatment I give to subject 1 himself, plus an effect due to the treatment I gave to subject 2.

This is formalised in the Linear network effect model. In the paper, we consider how to optimally design experiments where treatments can be transmitted through a network in this way.

 

R Code and Vignette

The aim of the vignette is to provide the means for users to reproduce the results in that paper, and extend them to their own work. This vignette, and indeed the whole package, is very much a draft, and suggestions
for changes/improvements are welcomed.

Vignette: A vignette which explains how to use the package

Code: Available on github

 

Future work

Myself and collaborators are currently working on extensions to this work e.g for Block Designs(arXiv:1902.01352), Faster algorithms for designs using networks (arXiv:1802.09582 ), and Viral Networks. Watch this space for expansions to the software!

Recently a lot of universities, including my own, have been asked to conduct teaching online due to the COVID-19 outbreak. For a while, I did quite a lot of online teaching and tuition, so I thought I would share this in case it helps anyone else teaching Mathematics or related fields. I found online tuition for mathematics very effective, and sometimes even better than face-to-face tuition for some topics.

Chalk and Talk

Mathematics is an unusual sport in that the vast majority of it still uses traditional teaching; the lecturer writes on some kind of board and students copy some of it down. In tutorials, students and teachers will often share a piece of paper, whiteboard, or blackboard. Although blackboards and chalk are not so common anymore, the basic principle of developing a proof or an argument, or performing a calculation live in front of students is still, I think, a very common form of teaching. (For some discussion of this, I love Prof. Korner’s essay, “In Praise of Lectures”)

Replacing this face-to-face learning with an online equivalent is therefore essential, so here are some tools that might help. Essentially, there’s three things you need: something to write on, a decent web camera, and the right software.

Mathematicians tend to be quite computer literate, and a large number don’t like Microsoft windows, and use Mac, or linux- compatibility in hardware and software is also needed.

Writing

Handwriting is still important in Mathematics, so having some hardware that allows writing on a screen is essential.

  • I use a drawing tablet to draw, which essentially replaces a mouse with a pen, and enables you to write when you depress a mechanical nib on a special mat. Personally, I chose a VEIKK A30 Digital Drawing Tablet (UK Link/ US Link)which is ten inches by six inches (about the size of an A4 piece of paper), and costs about £50. I use it with linux, but it is also compatible with Windows and Mac. (The market leader is Wacom, but in my opinion this is much more expensive, but overkill for mathematics- budding artists, etc, may find it has more features which are not needed.)
  • Some people prefer to draw directly on the screen of a device. I found this a little less good myself, as you are always looking down, and there is a small but annoying delay between writing and it appearing on the screen. These are much more expensive options, but if you are in the market for a new tablet or computer, worth considering. Some options, which really depend on your ecosystem:
    • The new iPads all work with an optional Apple Pencil, but this is close to £500, even with the educational discount. I find the baseline model (10.2″ diagonal, slightly smaller than A4) a little small for writing a page of mathematics, and the bigger iPad pro is better, but more expensive. I found it slightly annoying to change between apps as well, and you are always charging them.
    • The Samsung galaxy tablets and the S-pen are very good for android users, but have many of the same flaws as the iPad. They come at 10.5 inches, but again are above £400 quid.
    • If you are in the market for a new laptop anyway, I really like the 2-in-1 devices, which are PCs that come with a stylus with which you can draw on the screen, and can run Windows or even Linux. I have a Dell XPS 2 in 1, as the pen is fantastic, but there are many options now which might suit all budgets.

Webcamera and microphone

If you do a lot of online tuition or teaching, having a decent quality webcamera and microphone is important for audience experience. You may have one built into your PC or laptop, but this makes a big difference for the recipients of your teaching.

I use a Logitech C920 webcamera, which records in HD (1080p), sufficiently good quality, and also records sound well. It is compatible with linux (and Windows and Mac). You can spend more or less, but at around £50, this is a good investment in my opinion, and a good balance between cost and functionality.

Software

Here there is a lot of choice, and your choice in software might be imposed on you by your institution. Some tools I like:

  • For live one-to-one or one-to-n teaching, where n≤4, I really like bitpaper . Essentially you can share a whiteboard between you and your students/ collaborators, see each other, and all of you can draw on it just as if you were standing by a whiteboard. It is multi-platform, and works across all browsers. You can also cut and paste images, upload files, and share screen. There is a built-in video system, or you can use a separate app. This is free. Bitpaper have recently started charging the tutor, but for most people this is $8-$10 per month.
  • For recording videos (asynchronous teaching)
    • Use a screen recorder to record whatever you are doing in your screen.
      • OBS is software that you can use to record anything- it can capture your screen and then export to video.
      • For Mac users, you can use the in-built screen recorder (or QuickTime) – support.apple.com/en-us/HT208721 – together with bitpaper.(thanks to Tim Waite for the tip!)
    • Explain Everything is a great tool. There’s a small learning curve, but again you can write on the screen , add images, pdfs, show examples of software, (Here’s an example of a fun probability problem I did to mostly try out the software.) To produce polished videos takes some time, but to record your own writing on a whiteboard is very easy. Apps exist for apple and android.
      I have used for recorded videos, but live collaboration is also possible.
      There is a free option, although the paid for option is worthwhile if you want to use any of the advanced features.
    • If you want to polish your videos, you can edit the video if you have the time or inclination- I use Openshot. (free and open source). Many people are afraid of videos in that they don’t look professional. I think in the circumstances, students appreciate anything you do, and you should concentrate on good clear content, and now worry about your hair or special effects!
    • As an example of what you can do (and your lectures will be much better technically) here is the start of a short section I made using OBS and bitpaper. I recorded myself with the webcam while delivering the lecture, and recorded a bitpaper window.
  • For interactive lectures, seminars (synchronous teaching), I am yet to find a perfect option
    • Most institutions I have worked at or visited use panopto for lecture capture, and you may have a good setup in your institutional lecture rooms which negate you doing a lot more work. If you can’t use your university lecture rooms, you can also download a client to your own PC which lets you stream from your webcam and broadcast your screen. Chat rooms are also possible so participants can ask questions. Often Panopto recordings are integrated with virtual learning environments such as blackboard.
      If your institution has subscribed to this, it is probably the best option, although the software does not run on linux, and I have found university admins sometimes put some restrictions on what is allowed- worth talking to them though!
    • Blackboard Collaburate Ultra allows you to share slides, your webcam, use the built in whiteboard, and share , for example, computer code or a whiteboard app such as bitpaper. You may have breakout rooms, engage in chat, and do all kinds of things you would do in a face-to-face class. I find it really good, intuitive for students and lecturers, and would recommend it if your university subscribes. It also can automatically record to Blackboard, meaning that students don’t miss out if they don’t attend class This is one of the more user-friendly tools I have found. The web client works for me in Linux and Chrome. If your institution has a subscription, it’s a really good tool.
    • Zoom is a web conferencing software that includes the ability to share a whiteboard, or share a screen and use another tool such as slides, powerpoint, or bitpaper.
      I like it as it works very well cross platform, and is intuitive, and easy to send a link to someone to join in and view on the web.  There are severe limits on the free plan (40 mins maximum), but the paid plans (around £12 per month) are a good option for recreating a lecture environment.
      Breakout rooms are also possible, and you can set up ways to allow students to raise their hands and give instant feedback in a lecture/class. Recordings are also possible. Be careful of security/privacy concerns, and set passwords for your meetings. This would be my recommended tool if you can’t use Blackboard Ultra Collaborate.
    • Many universities use Skype for Business (being incorporated as Microsoft Teams) , and it does have a limited whiteboard option. I have found these sessions to be technically poor in terms of video quality and quite difficult to arrange as the cross-platform support tends to be mixed. Recording can also be added on centrally (at an institutional level) or you can record using other software. It is getting better, and if your organisation has sold its soul to Microsoft, it’s well worth checking out. (Similarly, and I haven’t used it, if your institution has invested in Google, Hangouts Meet might be a good option)
    • Youtube live has great cross-platform compatibility, but doesn’t have a built in whiteboard. You can use another whiteboard, and share your screen, and broadcast it via youtube. This is something that pretty much everyone can see, on their TV, phone, computer, wherever, so for public lectures or to broadcast to the masses, this could also be a good technique. Participants have the ability to chat, which may or may not be constructive!
    • For all these options, for a large class, if you do have an assistant who can help you run the tech, moderate comments, respond to student queries, it helps things along.

I’d be interested to hear any other great solutions in the comments below or drop me an email at web (at) ben-parker.co.uk

Conclusion (what I do:)

  • Use a drawing tablet and better quality webcam (total outlay: around £100)
  • Use bitpaper and the built in video service for teaching 1 to one or small groups.
  • Use blackboard collaborate ultra for interactive-classes , or if not available, Zoom.
  • Use OBS to make recorded lectures, and Openshot to edit them lightly.

It’s that time of year again- a glass of wine, friends and family gathered round the tellybox, staying up late with anticipation of the big day: it’s statistical Christmas- election night! All night swingometers, lots of numbers, and everyone is trying to predict who’s going to get the present they’ve always wanted, and who will get the statistical lump of coal.

I have been amazingly lucky for the last few elections, correctly predicting the EU Referendum result would be 52-48, and getting very close on the US Elections and last couple of UK General Elections. Friends have asked for my election prediction as I am viewed as some sort of Electoral Nostradamus now, so I now write down my prediction in advance and hope to be seen as the true mortal that I am by getting it vastly wrong- the statistical King Canute.

I should caveat again by saying that I am not  a specialist in poling or anything like that; I maintain an interest in it, and know a little statistics of course, but am happy to be challenged, corrected, and told I’m wrong!

If you’re interested in the Maths of Elections, we recently did a podcast of Maths at: The Election ( If you’re reading this, either you like elections or you know me, both of which are great reasons to tune in)

How Polling Is Done

Essentially there’s a few ways that polling is done- by online, telephone,  or face-to-face. These all come with different degrees of difficulty and expense, but generally online is the cheapest, followed by telephone, followed by face-to-face.
When polling, the idea is to interview a representative group of voters, such that the surveyed people will answer in the same way as the voters as a whole. So if 50% of people we ask vote for the Green Party, we expect 50% of the voters to do so.

There are several errors that can be made in polling such that the poll is not representative

  • People refusing to answer you or worse, lying to you.
  • People changing their mind between the poll and election day
  • The pollsters asking the wrong people.

If you remember, for the 2015 General election, pollsters widely predicted a dead heat, but in the end the Conservatives got a lead of 7%. In a very widely circulated piece of work led by Patrick Sturgis, some detailed investigation found that, 117 pages later, essentially, the poll asked the wrong people. Getting a representative sample of the voting population is difficult.

Think of it as an exercise- if you wanted to call people up to get their views on something, how would you even get a list of people to call? Many people don’t take calls from unknown numbers. Many people don’t have time to do a telephone survey. Until relatively recently, pollsters didn’t even contact people on mobile phones, meaning that an entire younger generation without landlines were excluded. It’s fairly clear that telephone surveys will over sample older people, who are more likely to be to the right politically. Politico recently reported that only 6% of Americans respond to phone surveys (although clearly the UK may be different). Phone polling, once the gold standard, may be finished.

Similarly, online sampling may connect better with a younger demographic, and older people may be left out. Whilst it’s appealing for YouGov to pay 50p for a young person to fill in a survey on their smart wi-fi enabled potato peeler, would you really get an octagenarian doing the same? Of course there are exceptions, but how you construct the sample really matters.

Broadly, if we can get a representative  sample of around 1000 people, we will be able to predict the each party to within 1.5%, and a representative sample of around 2000 people would be within 1%. So we need a surprisingly small sample to get the right number, if it is representative. 

Adjusting the Polling

Pollsters know that they have sampled the wrong people by asking them demographic questions, for example. So if the voting population is 50% male and 50% female, and the sample ends up 60% male and 40% female, they weight the female responses up and the male responses down. They do this for a number of categories: age, gender, social group, education level, but also for how people voted in previous elections.

This is an entirely sensible approach to sampling, but again it relies on the respondents not lying to you in some way, and also on having accurate information about demographics of the population. Essentially, there is quite a lot of hidden judgement here about what factors are important in weighting, so whilst the polling will be random and scientific, there will be some subjectivity in the weighting. Members of the British Polling Council will publish their decisions on the weighting, but we have to take care that polls are done slightly differently and that there is some subjective massaging the numbers.

Here is (from wikipedia) the list of polls conducted in the last few days.

Look how different they are. My considered opinion is that the pollsters have not exactly covered themselves in glory for the last few elections, and I see nothing to convince me that their guesses will get any better for this one. The wide disparity between opinion polls on the same day show me how wrong they are likely to be. I therefore take these polls with a large pinch of salt.

Translating polling to a national model

Despite what people tell you, we do not have an election happening on Thursday; we have 650 elections, where everyone votes for who they want to represent them (remember this: you are not voting for Johnson or Corbyn, but for someone who will represent you!). In each constituency, whoever gets the most votes wins. The national percentage of who votes for a party is only slightly related to who gets an MP.

Here’s an extreme example of how Blue can get 60% of the votes, but still lose an election in 5 districts.


So how do we work out from sampling a small proportion of the UK electorate who wins in the UK with 650 constituencies? Essentially, we take the results we have last time. Then, if the blue party gets 1% more votes than it did last time, we add 1% to the result in each constituency.  We assume that the gain in votes (the swing) is the same in every constituency across the country, and this is known as the Uniform Swing Model.

It’s rubbish. It doesn’t work. People don’t vote in the same way in Dundee as in Dungeness. There are a number of models that try to do better. My favourite is Martin Baxter’s Electoral Calculus. The model takes into account many important factors: for example, if there is an incumbent MP, that MP is more likely to do well the next time. Whilst he doesn’t list the model openly, he does tell us about the features and provide evidence that this is to be more trusted than other simpler models. Previous predictions using this model have been better than most competitors as well.

A major problem in election modelling, and even with the electoral calculus model, is that pollsters do not publish their models in full or leave them open to review. This is bad science. We can have no confidence in their correctness.

New approaches in Polling

One very clever new approach in polling is the YouGov MRP poll (Multiple Regression and Stratification). In their words:

The idea behind MRP is that we use the poll data from the preceding seven days to estimate a model that relates interview date, constituency, voter demographics, past voting behaviour, and other respondent profile variables to their current voting intentions. This model is then used to estimate the probability that a voter with specified characteristics will vote Conservative, Labour, or some other party. Using data from the UK Office of National Statistics, the British Election Study, and past election results, YouGov has estimated the number of each type of voter in each constituency. Combining the model probabilities and estimated census counts allows YouGov to produce estimates of the number of voters in each constituency intending to vote for a party.  In 2017, when we applied this strategy to the UK general election, we correctly predicted 93% of individual seats as well as the overall hung parliament result.

This is certainly, in my opinion, the way forward in polling- we’re borrowing knowledge from across the country, so we know that unemployed 45 year olds with a degree in Norwich are likely to vote in a similar way to unemployed 45 year olds with a degree in Cromer. Overall, the huge sample size as well helps smooth out some bumps, but this alone doesn’t help with accuracy too much, as even a small sample can be accurate if representative.

Does it work? In my opinion (and this is controversial), no! At least, it’s not been tested. The YouGov model has only been tried in anger at the 2017 election, and it correctly predicted 93% of individual seat results. However, is that a great achievement- 579 seats did not change hands at the last election, meaning you can get an 89% prediction accuracy just by predicting the status quo. The YouGov model got it wrong at the detail level as well. To be fair to them, they have only just started with the model, and have limited data points (one) available. But although they are not definitely using a better method, the problems with polling the right people, and the fact that demographic information on each constituency is not 100% accurate, are not resolved. Also, they have not (to my knowledge) submitted their model to peer review, so how can we say it is justified?

In particular, a big problem in polling (that isn’t lost with the YouGov method) is working out the likelihood of people to actually get to the ballot box. For the traditional polling, they ask people how likely they are to vote, and discount those that rank themselves less likely. For the Yougov model, the turnout is predicted by the model itself: they use the last election to predict this, so a 25 year old in 2017 will have the same likelihood of voting as one in 2019.  Turnout is likely to be a big factor, and with a close election, one seen as important politically, is this assumption really valid? My belief is that this will be the Achilles heel of the YouGov model as turnout does vary significantly. With a December election, and a very strange electoral climate in the UK, we could see substantial differences.

Turnout over previous elections:

(Image from https://inews.co.uk/news/politics/turnout-general-election-uk-voter-brexit-referendum-europe-elections-1337817 )

 

Putting it together and making a prediction

Some more comments before I nail my party political colours to a mast!

  • Momentum. The polls are certainly narrowing. There is no way that a pollster can take into account momentum as people can change their mind in the last days of the election- this is the point of campaigning! Whilst the pollsters show the Conservative vote fairly steady, there is some evidence that Labour are gathering some votes, mostly at the expense of the LibDems. Note that whilst I don’t trust the individual polls, as long as the polls are repeated in the same way, we can get some evidence that things are moving in or out of one parties favour. 
  • Turnout- crucial as always. The YouGov MRP is the best poll, but I think has perhaps modelled turnout wrong. I think the turnout may well be higher (those that have registered for a December election are more likely to vote), so again I think this will not favour the conservatives. (The weather forecast is for lots of rain- this anecdotally favours the Conservatives, but not sure there is evidence of this. There is a lot of guff about turnout, and we really get a datapoint once every 4 or give years, so who knows?)
  • Don’t knows- most polls exclude don’t knows. I think there is no reason to guess that don’t knows will vote one way or another. We have no evidence either way, and I see no clear pattern in the polls I have looked at. My guess is more “Don’t Know’s” might be torn between remain parties, but difficult to know.
  • Demographics. Looking at the polls (and this applies both to online and phone polls), we can get a great deal of detail about how people said they voted in the past compared with how they will vote in the future. So for example, this survey by comres surveyed 5014 people, of which 2289 said they voted Leave, and 2248 voted Remain.
    They have then weighted the leave voters up to the referendum result (52-48). I think this is wrong- at the very least, demographics mean that many of the older electorate have frankly died in the 3.5 years since the referendum, and I do not think the polling companies are weighting correctly. This pattern is similar in other polls I have checked I find it suspicious that both telephone and online polls have weighted in favour of the conservatives, and I think there could be some overweighting here- about 1-2% against the Conservatives.
    I also think that demographic change may be a large factor. The last UK Census took place in 2011- and I wonder how much these projections have been updated in the 8 years since. The effect of this is more difficult to see.

Prediction

I therefore make my GB prediction as follows:

Conservative 41%
Labour 35%
Lib Dem 12%.

With some tactical voting, I predict that the GB seat counts will be

Con 319 Lab 251 LD 15 Nat 45 Green 1 Speaker 1

(NI has 18 seats, not listed)

This would be right on the cusp of a hung parliament.

Good luck everyone, and don’t forget to vote!

Although I’ve been an R user for some time, and have taught a variety of courses in R for statistics, I’ve never been a great user of the data science elements of R; I had a little spare time over the summer and have been trying to catch up with the tidyverse, mostly by starting with Hadley Wickham’s excellent book, R for Data Science

Whilst I’m not sure I’ll ever be a data scientist, I find the power of this quite amazing, especially compared to how I used to teach graphing in R. It does take a little more time, but filtering large data sets in R, and graphing becomes a breeze.

I’ve been working for some time on a statistical model for test cricket, which seems quite promising. I’ve used the yorkr package , modified a little for test cricket, in order to download every ball of test cricket from the excellent cricsheet website. There’s some 415 published test matches, and after some data issues I’ve so far successfully converted 399 of them.

Anyway, to demonstrate how easy it is to get interesting results using the tidyverse, here’s some data on the number of runs scored and overs faced for each test wicket.

 

Continue reading

I do a lot of teaching in various forms, and I am constantly recommending resources to students. Here is a collection of some of the most frequently useful resources.

Business Statistics/ MBA/ Statistics for Economics

There are many first courses in Business statistics at Undergraduate level that spend a lot of time talking about samples for market research, inference from samples, hypothesis testing, using the normal distribution, t distribution, chi-squared distribution. Many students haven’t done A-level statistics, and find this difficult. I find these books useful:

 

 

0. For an introduction to the mathematics of what is needed, this book is very detailed; it even explains, for example, that 5t means “5 multiplied by t”, so doesn’t assume knowledge that university students might have not covered, or not remembered from school. For those struggling with mathematics rather than statistics, this is an ideal book.

 

 

1. The Schaums Outlines Series is very good indeed. It provides a very quick overview of the problem, and then lots of worked examples, and then exercises with solutions. The only way to succeed at Mathematics and Statistics is to practice, and this book gives lots of opportunity to practice. I’ve included a link to a latest edition on amazon, but these books can be picked up on the internet for just a few pounds.

There are two different versions, one aimed for a straight statistics course, and the second aimed particularly at business students. They both share the same kind of material, and depends on how applied your course is. There are other inexpensive books in the series.

 

2. Much of the stuff introduced on Business/MBA courses, is actually A Level (High School) statistics in disguise! In the UK, many bright students don’t study statistics even at A Level, which is a shame, so often students come across is as part of a Business/Economics course at undergraduate or even Masters level for the first time. There are a lot of free resources online which are good, but as a textbook I recommend A Concise Course in Advanced Level Statistics with worked examples. There are various older editions of this book with fewer examples, but as statistics taught at school hasn’t changed in 30 years, you can probably safely pick up an old edition for a few pounds.

 

 

If anyone wants the pleasure (ahem) of learning how to do statistics with R on a short course, or knows someone that does, there’s a course in April with a really excellent tutor. Cannot speak highly enough of him.

Computing and Modelling with R

10th-12th April 2018

The course is split into three days; participants can attend one day or more. All days will consist of interactive workshops, together with  time for guided computational practice on the material, supported by the lecturer and additional experts on the R language. Lunch will be provided on each day. Computers are provided, or participants can use their own laptop.

Day 1 is suitable for people with no experience of R, and will be an introduction to programming in R. There is little mathematical statistical knowledge assumed, and will be an introduction to the programming language.

Day 2 will be suitable for those that have attended Day 1, or who have some previous experience in  R. It will give an overview of statistical modelling in R.

Day 3 will focus on more advanced techniques for programming in R. It will focus on methods for visualisation in data science, with applications driven from Biological applications, and assumes some programming knowledge in R, such as that from Day 1 of the course.

More details here

“I have two children, one of whom is a boy born on a Tuesday. What is the probability that my other child is a boy.” This is a great fun puzzle that you don’t need much background in  probability to understand…

I made a youtube video to demonstrate the answer- well really to play with some new software and do something  useful to do. However, the answer isn’t the only one that is reasonable…