A Summary of Free To Choose – Milton Friedman

Introduction

Adam Smith in the Wealth of Nations:

An inidividual who intends only his own gain is led by an invisible hand…by pursuing his own interest he frequently promotes that of society more effectually than when he really intends to promote it. I have never known much good done by those who affected to trade for the public good

Thomas Jefferson in the Declaration of Indendence:

We hold these truths to be self-evident , that all men are created equal, that they are endowed by their Creator with certain unalienable Rights; that among these are Life, Liberty and the Pursuit of Happieness.

Smith and Jefferson alike had seen concentrated government power as a great danger to the ordinary man. They saw government’s role as an umpire.

A wise and frugal government, which shall restrain men from injuring one another, which shall leave them otherwise free to regulate their own pursuits of industry and improvement

The combination of economic and political power in the same hands is a sure recipe for tyranny.

As always, people took the favourable developments for granted.

Academia blaming the great depression on a failure of free market capitalism: Emphasis on the responsibility of the individual for his own fate was replaced by emphasis on the inidividual as a pawn buffeted by forces beyond his control.

The invisible hand: An inidivual who intends only to serve the public interest by fostering government intervention is led by an invisible hand to promote private interests

A complex, organized, smoothly running system can develop and flourish without central direction.

We must rely on inidividual initiative and voluntary cooperation.

The Power of the Market

A command method of coordinating activities can be the principal method of organization only in a very small group. Even in a command method, commands must be supplemented by voluntary cooperation.

Just as no society operates entirely on the command priciple, so none operates entirely through voluntary cooperation.

The Role of Prices

If an exchange between 2 parties is voluntary, it will not take place unless both believe they will benefit from it.

The price system is a mechanism that performs its task without any central direction. Supplies allocated by command in a country as opposed to naturally guided by the pressures of demand reflected in prices will lead to surpluses in some areas and shortages in others.

Transmission of Information

To induce suppliers to produce more of a good, they will have to offer higher prices for the good.This induces suppliers to increase their work force, to grow their work force they will have to offer higher wages.

The price system transmits only the important information and only to the people who need to know.

The market prices reflect both the current price and the price offered for future delivery.

Private monopolies on a commodity or by cartels does not prevent tranmission of information via the price it does however distort the informatino transmitted.

Incentives

The effective tranmission of accurate information is wasted unless the relevant people have an incentive to act and act correctly to that information.

One of the beauties of a free price system is that the prices that bring the information also provide both an incentive to react to the information and the means to do so.

A producer will produce output up to a point where producing a little more would add as much to his costs as to his receipts. A higher price shifts this margin.

For a wood producer, the more he produces the higher the cost of producing more. He must resort to wood in less accessible or favourable locations and must hire less skilled workers or pay higher wages to attract skilled workers from other pursuits.

Interference by government, through minimum wages, or trade unions by resticting entry distor the information transmitted and prevent individuals from freely acting on that information.

You have a choice of how to apply your labour. The decision depends on your interests and capacites.

Statisfaction in a job may compensate for low wages. On the other hand, higher wages may compensate for a disagreeable job.

As an entrepreneur, the major productive resource he owns is the capacity to organize an enterprise, coordinate the resources it uses and assume risks.

The corporation is an intermediary between its owners – the stockholders – and the resources other than the stockholders’ capital, the services of which it purchases. Only people have incomes and they derive them through the market from resources they own.

In the US the major productive resource is personal productive capacity: “human capital”.

Without the maintenance of inherited capital the gains made by one generation would be dissipated by the next. But the accumulation of human capital in the form of increased knowledge and skills and improved health and longevity has also played a role.

 

Wrangling with Django’s DecimalField and representations

I’ve been building an application that needs precicion as it is dealing with money, finance and trading. For these tasks there is a special datatype called decimal.Decimal that python has built in. It has many advantages but has a few disadvantages when displaying the decimal to the user.

  1. The trailing zeroes are shown (which is unnatural to the normal person)
  2. Sometimes python decides to show the simplist form in scientific notation

let’s get into it…

Python’s Decimal

Working with Decimal is quite nice. They are intialised with string representations of numbers and can be cast to str:


In [1]: from decimal import Decimal

In [2]: precise_number = Decimal('54.899721')

In [3]: precise_number
Out[3]: Decimal('54.899721')

In [4]: str(precise_number)
Out[4]: '54.899721'

Which all seems normal but maybe this won’t:


In [5]: zeroed_num = Decimal('54.000000')

In [6]: zeroed_num
Out[6]: Decimal('54.000000')

In [7]: str(zeroed_num)
Out[7]: '54.000000'

More on this later…

Django’s Decimal Field

Django’s Decimal Field (model field) is slightly different as now we are integrating the saving of this python decimal in a database. That is why we need to set max_digits and decimal_places. So you need to think about the maximum precision expected and maximum total digits in a decimal stored you will be working with in your system. No need to go overboard and from my experience this field can be modified to go bigger.


planned_quantity = models.DecimalField(
        max_digits=19, decimal_places=10,
        blank=True, null=True
    )

So with the above example I can store 999,999,999.1010101010 but nothing more precise or with more digits.

Everything feels great.

Trailing Zeroes

Now after creating a record and now returning to edit the record the form will output the number including the trailing zeroes:

decimal-field-trailing-zeroes

Which just feels wrong. It should just show 3 like a normal human would expect. We saw this a bit earlier and I did a bit of digging. After searching django remove trailing zeroes the result mentioned normalize() which is a function of Decimal:


In [7]: str(zeroed_num)
Out[7]: '54.000000'

In [8]: str(zeroed_num.normalize())
Out[8]: '54'

In [9]: zeroed_num.normalize()
Out[9]: Decimal('54')

As mentioned in the normalize docs it doesn’t just strip the trailing zeroes it also converts 0 to scientific notation.

So it will exhibit this behaviour:


In [11]: damn_decimal = Decimal('600')

In [12]: damn_decimal
Out[12]: Decimal('600')

In [13]: damn_decimal.normalize()
Out[13]: Decimal('6E+2')

In [14]: str(damn_decimal.normalize())
Out[14]: '6E+2'

So normalize is not what we want.

Scientific Notation

So as we alluded to previously sometimes things will just look wierd to the non-scientific on our website:

django-scientific-error-message

This error message is created by the validator:


planned_quantity = models.DecimalField(
        max_digits=19, decimal_places=10,
        blank=True, null=True,
        validators=[MinValueValidator(Decimal('0.0000000001'))]
    )

So things are looking a bit tricky and difficult to work with. Even more so when you bring in validation messages and positive decimal validation.

For now though let us just try and display decimals nicely to the user

The Fix

So we know that normalize is not going to cut it. We’ll need a bit more and that comes from format.

So the intial data of the form will need to be modified for the user and to do that when loading the form do:


    for key in form.initial.keys():
        if isinstance(form.initial[key], decimal.Decimal):
            form.initial[key] = '{0:f}'.format(form.initial[key].normalize())

The important thing here is getting all our decimal fields and then normalizing them.

Then displaying the number in a fixed-point float format with: x = '{:f}'.format(my_dec.normalize())

So that is it. There is a better way of doing this I think and that would be modifying the model and I’m looking at that now.

 

Running multiple django sites on a single server with gunicorn and nginx

When days are dark, friends are few, cash is low and we need to take our django sites and put them on a single server.

I am assuming you have site up and running that is using django with nginx and gunicorn. Now we need to do a few modifications to get the new site working.

First things first get the application code on the site, create your database, create your virtual environment, install your requirements, set any environment variables, migrate, collect static and create your super user. It is that easy (any issues see the guide above)

Creating 2 Gunicorn Services

We setup the nginx config using unix sockets instead of a local http port. So we used:


proxy_pass http://unix:/var/www/myfolder/mysocket.sock;

Instead of:


proxy_pass http://127.0.0.1:8000;

We then created a systemd service to start and stop guncorn for our site called /etc/systemd/system/gunicorn.service

What we need to do now is create a service for each site

Lets rename /etc/systemd/system/gunicorn.service to /etc/systemd/system/site1.gunicorn.service

Make a copy of this site and make relevant changes for the new folder location and socket etc. For a refresher of how this looks:


[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=
Group=www-data
WorkingDirectory=/var/www/
ExecStart=/var/www//env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www//.sock config.wsgi:application
EnvironmentFile=/var/www//.gunicorn_env
[Install]
WantedBy=multi-user.target

Creating another Nginx server block Config

Make a copy of your existing server config and the new config will look similar to the below, remember to set different log files now as we are hosting multiple sites on a single server.


server {
    listen 80;
    server_name site1.co.za www.site1.co.za;
    error_log /var/log/nginx/site1.error.log
    access_log /var/log/nginx/site1.access.log

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }   

    location /static/ {
        alias /var/www/site1/site1/staticfiles/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/site1/site1.sock;
    }
}

Now to test if things are working:

  1. Reload the daemon with sudo systemctl daemon-reload
  2. Check the status of your new services sudo systemctl status site1.gunicorn.service
  3. Start the services: sudo systemctl start site1.gunicorn.service

Remember to ensure your .guncorn_env file is in the project folder

Now we need to enable the nginx config:


sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/

Check you don’t have any types with: nginx -t

Then reload the service: sudo service nginx reload

Now everything should be working correctly.

I would like to thank Khophi for his recent post that pretty much guided me on the things to do to make this work in his post on running multiple django projects behind an nginx proxy