Is the French two round electoral system better than First-past-the-post?

In a couple of days France will be taking part in the 2017 Presidential elections, after which a new president will be chosen for 5 years. The electoral process in these elections is unlike how elections work in the United Kingdom or the United States. The elections in France are done in two polls. In the first poll, voters can cast a vote for any candidate from a number of candidates. In the second poll, two weeks after the first polls, voters must choose between the two most successful candidates from the first round.

For anyone remembering the UK 2015 General Election the French election system may be appealing. In the UK, in 2015, the Conservative Party won the majority of seats in the House of Commons, but only got 36.1 percent of the popular vote. This came as a shock as many would have preferred Labour over The Conservatives even though Labour wasn’t their first choice. For instance, most Green voters (about 4% of the voters) would have backed Labour (30%).

The reason why the Conservatives got the majority of the votes is something called “First-Past-the-Post“. Loosely speaking in a First-Past-The-Post voting system whoever gets the majority of the votes wins. So for instance if the Conservatives get 45% of the votes, Liberal Democrats 20% and Labour 35%, then the Conservatives won. Plus in the UK, elections are held in every constituency (one seat for each constituency) and the results vary between constituencies. So in practice First-Past-The-Post does not mean gaining all the seats but rather it tends to amplify the success of the party which came first in the polls.

The French electoral system with two rounds promises to improve on First-Past-The-Post by avoiding the least desirable option. In principle if there is a candidate who is the least wanted choice by a majority of the people, even if he/she passes the first round, he/she will lose in the second. Anyone who has been following the French election closely will have heard this claim.

My question is: “Does the French electoral system prevent undesirable candidates in general?” The answer is no, but it does improve.

What is the limitation with First-Past-the-Post?

Let’s imagine three hypothetical candidates: L, C and R. Now have a look at the following table:

 1st choice2nd choice3rd choice
L voterLCR
C voterCLR
R voterRCL

In reality not all supporters of a candidate or party will have the same 2nd, 3rd, … choices but we will suppose so for simplicity sake.

And suppose the following polls: 45% for R, 35% for L and 20% for C. Using First-Past-the-Post R wins with only 45 percent of the vote. R did not need an absolute majority to win.

Let’s analyse what this result means in terms of voter preferences:

  • R is the 1st choice for 45% of voters
  • R is the 2nd choice for 0% of voters
  • R is the 3rd (least preferred) choice for 55% of voters.

The electoral system ended up selecting the least-preferred candidate

Now  imagine there is a second round like in France. In the second round, R will get 45% of the vote from R-voters, and L will get 35% from L-voters and 20% from C-voters. In other words, L would win the second round of the election with 55%. The winner:

  • L is the 1st choice of 35% of voters.
  • L is the 2nd choice of 20% of voters.
  • L is the 3rd choice of 45% of voters.

So a two round electoral system would not elect the least-preferred candidate. The new winner is less least preferred, but it also less the most desired. If this is the desired behaviour than we should say that “an electoral system should select the candidate that is least least preferred of all candidates” (yes I put twice least on purpose).  So does the French election system do a good job at preventing undesirable candidates? So far it seems so.

The French electoral system still has the same limitation

I will illustrate why the French electoral does not necessarily solve the problem.

My new example needs 5 candidates so bear with me. Here are the new 5 candidates: C, L, M, R and N. And here is the preference table:

 1st choice2nd choice3rd choice4th choice5th choice
C votersCLMNR
L votersLMCRN
M votersMLRCN
R votersRMLNC
N votersNRMLC

Now the polls are: N and R with 25%, M with 20% and C and L with 15%. In a two round system we would get:

  1. In the first round M, C and L are eliminated and N with R goes to the second round.
  2. N gets in the second round 25% + 15% = 40% and R gets in the second round 25% + 15% + 20% = 60%

So R wins. Let analyse that result:

  • R is the 1st choice for 25% of voters
  • R is the 2nd choice for 25% of voters
  • R is the 3rd choice for 20% of voters
  • R is the 4th choice for 15% of voters
  • R is the 5th choice for 15% of voters

Is there anyone less least desirable? What about M?

  • M is the first choice for 20% of voters.
  • M is the second choice for 15 + 25 = 40% of voters.
  • M is the third choice for 15 + 25 = 40% of voters.
  • M is the fourth choice for 0% of voters.
  • M is the fifth choice for 0% of voters.

So the French electoral system does not produce the best choices (if we are seeking the least least desirable candidates, of course).

 

 

 

 

Finally I have a professional email … with PGP

As a computer geek I should have my own email address, and by my own, I mean my own domain on it. There are many benefits:

  1. For a software engineer it looks more professional. Proves she/he has knowledges of things like DNS, MX, … . It is not as strong a proof as a diploma or certification but it helps.
  2. Because it gives me control. I can host my own email server or use someone else service e.g. Google Apps or Kolabnow.

So my new email address is michal [fancy email symbol] parusinski [like a comma but round] me.

And I have a PGP key attached to my email address: EB8B9B2C

Issues with nVidia prime on Ubuntu

I consider myself a lucky owner of a laptop with a hybrid graphic card system. More precisely in my computer there is an Intel integrated graphic card and there is an dedicated nVidia graphic card. Both cards can be used on the computer in conjunction. This system is known as nVidia Optimus, and the idea loosely speaking is that most of the time you use the integrated graphic card unless the computer is doing some intensive graphics computation. The purpose is to be able to GPU intensive stuff like gaming, video editing and so on, and still have a computer that on average has a low power consumption.

Optimus support on Linux is limited and I am a Linux user so I have trouble taking advantage of the setup. The most advanced solution on Linux currently is bumblebee that let’s you run specific applications on the dedicated graphic card while the rest runs on the integrated graphic card. I personally am using nVidia PRIME because it is better supported on Ubuntu Gnome.

PRIME works as follows you can either boot into your computer in ‘Intel’ mode or ‘nVidia’ mode. You can probably guess that in ‘Intel’ mode the dedicated card is not used at all, while in ‘nVidia’ mode the dedicated card is used for everything. However I run into a bug recently.

The setup actually works fairly well. To use PRIME I interface the system via the following commands:

  1. Check which ‘mode’ I am in:

    prime-select query

  2. Ask to be in ‘Intel’ mode at next reboot

    prime-select intel

  3. Ask to be in ‘nVidia’ mode at next reboot

    prime-select nvidia

When using the first command it will tell either ‘intel’, ‘nvidia’ or ‘unknown’. The latter is not suppose to happen, except it did happen to me quite a lot. I was always in ‘intel’ mode, PRIME would always say ‘unknown’ and I couldn’t switch to ‘nvidia’.

This bug is related to a Linux kernel update. The following version works fine: 4.8.0-28. But the newer version 4.8.0-30 runs into the problem I described above. As far as I understand a change was introduced with power management on the PCI bus. If you run into the described problem please downgrading the kernel version to see if it still happens to you.

It is possible to instantiate a static variable twice.

I discovered recently while working on a C++ project that if being a bit
sneaky one can instantiate a static variable twice. Normally C++
compilers won’t let you do as when creating the object code it will a
variable being initialized twice. But if the variable sits inside a
library that is dynamically linked this is possible.

Now it goes without saying that this may caused a lot of obscure and
difficult to debug errors. For instance if the static variable allocates
memory dynamically on the heap, having more than one static variable
being instantiate this lead to a memory leak.

I created a mini project on
Github
to
illustrate this.

Building Jolla applications from the command line

While I like the Jolla phone and the ecosystem, I am not fond of the
development tools. I guess many other Jolla developers have found the
Sailfish IDE (QT Creator) quite cumbersome, and are trying to find
alternatives. As far as I know the only viable alternative is the
command line. While the command line is not as glamour as a polished
well designed IDE (e.g. Eclipse, XCode, Visual Studio, …) there are
many benefits:

  1. Troubleshooting build problems is so much easier in command line.
    But there is a learning curve!
  2. Theoretically you can integrate the build commands into your
    favourite IDE or editor

By theoretically I mean of course I have not tried myself, but so far I
see little reason why it couldn’t be done.

There is a great Github page which offers information on how to build
from the command line, namely Amarchen’s “helloworld-pro-sailfish”
github project
website
. However
there is a minor issue with the steps given. If you execute the steps
you will find all the compilation files and the build files polluting
your project folder. Here is how to build a Jolla (Sailfish) application
outside the project folder:

  1. Start the MerSDK virtual machine:

    VBoxManage startvm MerSDK

  2. SSH into the MerSDK virtual machine:

    ssh -p 2222 -i ~/SailfishOS/vmshare/ssh/private_keys/engine/merdsk mersdk@localhost

  3. Navigate to your project’s build folder (not the one with the source
    code but where you want the build files to be located):

    cd ~/share/local/path/to/build/output/folder
    Remember the MerSDK virtual machine has a ‘share’ folder which is a
    network link to your local home folder. For instance on my machine
    my project folder is located under

    ~/Documents/Projects/harbour-quotes-project/builds/
    and in the MerSDK virtual machine the path becomes

    ~/share/Documents/Projects/harbour-quotes-projects/builds/

  4. Generate the Makefile using qmake and the Mer build tool ‘mb2’:

    mb2 -t SailfishOS-armv7hl qmake -o Makefile ../path/to/projects/project.pro
    For instance:

    mb2 -t SailfishOS-armv7hl qmake -o Makefile ../harbour-quotes/harbour-quotes.pro

  5. Build the rpm package:

    mb2 -t SailfishOS-armv7hl -s ../path/to/projects/rpm/project.spec rpm
    For instance:

    mb2 -t SailfishOS-armv7hl -s ../harbour-quotes/rpm/harbour-quotes.spec rpm

My first Jolla app – Quotes

Finally I finished working on my first Jolla application – Quotes.
Quotes is an application allowing you to browse over a collection 9000+
quotes from 100 different authors. The application does not require any
internet connections and browsing through the quotes is straightforward.
Quotes is open source and the source code can be found on
Github

Making the Jolla/Sailfish OS virtual keyboard hide when you press return

Whilst programming an app on my Jolla phone I discovered that the
virtual keyboard wouldn’t disappear when the return button is pressed.
What is even more frustrating is that I couldn’t find on Google easily
how to solve this problem.

 

It turns out there is a generic way to access input controls in QT.

Qt.inputMethod

In the case of a Jolla phone (or a Sailfish OS device) this object
controls the virtual keyboard.

The way to get the ‘Return’ key trigger the removal of the virtual
keyboard is done as follows:

Keys.onReturnPressed: { Qt.inputMethod.hide() }

How to add a dependency in a Jolla application project

When creating a Jolla application you may at some point need to add a
dependency. Doing so is not trivial and there does not seem to be much
documentation on the topic. Consequently I decided to write a short
guide on how to add a dependency in a Jolla app project. In my case I
will be showing how to add zlib, a popular library for compression.

You need to modify two files the “.yaml” file and the “.pro” file. The
first one is used to specify the runtime and build dependencies and the
second one is used to specify the compilation method.

The first step is modifying the “.yaml”. You probably have something
that looks like this (using the file from my
project
):

Name: Quotes Summary: Quotes display quotes from philosophers Version: 0.1 Release: 1 # The contents of the Group field should be one of the groups listed here: # http://gitorious.org/meego-developer-tools/spectacle/blobs/master/data/GROUPS Group: Qt/Qt URL: http://example.org/ License: LICENSE # This must be generated before uploading a package to a remote build service. # Usually this line does not need to be modified. Sources: - '%{name}-%{version}.tar.bz2' Description: | Short description of my SailfishOS Application Configure: none # The qtc5 builder inserts macros to allow QtCreator to have fine # control over qmake/make execution Builder: qtc5

# This section specifies build dependencies that are resolved using
pkgconfig.
# This is the preferred way of specifying build dependencies for your
package.
PkgConfigBR:
– sailfishapp >= 1.0.2
– Qt5Core
– Qt5Qml
– Qt5Quick

# Build dependencies without a pkgconfig setup can be listed here
# PkgBR:
# – package-needed-to-build

# Runtime dependencies which are not automatically detected
Requires:
– sailfishsilica-qt5 >= 0.10.9

# All installed files
Files:
– ‘%{_bindir}’
– ‘%{_datadir}/%{name}’
– ‘%{_datadir}/applications/%{name}.desktop’
– ‘%{_datadir}/icons/hicolor/86×86/apps/%{name}.png’

# For more information about yaml and what’s supported in Sailfish OS
# build system, please see https://wiki.merproject.org/wiki/Spectacle

There are two sections that are important: PkgConfigBR and Requires.
PkgConfigBR specifies the build dependencies and Requires specifies the
runtime dependencies. What you specify is the name of the rpm where your
dependency is. In my case the package I rely on is zlib. To do so you
add zlib to both sections. Giving you code that will look like this:

# This section specifies build dependencies that are resolved using pkgconfig. # This is the preferred way of specifying build dependencies for your package. PkgConfigBR: - sailfishapp >= 1.0.2 - Qt5Core - Qt5Qml - Qt5Quick - zlib

# Build dependencies without a pkgconfig setup can be listed here
# PkgBR:
# – package-needed-to-build

# Runtime dependencies which are not automatically detected
Requires:
– sailfishsilica-qt5 >= 0.10.9
– zlib

You don’t need to specify the development packages (zlib-devel in my
case) because this is handled automatically by spectacle and pkg-config
(tools used in the build process). Unfortunately modifying the “.yaml”
file is not enough. If you try to build now you will get an error:

Missing linker
flag

This error informs that linking did not succeed and this is because the
build process did not use the correct linker flags. To solve this one
needs to modify the “.pro” file that defines the QT Creator project.
This file normally look something like this:

# NOTICE: # # Application name defined in TARGET has a corresponding QML filename. # If name defined in TARGET is changed, the following needs to be done # to match new name: # - corresponding QML filename must be changed # - desktop icon filename must be changed # - desktop filename must be changed # - icon definition filename in desktop file must be changed # - translation filenames have to be changed

# The name of your application
TARGET = Quotes

CONFIG += sailfishapp

SOURCES += src/Quotes.cpp \
src/quotecontroller.cpp \
src/quote.cpp \
src/quotedb.cpp \
src/quotemodel.cpp \
src/idregistry.cpp

OTHER_FILES += qml/Quotes.qml \
qml/cover/CoverPage.qml \
qml/content/images/*.png \
rpm/Quotes.changes.in \
rpm/Quotes.spec \
rpm/Quotes.yaml \
translations/*.ts \
qml/content/quotes_en.json \
Quotes.desktop \
Quotes.png \
qml/pages/AboutPage.qml \
qml/pages/QuotePage.qml \
qml/pages/SearchPage.qml

# to disable building translations every time, comment out the
# following CONFIG line
CONFIG += sailfishapp_i18n
TRANSLATIONS += translations/Quotes-de.ts

HEADERS += \
src/quotecontroller.h \
src/quote.h \
src/quotedb.h \
src/quotemodel.h \
src/idregistry.h

To get it to add the wanted library you need to add the line:

PKGCONFIG += zlib

This will tell QT Creator to pull the linker flags using pkg-config to
match the requirements.

For more information on the build process please visit this excellent
although a bit outdated guide at
flyingsheep.

[feather_share show=”google_plus, twitter, Facebook, reddit”
hide=”pinterest, linkedin, tumblr, mail”]

Downloading quotes from wikiquote.org

In my pursuit of making a great mobile application to show quotes from
various author, philosophers and thinkers I created a python tool to
download in bulk quotes from
wikiquote.org. The tool is
simply called “wikiquotes-downloader”. “wikiquotes-downloader” is aimed
at downloading all quotes from a certain individual from Wikiquote’s
website. For instance if you ask for quotes from “Friedrich Nietzsche”,
the tool will visit “Friedrich Nietzsche” Wikiquote
page
and will create
a JSON object containing quotes from “Friedrich Nietzsche”. In the
process “wikiquotes-downloader” will remove any quote that is not from
the individual in question (e.g. misattributed quotes will be removed).
The tool is also capable of language detection and will only grab the
quotes in English. Furthermore the result is clean from any wikitext or
html metadata. “wikiquotes-downloader” is hosted on
Github.

To use the tool clone the github repository using the following
command:
git clone https://github.com/mparusinski/wikiquotes-downloader.git

Run the following command
python DownloadQuotes.py --author

For instance to download quotes from “Friedrich Nietzsche”:
python DownloadQuotes.py --author "Friedrich Nietzsche"

Quick guide to using encrypting your external hard drive.

Let’s face, if anyone steal your USB stick, external hard drive or even
your laptop what will prevent from accessing your data? Have you
encrypted them? They would have access to all your financial data,
photos, your private communications, …

There are legimitate secrets and your should protect them.

What will I teach you?

I will explain how to create your own private USB stick. You and you
alone will be able to read the data. The data on the USB stick will be
encrypted with a password of your choosing and only that password will
permit decryption. The downside is that you need a piece of software
that handles the encryption.

To illustrate what happens to the data I invite you to take a look at
the following images from Wikipedia’s article on ‘Block cipher mode of
operation’

(a technical term you don’t have to understand to use encryption).

When an USB stick is not encrypted, to data looks like on the left: easy
to read. However encryption actively hides the data on the stick and
makes it look like noise to experts with forensic tools.

Why Truecrypt?

I choose Truecrypt for three reasons

  1. It is open-source and anyone can audit it. This means security
    experts can take a look at how the software works and spot bugs,
    and/or malicious code.
  2. It strongly adheres to industry standards such as AES. Truecrypt
    doesn’t invent their own unreviewed technology but uses technology
    that is constantly analysed and reviewed for security.
  3. It is cross platform. It works on Windows, Mac OS X and Linux
    distributions such as Ubuntu. Should you change your computer you
    will be able to run Truecrypt and access your encrypted data.

You might ask: If the code is public and the standards it uses as well,
doesn’t it make Truecrypt less secure?
Actually the security resides in
a secret password you only know. The algorithms should be known because
they need to be trusted. How do you someone did not insert some malware?
How do you there are no easy ways to circumvent the encryption? To do
this there needs to be a way to check the code and the encryption
standards, that is why it is better for them to be open. There are
probably thousands of academics, security researchers, and other IT
geeks world wide who checks the securit of Truecrypt.

Let’s encrypt our USB stick

Prerequesites

Of course none of this would not make sense if you do not have a spare
USB stick. You can use an already existing USB stick but you will have
to backup all its data to your computer. You also need a conventional
computer with USB ports running Windows, Mac OS X or Linux (such as
Ubuntu) to use Truecrypt. You will need Admin rights on the computer.

Installing Truecrypt

Go to the following webpage: http://www.truecrypt.org/downloads

Click on the appropriate download link for your operating system. The
PGP signature is an additional file you can download to verify the
installer has not been tampered with between your computer and the
server. I won’t cover it here but I will provide a link for more
information
(link on more
information).

Backing data on USB
stick

Because encrypting the data on your USB stick will destroy all the data
on it, you want a backup. Backing up is not hard, you need to simply
make a copy of the files on the USB stick on your computer or anywhere
other storage device that is not your USB stick.

Insert your USB stick into your computer.

Open the drive and copy all of its content on your computer.

Remember where you have put your backed up data.

Encrypting USB stick

Start Truecrypt – Use the start menu on Windows, use Launchpad on Mac OS
X, use the dash on Ubuntu – You should be more or less greated with an
application that looks like this. (How it looks exactly depends on your
operating system)

Truecrypt

Press on button ‘Create Volume’, a new window will pop up called
‘TrueCrypt Volume Creation Wizard’.

CreateWithinPartition

Choose ‘Create a volume within a partition/drive’ and the press on
‘Next’. This tells TrueCrypt that you will encrypt your data on a
physical device .

StandardVolume

Choose ‘Standard TrueCrypt volume’ and press ‘Next’. (The other option
is quite advanced and requires some experience with TrueCrypt).

VolumeLocation

You are now greeted with a  ‘Volume Location’ chooser window. Press on
‘Select Device’. You will be presented with a list of devices in a new
window. This is the trickiest part as you need to find which devices to
select.

List of
devices

To find which device is your USB stick go find the one that satisfies
the following conditions:

  1. It should have the correct name – On Mac OS X it may look like
    /Volumes/\, on Linux /media/\, …
  2. Look at the sizes of the devices, it should be small (dozen of GB)
    and match the size of the USB stick

Select the correct device and click OK. If you can’t find your device,
make sure your USB stick is plugged in. You will go back to the previous
screen. Press ‘Next’, you will be greeted with a Warning, read it
carefully. When you encrypt your device all the data on it will be lost.
Since you have done a backup you should be OK, so press ‘Yes’.

Type in your administrator password, and you will be greeted with yet
another Warning. Think hard and if you are sure you want to have an
encrypted device and have a backup like I told you to do, press ‘Yes’.

EncryptionOptions

A window will show up giving you a list of option for encryption. AES is
the ‘Advanced Encryption Standard’, and is an industry standard
encryption algorithm. You don’t have to do anything as AES is usually
the fastest and the most well-tested. The Hash Algorithms relates to how
the encryption password will be used. Leave the default. Press ‘Next’.

PasswordDialog

Now you need to choose a good encryption password. Don’t bother with
keyfiles because you might lose them. Read the instructions regarding
the password. Additionally here are my recommendations:

  1. Don’t use a password you use somewhere else like your email. For
    instance if you use your email provider password, your email
    provider will be able to access your data.
  2. Don’t share your password, don’t store it in an email. If you have
    to write it down, write it down on some paper no one will be able to
    access. But I recommend you memorize it. Practice a few times on a
    text editor.
  3. If you don’t like special characters, you can follow the
    recommendations from http://correcthorsebatterystaple.net/ and
    https://xkcd.com/936/. I prefer to use many random words as it is
    easy to write on non conventional keyboards. But you need to use at
    least 5 completely random words.

Type your chosen password and press ‘Next’. Leave the default options,
i.e. ‘I will not store files larger than 4 GB on the volme’. Choosing
otherwise might prevent you from reading your USB stick on other
operating systems than your own (like Mac OS X, Ubuntu, …). Press
‘Next’.

FormatOptions

Leave filesystem type ‘FAT’. ‘FAT’ is readable on Mac OS X, Windows and
Linux (like Ubuntu). FAT is unrelated to encryption. First your data is
encrypted then formatted under ‘FAT’, so choosing a Linux only or Mac OS
X only filesystem serves little purpose and provides no security. The
security lies in the encryption, not OS support. Press ‘Next’.

Now for about a minute, press on a few random keys on your keyboard and
move the mouse around but leave focus on the TrueCrypt window. This
protects against some attacks against encryption by creating random data
from your action. Don’t do it for too long as it quickly becomes
pointless. Don’t enable ‘Quick Format’ and press ‘Format’. Once again
you will be greeted by a Warning, press ‘Yes’ if you have done a backup.

Encryption

It is encrypting, so wait for a while. All the bits on the USB sticks
will be encrypted, for this reason it takes time (encrypting everything
even unused data enhances security). Actual usage of the encryption /
decryption will be much faster.

You are done, don’t encrypt another drive, just press cancel.

How to access your encrypted USB stick?

To access your data, you need to use Truecrypt to ‘mount’ the encrypted
drive on your computer. ‘Mounting’ means creating a link between the
computer operating systems and the encrypted contents on the hard drive.

Run Truecrypt, you get back to the original screen. This time press on
‘Select Device’.

Select the appropriate device.

Press on ‘Mount’.

Enter your encryption password and press ‘OK’, and enter your computer
administrator password.

Tada it is mounted, a new drive should be accessible in your computer.
On Windows you should be able to acces it through ‘My Computer’, on Mac
OS X through Finder, and on Linux (e.g. Ubuntu) whatever is your file
manager.

Copy over the data your have backup. Once you are done with accessing
your encrypted USB stick, your need to unmount first. To do you need to
go back to Truecrypt, select the appropriate slot and press ‘Dismount’.
You don’t need to select any device through the ‘Select Device’ button.

For more information

Here are some useful links to get more information about Truecrypt:

  1. For Windows:
    http://www.howtogeek.com/108501/the-how-to-geek-guide-to-getting-started-with-truecrypt/
  2. For Mac OS X:
    http://www.howtogeek.com/howto/20150/getting-started-with-truecrypt-on-mac-os-x/
  3. For Ubuntu: https://help.ubuntu.com/community/TrueCrypt