Search This Blog

Thursday, November 24, 2022

Python Weather app - current conditions from the national weather service

 So, I was reading through a course on XML from W3 schools and in there, it mentioned that current conditions are available as an XML file for various locations.  So I investigated it.  After a bit, I thought I should make a Python program to pull the information from the National Weather Service in XML format and then display this information to the user.

It took some time to refamiliarize myself on the use of the urllib functions in Python to make an http request and download the text of the response.  Then, I needed to learn how to use the XML functions to parse the XML to retrieve the values for each of the tags.  My solution is quite fragile in that any changes to the file format by the National Weather Service will break the code.  Nonetheless, I was quite pleased with myself when I had finally got this working!  So I thought I would share it with the world.

While I don't think that this is going to revolutionize anything, maybe you can re-use some of the code.  Probably the list of the different Oregon locations is the most useful thing.

Here's what the user sees after hitting Fetch:


Wednesday, November 23, 2022

Shopping list and recipe app development plan

 So this is going to be the first of a series of posts that are going to follow along developing a desktop application (mostly cause I don't have an IDE in which I can create a mobile app) that will store recipes, allow the user to make meal plans, and then based on those meal plans create shopping lists for all the ingredients.

The overall plan is going to include:'

  • Defining the requirements that the software has to fulfill from a user perspective.  This would be the functionality that the user expects the software to perform.
  • Defining the technical approach that is going to be used to fulfill the user's requirements defined in the previous bullet.
  • Create a test script to be used to ensure that the software is functioning adequately.  Each of the tests will check that one or more of the requirements have been fulfilled.
  • Perform the testing and then do any required remediation to correct any discovered test failures.
  • At this point, I may end up discussing what additional or changed user requirements could be used for the next version of the software.  But let's not get ahead of myself.
So what are the user requirements?  While I'd like to use a table here, I'd have to create it by writing raw HTML as the blog's UI isn't accommodating of tables so you will have to bear with me as I present it as a numbered list.  While the numbered list will not allow me to, within the list, add a prefix, for any cross-references, pretend that Recipe requirements are prefixed with an R, Ingredient requirements are prefixed with an I, Meal Plan requirements are prefixed with an M, Shopping list requirements are prefixed with an S, and general requirements with a G.

Recipe module

  1. The user will enter for each recipe:
    1. A title
    2. A category for the recipe
    3. The number of servings the recipe makes
    4. One or more ingredients
    5. One or more cooking steps
  2. Recipe categories can be created, edited, and deleted.
  3. Recipe categories will be pre-populated with:
    1. Entrée
    2. Side
    3. Appetizer
    4. Dessert
    5. Snack
  4. Do not allow Recipe Category to be deleted if it is used in an existing dish.  Advise the user of at least one recipe title that they need to address if they try to delete in this case.
  5. For each ingredient, the user will specify:
    1. Quantity to be used
    2. Unit of Measurement of consumption quantity
  6. For each cooking step, the user will specify:
    1. Step number
    2. Text of the step instructions
  7. Recipes can be exported.
  8. Recipes can be imported.

Ingredient Module

  1. For each ingredient, the user will enter:
    1. Ingredient name
    2. Purchase Quantity
    3. Purchase Unit of measurement
    4. Zero or more unit of measurement conversions
  2. Ingredients can be exported.
  3. Ingredients can be imported.
  4. Ingredients can be created, edited, and deleted.
  5. Ingredients cannot be deleted if they are used in a Recipe.

Meal Plan Module

  1. The user can populate the meal plan module with a meal record consisting of:
    1. A Recipe
    2. The number of people dining
    3. The date of the planned meal
  2. An export can be created for a date range showing all the meals planned within that date range.  The export will be a table modeled to simulate a calendar.  Also a multi-level list format will be available.
  3. Meal records can be created, edited, and deleted.

Shopping list Module

  1. The user can create a shopping list for a user-selected date range.  This will produce a shopping list for all the ingredients that will be consumed in the preparation of all the meals in the selected date range for the indicated number of diners and indicate to the next whole purchase Unit of measurement.

General requirements

  1. The user can create different databases.
  2. The user can open a specific database.
  3. There are no security requirements.
In writing this, I've had some thoughts already about other behaviours I'd like the program to have, but I think this is already enough to get on with.  The next post, hopefully within the coming week will discuss the technical approach I will take to providing the functionality defined above.

Friday, May 6, 2022

Articles of the Communal Charter of Amiens

I ran across this page.  Although I suspect I would disagree with those behind the website, it seems to be a well curated collection of texts and for the efforts made to make texts available for free to the public, I am ok with it.  In any case, they have included the articles of the communal charter of Amiens as a document but lamentably, only include a Latin version complaining that an English translation is not available.  As such, I have taken the time to use Google translate as well as a bit of clean up to come up with the following text.  However, it is clear that the translation from Latin is a bit clunky at best and the resulting text is hard to understand.  Had I a better understanding of the history and context of this document, I might be able to sort out questions like whether the resultant "commons" and "common" from Google translate meant "commoner", "commune" or "community" or if indeed there was a commons (either a pasture or city square) that is being referenced within this charter.

In the name of the holy and undivided Trinity. Amen. Philip, king of the Franks, by the grace of God, since our friends and faithful fellow citizens of Ambianenses have often faithfully exhibited to us their service, we have given them a common observance of their love and fidelity to us, at their request; an oath.

1. Each one, under his oath, will justly watch for his faith, assistance, and counsel in all things.

2. Whoever commits theft will be caught within the boundaries of the community, or will be known to have committed it, he will be handed over to our provost, and whatever will be done about him will be judged and done by the judge of communion. To the protestant, our provost shall restore what was stolen, if it can be found; the rest will be converted into our uses.

3. Let no one presume to disturb any one residing among the common stock itself, or with merchants coming to the city with their wares, to disturb the peace of the city below the Banleucam (1). And if any one does this, let him do it in common with him, that he may do justice to the violator of the common law, if he can apprehend him, or anything of his own, to do justice.

4. If anyone has taken away his property from communion with someone under his oath, then, having been summoned by our provost, he will pursue justice; but if the provost of justice should have failed, having been summoned by a major or a [leader], he will come into the presence of communion, and will do there, as far as the [leader] shall judge from it, without prejudice to our right.

5. But he who is least of all from communion, has taken away his property from any one of the commons, and has denied that he will execute justice within Banleucam, after he has made it known to the men of the fort, where he remains; our provost will retain them until he has our own and our generals in like manner.

6. If he strikes with a fist or palm any one of the commons, apart from the customary agitator or lecturer, unless he has acted in self-defense, he will be able to prove it before our provost, before striking two or three witnesses, to give twenty shillings, fifteen shillings for the general justice and five for the justice of the lords.

7. But whoever wounds his oath with arms, unless he will be able to prove himself by the lawful testimony and assertion of the oath by defending himself against the wounded, shall lose his fist, or shall pay nine pounds, six to the firmness of the city and common, and three to the justice of the lords, shall pay the fist as ransom; or if he will not be able to pay, he will deliver the fist in the mercy of the common people, saving the principal of the masters.

8. But if the proud man is so wounded, that he does not wish to receive an amendment at the discretion of the provost and mayor and of a postage, or to provide security, his house, if he has a house, shall be destroyed, and his chattels shall be taken; if he does not have a house, his body will be taken until he has received either an amendment or a security.

9. But he who, by no means existing in communion, has smitten or wound any one of the commons, unless he wishes to execute justice before our provost by the judgment of the common, and, if he may be able, throw to the commons his house, and they will be our capital. And if he will be able to arrest him, he will be taken revenge on him by the district provost before the mayor and scabbards, and our chattels will be.

10. If any man has violated his oath with indecent and disgraceful reproaches, and if two or three have heard him, we have determined to be convicted by them, and shall give five shillings, two to one who is abused, and three to the common.

11. Who would say that it would be dishonorable for anyone in the audience of some common people, if it was made public, and that he would not be willing to defend himself by the judgment of common judges, if he were able to throw to the commons his house, and to stay in common until he amended it? he will suffer, and if he will not make amends, his chattels will be in the hand of the lord king and common.

12. If any one who has taken an oath to his oath, or has lied in good faith, shall be approved before the provost and mayor, he shall be punished by a common judgment.

13. If any man knowingly buys or sells spoils of the common stock, if it shall be proved, he shall lose the spoil, and shall restore it to the spoils, unless any thing be committed against the lords of the common, or from the plunderers themselves.

14. He who cannot prosecute justice after making a claim against his adversary through the provost and major and judges of the common law, if he afterwards shall do anything against him, it will be reasonably common to him, and, having heard of it, he shall judge by reason of what ought to be done afterwards.

15. He who, having been summoned by the mayors and judges and deans, to wit, those who serve the commonwealth, having been prompted to evade justice and common judgment, will knock down his house if they can; but they will not permit him to remain among them until he has satisfied them.

16. Whoever knowingly receives the common enemy in his house, and has communicated to him by selling and buying and eating and drinking, or by providing some solace, or giving counsel or assistance against the common, shall be made guilty of the common; he will be able to lay low the common stock, and the chattels of the king shall be.

17. Within the common limits, a hired champion will not be received against the common man.

18. If any man knowingly violate the constitutions of the commons without a shout, and shall be convicted of it, he shall soon lay the ground for that common house, if possible, and shall not suffer him to remain among them until he has satisfied himself.

19. It is also established that the general should not admit of lands or feudal lords.

20. He who wishes to test the general judges of the falsehood of the judgment, unless he has been able to prove it, is in the mercy of the king, and of the greatest, and of the scabbard, of all that he has.

21. The woman will not be able to sell the dowry which she holds, nor will she be able to send bail except to the nearest heir and from year to year. But if the heir is unable or unwilling to buy it, he must retain the woman for his entire life, and be able to lease it for a year.

22. If any man and his wife have infants, and it happens that their infants die, which of them survives, whether man or woman, whatever they possessed or chosen in like manner. the lieutenant was then made. But if before they have come together, or the husband or wife have had children, after the death of their father or mother, the inheritance of their children will be returned to them, unless there is a fee.

23. If the wife survives after her husband's death, and her children remain alive, the woman will not answer for all the possessions which her husband had in peace, so long as the children will be in custody until she has an attorney, unless he has bail, he will not answer.

24. If any one should seek money from any widow, she will defend herself by oath against one witness, not against many, and will remain in peace; but if she shall seek from her any possession of her as a bail, she will defend herself in war.

25. If any man buy the land or any inheritance from any one, and before it has been purchased, it will be offered to the nearest heir, and the heir will not buy it, he will never again answer to that heir in cause of it. But if it is not offered to the nearest heir, and the one who buys it, the heir seeing and knowing, will keep it in peace for a year, never will he answer it any more of it.

26. If any one has held any possession of his property in peace for seven years, he shall never answer it again concerning it with the present adversary.

27. If any foreign merchant sells anything, and at the same hour could not have his money, he shall first make a noise to the owner of the purchaser, or to the provost of the lord; whatever day it may be.

28. Whoever makes a cry of promise will recover nothing.

29. If any major, or a scabinus, or any of the major justice, has received or inquired of a premium, and he who gives, or by whom the premium has been asked, shall call to the greater, or has a witness concerning this, the accused shall pay twenty shillings; and if he receives a premium, he shall pay it.

30. But if the accuser does not have a witness, he who will be accused by the oath will defend himself.

31. If any one shall bring a shout to the provost, and the provost will not do justice to him, the crier shall bring a louder shout, and the major shall send the provost to the account to do him justice; and if he refuses to do so, the major, without prejudice to the country's law, will do justice according to the rules of the image.

32. If a man has slain any thing that is his own, and he who is accused will answer that he has not knowingly bought it from a robber, he for whom he will be accused will lose, and will defend himself before justice by oath, if the provost or justice will ; And this garanus will do the same, if he shall say the same, both the first, and the second, and the third; but the accuser will confirm by oath what he has cried out, if he who wishes will, who will obtain justice.

33. In every case, both the prosecutor, the accused, and the witness will speak through an attorney if they wish.

34. Let no one presume to make a case outside the city concerning possessions pertaining to the city.

35. If the husband and wife acquire some possession during their lifetime, and one of them dies, the one who survives will have only half of them and the other children. If the husband dies, or the wife dies, and the children remain alive, the possessions, whether on land or in rent, and which part shall come from the dead the relatives of the parents have died, or until their infants have been imprisoned.

36. If any one provokes the king's provost, in a plea or without a plea, with disgraceful and disgraceful words, he shall be in the mercy of the provost, at the discretion of the major and the scabbard.

37. If anyone provokes a major in a plea to disgraceful and disgraceful words, his house shall be thrown down; or according to the price, the house may be redeemed in the mercy of the judges.

38. If any one strike or wound his oath, and he who is struck shall make a cry that he was struck by the old hatred, the striker shall do the right, according to the statutes of the image, for the blow; he will make the common, and he will give nine pounds, namely, six pounds common and sixty shillings for the justice of the lords, and he will pay half of the right within eight days, or the whole, if they wish. For no one will perform the oath for him who kills, whoever he is, either a man, a woman, or a boy.

39. If the mayor sits in the case with the generals and juries, and there someone strikes his oath; his house, against which several witnesses came out, who was the first to strike the blow, was thrown to the ground.

40. But he who is convicted in his case under oath shall pay twenty shillings of the common stock, there the justice of the lords will receive nothing.

41. Whoever casts his oath into the water or into a marsh, if the outcry bears one witness and has seen greater uncleanness, the criminal shall pay sixty shillings, and from these the justice of the lords shall have 20 shillings. If an unclean man has no witness against blood or uncleanness, he will defend himself by oath, and will depart free.

42 But whoever has called his oath, a disobedient servant, a traitor, a wissot (2), that is, a coup, shall pay twenty shillings.

43. If the son of a burgess has committed any forfeiture, his father will execute common justice for his son. But if he shall not be in the custody of his father, and be warned, and evade justice, he must be a stranger to the state for one year. If, however, he wishes to return in the past year, he will do the right thing according to the statutes of the image file provost and mayor.

44. If any agreement has been made before two or more scabies, there will no longer be a battleground or a duel in respect of that agreement, if the scabini, who were present at the convention, shall testify to this.

45. All those major and common laws and precepts we have mentioned above are only among the jurors. There is no horse judgment between a sworn and an uninjured.

46. It was the custom of the Amiens, that, on the feasts of the Apostles, when entering into the country of each of the four chariots through one of the four gates of the city, Guarino archdeacon of Amiens received a penny. The elder and the scabini, who existed at that time, by the advice of Theodoric, then bishop of Amiens, purchased from the archdeacon the aforesaid custom, at five shillings and four capons; and the archdeacon takes that tribute at the furnace of Firmin's Cloister, situated outside the gate of Saint Firmin in a valley.

47. On all the tenements of the town justice will be shown by our provost, three times a year in the general plea: namely, on Christmas, Easter, and Pentecost.

48. Moreover, the major and the scabini shall judge all forfeitures which shall be made within the banleugam of the state, and shall do justice to them, as they ought, in the presence of our bailiff, if he will be present there; but if he will not be present, or will not be able to do justice for his absence, they will not cease to do justice, but will do justice to him, with the exception of many and many rape, which we retain for ourselves and our successors forever, without the other's side.

49. Our chattels of murderers, arsonists, and traitors are absolutely, without regard to the other. In the chattels of other criminals, we retain to ourselves and to our successors what we have and ought to have.

50. No one can make a ban in the country except through the king and the bishop.

51. If anyone is banned for any forfeiture, except for the many, murder, arson, treachery, rape, the king, the steward, or the king's provost, bishop or major, each of them will be able to hire him once a year in a villa.

52. We will also, and we will perpetuate the commonwealth, and grant that it may be lawful for us, and our successors, to send the city of Ambiaan or the common stock without our hand, but that it always clings to the crown royally.

To ensure that all these things remain permanently ratified and stable, we confirm the present page of our seal with the authority and stylus of the royal name annotated below, without prejudice to the right of bishops and churches and nobles of the country and of foreign law. Act of Lorriacus in the year of the incarnation of the Word in the twelfth year of our reign. Those standing in our palace whose names and signs have been placed: S. Count Theobald, our waiter; St. Guy, buticular St. Matthew, chamberlain S. Ralph, constable. Chancery vacancy date.

(1) This appears to be a reference to a geographic place within Amiens.  "banlieu" is French for suburb and thus it may be intended to refer to the surrounding areas to Amiens that are still within the jurisdiction.

(2) The original marks this as "alias wisloth" which is well, not insightful either, but from the context, it is clearly a pejorative term.


Saturday, January 2, 2021

Book Review: Rescher's Free Will

Rescher's Free Will: A Philosophical Reappraisal [0] argues for a compatibilist view on the question of free will.  He very carefully presents his argument to reconcile the internal subjective experience of deliberate free decision and action within the current objective scientific view that the universe is deterministic.  The pages span various arguments for and limitations to free will that asserts that humans are ration beings that make "choices".  But despite the quantity, depth and breadth of the arguments presented, he demurs that the book "does not lay claim to the status of a categorical proof." [1]  He alludes to the ramifications at the end with regards to ethics and how we should understand ourselves.

The sort of free will envisioned by Rescher is very limited in comparison to the oft imagined "can try anything at any time" sort of freedom. He limits this freedom to choose otherwise only "if something-or-other and [sic; had?] been different." [2]  He rejects that agents have the possibility to choose differently if it were possible to somehow re-play the choice made as showing that the agent is not rational.  However, the determinism must not by-pass the agent either, the agent's internal deliberation and resulting decision are necessarily in the stream of causality, but these have a different quality than natural phenomena.  In contrast, a hard line materialist view can be summarized as "an illusion.  The experience we have of deliberating before some important decision is a mere bit of electrical chatter that our brains generate, the effect of which is to obscure from us the fact that our decision was cast before we were even aware of it." [3]  Thus, although Rescher does not postulate some supernatural decider such as a mind, the subjective experience of deciding is nonetheless elevated above other natural phenomena.

Overall, Rescher argues ardently on the question and common objections to the compatibilist position from both advocates of determinism and free will.  These are technical in nature and for someone leaning in the general direction, I suspect, quite convincing.  Even if one leans away, there is much here worth mulling over.  Overall, it seems that there is a dichotomy between subjective experience and objective reality.  Perhaps it can be thought of as akin to Heisenberg's uncertainty principle where position and momentum of a particle cannot be known to a high degree of precision at the same time.  One can address the question from the perspective of materialistic science or by analyzing the subjective experience, but doing so from both at the same time leads to unresolvable paradoxes.

Suppose you and I are playing billiards.  The playing table, suitably situated in the pub down the road such that relativistic and quantum level effects are negligible, is deterministically defined through Newtonian mechanics.  As each player takes a turn, the shot one will take, while affected by the situation on the table, is not predetermined by that situation, but rather determined by the player's deliberation.  One can discuss the forces, momenta and positions of the balls and predict the result of the shot taken, or one can discuss the motivations and strategy of what shot should be taken, but not at the same time.  The key feature is that different rules apply depending whether you are analyzing the movement of the balls or deliberation of the thoughts.  But you cannot find a single set of rules to analyze both at the same time.  Well, it's a thought.


[0] Rescher, Nicholas. Free Will: A Philosophical Reappraisal. United Kingdom, Transaction Publishers, 2009.  Note: this version did seem to have a higher quantity of typographical errors than other books I have read.

[1] Rescher, p. 162

[2] ibid, p. 50.

[3] Crawford, Matthew B.. The World Beyond Your Head: On Becoming an Individual in an Age of Distraction. United States, Farrar, Straus and Giroux, 2015.

Wednesday, December 23, 2020

Book review: Graziano's Rethinking Consciousness

Michael Graziano's Rethinking Consciousness, A Scientific Theory of Subjective Experience [0] is a popular science book where he elaborates in a non-technical way about his attention schema theory and concludes the book discussing implications for artificial intelligence and uploading minds.  During the journey, he explains his materialistic view of what consciousness is.  His definition is that consciousness  is the ability to have a subjective experience.  He explains that through neuroscience, consciousness, or one's subjective experience, arises from the attention schema which constructs a rough simple model of the world which determines what stimuli one's attention should focus on.  This interplay between stimuli, the underlying neural network, and the attention schema creates the feeling that one has a mind or subjective experience.  Consciousness is not an illusion nor is it something non-physical such as a soul or essence, but rather, it emerges from the ingredients similarly to temperature and pressure emerge from the average speed of atoms in a gas in a container.

This first part of explaining the foundation necessary for the attention schema theory and how that theory actually works is somewhat confusing and left me with a feeling that somehow it was all begging the question...the conclusion was somehow assumed in the premises of the argument and it ended up in a big circle.  But the next part speculating on the application to artificial (and given enough technical progress, biological) intelligence is worth the slog.  He posits that there are 4 essential elements to a being that can have consciousness or a subjective experience: (1) be able to pay attention to something; (2) an attention schema that can shift the attention between different things; (3) a wide range of content (memories, data, etc.); and (4) the ability to search the content to find relevant facts on which the attention schema can make decisions to direct the attention.  Following this explanation is a survey of the state of technology (in 2019) to be able to instantiate the necessary pieces to construct an artificial intelligence per this blueprint.  In the discussion, emotional states and body awareness are mentioned as two possible additional necessary elements to arrive at consciousness as we know it.  Graziano speculates these could be added virtually for artificial intelligences, but need more consideration in the biological scenario.

This deconstruction of the necessary elements of consciousness, Graziano suggests, could provide a means to test for it.  Certainly, a computer program that was written to be an artificial intelligence could be examined to see if it in fact fulfilled the requirements.  Biological wetware is harder to examine, but morphological differences between brains would show whether the structures where we currently believe the processes take place are present.  As scanning resolution increases (cue the alien abduction visual), scanning and probing could allow verification that these essential elements are present.  Based on morphology, he asserts that only the higher mammals have consciousness.  One of the consequences of what Graziano proposes is that consciousness is algorithmic.  So, on the question of Searle's Chinese Room paradox, it seems Graziano would argue that the room (not the person within), is conscious. [1]  Thus any Universal Turing Machine can become conscious given the right algorithm and enough data.

The last portion of the book focuses on the possibility of uploading one's mind, whether the Matrix is possible, and the ethical challenges with duplicate "selfs", if such are ever constructed.  While interesting, given how far technology has to go, it remains firmly in the realm of science fiction.

My motivation for reading this book came from discussions on the topic of free will.  Graziano offers no opinion overtly.  The materialistic approach of the explanation suggests he might fall into the determinist camp although he does offer that consciousness is not necessary for decision making [2] and usually, it is argued that consciousness is necessary for free will.  And so, I will end it here and I have Rescher's Free Will [3] on my night stand, borrowed through interlibrary loans from the University of Idaho.  I am in the middle of chapter 1 and already have a lot of reactions to put into words.  (stated that way as a preview of what's to come!)


[0] Graziano, Michael S A. Rethinking Consciousness: A Scientific Theory of Subjective Experience. United States, W. W. Norton, 2019.

[1] https://plato.stanford.edu/entries/chinese-room/

[2] Graziano, p.6

[3] Rescher, Nicholas. Free Will: A Philosophical Reappraisal. United Kingdom, Transaction Publishers, 2009.

Sunday, December 6, 2020

Book Review: Free Will by Sam Harris

 Sam Harris' Free Will [0] is a thin volume (one of the jacket blurb mentions 13,000 words) that is easily digested in an afternoon.  He sets out to show that two assumptions are false: "(1) that each of us could have behaved differently than we did in the past, and (2) that we are the conscious source of most of our thoughts and actions in the present."  Both of these are approached from a neuroscience perspective.  Neuroscience has made progress in mapping conscious and unconscious mental states and processes to physical brain states and changes. [1] Some of this comes from analysis of people with brain injuries. [2]

Examining through introspection, it seems that the thoughts that occur are self-generated.  The way thoughts appear to ourselves is highly suggestive that we have free will.  However, since much of what happens in the brain is not accessible to introspection and we know that some of the heuristics used by the brain can be easily manipulated [3], the introspective conclusion is suspect at best.  For example, having sat on the beach observing carefully for ideas for next year's sandcastle building competition, one would also notice the movement of the sun over the course of the day and conclude that the sun moves around the earth.  But of course this is an illusion.  Physiologically, Harris argues, the conscious thoughts and decisions come from the unconscious which has been ultimately influenced by externalities and therefore no thought or decision can truly be created ex nihilo.

The question that arises here is whether the existence of prior influences are sufficient that thoughts and decisions are pre-determined or is there room for a person to have bechaved differently in a given situation. A die thrown with a certain velocity and spin onto a specific surface will stop with the same number on top under Newtonian mechanics.  That we may not be able to predict it because we do not know the specific values of the velocity or spin does not negate the fact that repeating the throw exactly will not alter the result.  The sources and effects of the pre-determining influences on our thoughts are vast and diverse so as to make it currently impossible to determine.  (Although, one might consider "psychohistory" envisioned in Asimov's Foundation series as a basis for a methodology. [4])

The definitive experiment would be to run time forward and backward over some point in time to see if indeed one behaved differently.  Again, under Newtonian mechanics as well as Einstein's relativistic mechanics, outcomes are pre-determined and therefore would never vary although they may be difficult to compute.  Quantum behaviour, such as where an electron will hit a screen after passing through a double slit, is not pre-determined although there are regions where the probability is zero.  For the regions with non-zero probability, there is no calculation made or process used that can narrow the result a given trial any further than the computed probability density.  For many, this has given hope that if the physiological mechanics occur at scales sufficiently small that quantum effects can result in different outcomes for the same set of inputs, then a physiological basis for free will is established.  Unfortunately this may be a pyrrhic victory in that one does not consciously think and decide upon a course of action, it is merely chosen for us through the arbitrary collapse of a wave function somewhere amongst the myriad of neurons in one's brain.

Another argument that gets raised is the emergent complex behaviour of simple systems such as, for example, cellular automata [5]: the "self" that thinks thoughts and decides decisions is something arising out of the complexity of the neural network in the brain and is distinct from the underlying substructure.  But this emergent behaviour is not something that exists of itself and is merely pareidolia no more autonomous than the patterns of dots occurring in the cellular automata simulation.  Of course there is a difference between simple cellular automata and a human brain in that the brain does exhibit the ability to learn and change behaviour based on this learning.  But learning should not be mistaken for free will, it is an improvement in the ability to predict outcomes and thus is another input into the calculation of what is next thought or decided.

Harris dismisses the compatibilist view as simply a post hoc justification of what has occurred which does not substantiate that an alternate history could have happened.  When it is asserted that someone or something, other than the self, has free will, this is based on observation that it behaves in ways that are self-generated, goal-seeking, and non-uniform in response the same stimuli.  However, rule-basead "artificial intelligences" such as computer opponents in various games also display these traits as do most animals.  Assertions about free will are about interior states not amenable observation and where self-reporting is suspect as explained above.  As a result, when discussing the past, at best one can provide an enumeration of roads not taken, and when discussing the future, it devolves into an analysis of constraints on free action.  Far from making the case for free will, it is not even wrong.

Thus Harris succeeds in his argument for determinism and concludes with some discussion on the implication that people are not the authors of their actions.  Since much of morality considers the intent to cause harm a prime consideration as to the morality of an act, does determinism make morality irrelevant.  Harris argues that it does not make morality irrelevant but rather it suggests that punishment does very little (except in a Pavlovian sense) to deter unwanted behaviour.  Determining desired and undesired behaviours (i.e. what is moral vs. immoral) does not change whether people have free will.  However, how justice is meted out must change once you accept that people do not have free will.  While containment of dangerous persons and deterrence through the threat of punishment is necessary for the greater good, rehabilitation should be the focus rather than incarceration as punishment.

The persistence of the illusion of free will is not difficult to explain.  Human beings tend to give themselves credit for good things and blame bad things on externalities.  When someone achieves a goal or completes a project or provides for their family or community, these good things are attributed as achievements of the self, including that they are self-generated, innovative, or even brilliant solutions to problems that others have failed to solve.  The credit is then given to the conscious self because of the physiological reaction of good feeling that follows.  Bad things that happen are blamed on externalities because if one admits the harm done, if often results in a physiological reaction of bad feeling.  Whether good achievements are self-generated or are the results of externalities does not diminish their good.  The key here about bad things is that humans do have a capacity to learn which can lead to a change in behaviour in a future similar situation.  This learning comes from the sum influences on the individual and is not a function of some illusory free will to learn.


References:

(0) Harris, Sam. Free Will. India, Free Press, 2012.

(1) https://www.thebigq.org/2020/11/26/what-are-some-of-the-key-developments-in-cognitive-neuroscience-%E2%96%B6/

(2) Ananthaswamy, Anil. The Man Who Wasn't There: Tales from the Edge of the Self. United States, Dutton, 2016.

(3) Judgment Under Uncertainty: Heuristics and Biases. Spain, Cambridge University Press, 1982.

(4) Asimov, Isaac. Foundation. United States, Random House Publishing Group, 2004.

(5) https://lifeinbeaverton.blogspot.com/2019/01/python-version-of-game-of-life-cellular.html

Saturday, October 3, 2020

Time series on Oregon State Rep and Senator

 So I have calculated the gerrymandering co-efficient for Oregon State Representatives and Oregon State Senate for the years from 2002 to 2018.  The co-efficient is stable for the State House of Representatives and does show that the re-districting resulting from the 2010 census had some effect in 2012.  The coefficient for the Oregon State senate is unstable.  One of the issues here is that approximately half of the senate seats are voted on every 2 years with possibly some special 2 year terms to replace vacancies.



Saturday, September 19, 2020

More on gerrymandering

 I have done some analysis on the variance between the popular vote vs. seats held in a legislative body for a few states across the 2018 and 2016 elections.  I've tries to find if I can download the data in some format and while some of the states do provide some files that can be downloaded, it has been easier to create a spreadsheet and enter the values myself.  As a result it is rather slow to develop the data, but already, for comparison I have found a few interesting results.  See the graph below (made with matplotlib, let me know if you are interested in the code).  This plots a number of elections with the Kg coefficient that I have calculated on the x-axis and the number of seats in the legislature on the vertical axis.  For the state senates, I have used the number of seats in the senate even though generally only half the seats are up in any general election.  Then I plotted 2 lines.  The green line is where the marker would be if the overall Kg reflected a variance of 1 seat between the popular vote and the seats held.  The yellow line is where the marker would be if the Kg reflected a variance of 2 seats.

Graph of elections

Some of the interesting things that we see here is that most legislative bodies seem to reflect some bias.  This does not identify which party benefits.  It just identifies if there is a bias.  Some bodies do have a pretty high level of bias.  For a number of these bodies I included a point for 2016 as well as 2018.  The direction of change cannot be predicted and seems to be a function of what motivates voters to go vote more than a function of the map.  While setting up a map overall can tilt the playing field, judging on the change in Kg from election to election, shows that migration, candidate quality and voter motivation can all influence the results, for example note the movement of the Idaho Senate and House between 2016 and 2018.  One moved in the direction of more gerrymandering and the other in the direction of less.  In compiling the statistics, I was struck by how many districts (about 20%) only had a single candidate.  This kind of situation clearly depresses voting based on the results that were reviewed.  In a district where there were multiple candidates, the number of votes cast was in some cases double.  For example, in Oregon, when there was only 1 candidate the number of "other" votes, typically write-ins and possibly spoilt ballots, was 10 times when a candidate from both the democratic and republican parties was on the ballot.
The next thing I am going to investigate is the change over time of the Oregon House and Senate, especially if I can see if there was a marked change as a result of the decennial redistricting.


Tuesday, September 15, 2020

Definition of a Gerrymandering coefficient using Oregon voting data 2012-2018

Abstract

Drawing of the lines of electoral districts is difficult work.  It is also one where an advantage can be built in systematically to favor a political party.  This systematic bias is called gerrymandering.  It creates a difference between the aggregate vote for each party and the number of seats held in a legislative body by each party.  It can be done in multi-party or two-party systems.  We will briefly look at some previous work that has been done to measure the level of gerrymandering.  This post introduces a way to quantitatively compare the level of gerrymandering by way of calculating a gerrymandering index [Kg] to be able to compare different jurisdictions.  In the USA, since district lines are drawn by a state body, it makes sense to look at this at the state level.  We will calculate the Kg for Oregon across a number of different elections and draw conclusions about the fairness of how the district lines are drawn.

Theortical considerations

An assumption of a fair electoral system is that the population, as a whole, gets a legislative body that closely mirrors the overall vote for the various parties that were running representatives.  This is represented by equation (1).







Where Vtotal is the total number of ballots cast in the election, Vi is the number of ballots cast for party i, Stotal is the total number of seats in the legislature, Si is the number of seats won by party i.

We can calculate the difference between the actual result in the proportion between parties and the theoretical result as shown in equation (2)

In the case where there is perfect agreement between the aggregate vote totals for each party and the number of seats won, this value is close to zero.  The maximum value is n, the number of parties.  One of the limitations is that the number of seats being considered is a small integer and as a result there is not a level of granularity to be able to get Kg smaller than n/2Stotal.

Previous work that has been done on this topic include:

https://www.washingtonpost.com/wp-srv/special/politics/gerrymandering/

http://www.svds.com/gerrymandering/

One of the metrics used in the previous research is "squiggliness" of the borders of districts where they are compared based on the variance from regular shapes.  I think this is unrealistic.  Borders and coastlines are ragged in many places and population density varies.

The Silicon Valley Data Science site has aged data and it is calculated for only the 2012 general election.  But it does use a similar methodology to determine the discrepancy between the number of seats vs. proportion of the vote on a state by state basis as overall nationally.  (It reports nationally, that for the US house of representatives, the republicans have 17 more seats than proportional representation would award them on a national scale but when considered on a state by state basis, the discrepancy is only 4 seats in favor of the republicans.)

Analysis of Oregon voting and results

I've calculated the Kg for the US house seats in Oregon, the Oregon Senate seats and the Oregon House seats based on data obtained from the Oregon Secretary of State for the 2012 through 2018 elections.  The total number of votes for Democratic candidates was summed.  Similarly for Republicans and finally, all of the other candidates as well as write-in votes was totaled for a third category.  Then the seat allocation by party was compared with the proportion of the overall vote.  From the difference in this comparison, the coefficient Kg was calculated.  The results are summarized in the graph below.

Conclusion and reflection

The US house races have the highest Kg values due to the small number of seats.  There isn't really any specific time trend in the values.  As you might imagine the imbalance was mostly in the Democrats favor, but in one case, the imbalance was in favor of the Republicans.  Comparing the US house vs. OR senate vs. OR house, it seems that the OR senate districts have the least amount gerrymandering since with 30 seats, you would expect higher values than the house, which has 60 seats.  More data needs to be gathered.  Analysis of years prior to 2012 will give insight as to whether the redistricting after the 2010 decennial census tilted the political field or not.  Analysis of other states will help calibrate the interpretation of Kg for the number of districts being considered.

In terms of seats, the US house of representatives is actually 1 seat in favor of the democrats.  The Oregon House of representatives is tilted 4 or 5 seats in favor of the democrats.  However the Oregon Senate has changed over time having 1 or 2 seats in favor of both parties at different times.

Having delved into this data so deeply has made me realize how simplistic the representation of Oregon as a Democratic haven is.  There are some areas that are have a large majority of supporters of one of the parties, but there are in fact a lot of areas which are quite close and the "Other" vote total was greater than the difference between the democrat and republican candidates.  Since the collapse of the center and the proliferation of litmus test issues, it does raise serious questions how government can be made to work to find compromises between the two factions and avoiding the tyranny of the majority.

Friday, September 11, 2020

We need a Universal Borrowing Privilege for all Oregon Libraries

Knowledge is power.  Libraries, especially free public libraries, are a source of knowledge and we need to do everything possible to encourage their use.  I am advocating that Oregon adopt a Universal Borrowing Privilege similar to the one in place in California.  Effectively, this allows every Oregon resident of a district which has a public library to be able obtain borrowing privileges from any other Oregon public library.  Since many of the resources loaned by libraries are now electronic, allowing residents to get library cards at other libraries can smooth out demand for electronic resources.  Also, if you happen to live on the edge of a jurisdiction, this would make it possible to access the library closest to you if it happened to be in another jurisdiction.  Further, this can allow a library to justify establishing a narrowly targeted collection since this now may be accessed by all Oregon residents.  Already, the Inter-Library Loan system does something similar with physical books and a number of libraries have established reciprocity agreements amongst themselves.  The proposed change would simply make these reciprocity agreements universal across all of Oregon automatically.  This can be simply enacted by adding to ORS section 357 wording similar from California Law Sections 20204 and 20205, with just a couple of editorial change, reproduced below:


ยง20204: (a) Public libraries participating in direct loan programs under this Act shall not charge any fee to non residents for borrowing privileges.

(b) Reserves and interlibrary loan requests shall be accepted by the participating public library under the same rules and policies applied to local residents.

(c) All procedures governing registration of borrowers shall apply equally to residents and non-residents.

(d) All materials normally loaned by a participating public library are available for loan to non-residents under the same rules and policies applied to local residents.

(e) All loan and return rules governing circulation apply equally to residents and nonresidents. If overdue material are returned to a library other than the library from which borrowed, fines may be paid to and retained by the library to which the return is made. Payments for lost or damaged materials are payable to the lending library, and are to be forwarded by the library to which payment is made.

(f) Special loan privileges extended by the participating public library to teachers and other groups within its jurisdiction need not be extended beyond the jurisdiction.


ยง20205: An eligible non-resident borrower must be a resident of California,

(a) Hold a valid borrowers card issued by their home library, or

(b) Hold or obtain a valid non-resident borrowers card issued by any California public library, or

(c) Hold a valid state borrowers identification card issued by any California public library;

(d) And present any additional identification normally required by a library of its own residents.

(e) Nothing in this section shall prevent the issuing of a non-resident card or charging of fees to a resident of another state, except that loans to such non-residents shall not be counted as reimbursable transactions.


I strongly urge that you write to your State Senator, State Representative and the Governor of Oregon to express your support to pass legislation to give Oregonians a Universal Borrowing Privilege.  Here's a link where you can find their email addresses:

https://www.oregonlegislature.gov/FindYourLegislator/leg-districts.html

Saturday, April 18, 2020

Death rates

In the news, we are told that Covid-19 has killed 37,841 people as of 4/18/2020.  While the rate has varied, it seems to be about 2100-2200 people per day.  So let's get some context around that.
The population of the US is approximately 330 million people.  The average life expectancy is 78.5 years.  This means that each year, 4.2 million people die.  That is 11,517 people per day.  So what we are saying seems to be that an extra 20% of death is being handed out?  Not necessarily.  Later, government statistics will come out to determine whether Covid-19 was killing people in addition to normal causes of death, or rather instead of normal causes of death.
Repeating the calculation for Oregon, with the same life expectancy and a population of 4.2 million, normally 147 people die each day.  Currently, the number of deaths from Covid-19 seems to be about 4 or 5 people per day.  This is a much lower rate in comparison to the national picture.  Here one might argue that it is much more likely that people are being killed by Covid-19 rather than some other causes such as traffic and gun accidents because of social distancing measures.  In other words, probably Covid-19 is not adding to the death toll, people are dying at the same rate as before but for different reasons.  Unfortunately, data compilation, validation and publishing take time, so resources such as CDC's Wonder are not going to answer just what kind of effect Covid-19 has had on the country until later.
Just also want to note that I have way simplified the subject.  There are regional and seasonal variations.  The short term effect of social distancing may be very hard to determine vs. the effect of the Covid-19 infection rate.  Assumptions of paribus ceteris may be invalid for many reasons.  Also, it is not clear that we have reached the peak of the mortality curve for the US or the deaths from Covid-19 could possibly remain level for some weeks.  In the case of a doubling of the daily death rate or a sustained high level, then clearly the virus is adding marginally to the death rate and the life expectancy will go down when they calculate the statistics.  For example, if we assume that 2200 daily deaths are indeed on top of the normal national death rate, then this would reduce the life expectancy to 65.9 years which is a pretty significant reduction.
Again, I want to stress that we won't be able to figure out the actual effects until all the data has been gathered and analyzed but it sure is fun to speculate about it.

Saturday, April 11, 2020

Random Alien Writing

I've been thinking about this for some time and finally found some time to write the program.  The program is at the bottom of the post and written with Python 3.8.  Basically, it creates a canvas filled with strange looking writing.  I was inspired by some of the examples of "alien" writing used in various sci-fi movies and TV shows.  I thought to myself, should be easy to create a program that can create examples of this kind of writing.  I re-used the basic framework I had developed for the program to simulate cellular automata. (I have re-used the framework for some other bits I've made for myself, not (yet) posted here and it is becoming one of my favorites.)  My first attempts, frankly, looked like white noise on a screen.  Over a couple of days I refined the parameters to get it closer to what the brain will recognize as having some semantic content and organization and the current version is pretty good.  I've used it to generate the background I've added to the blog.  I hope you like it!  I'm not going to go into a detailed explanation of the program elements, but if you have questions about it, post a comment.

The Program




from tkinter import *
from random import randint

class App:
    """This class is derived, I think, from a TK class and is designed to
    work with the TK environment.
    Requires imports from Tkinter, math, and random"""
    def __init__(self, master):
        """This class is the whole of the application """
        # Necessary Frames
        frame = Frame(master)
        control_frame = Frame(frame)
        control_frame.grid(row = 0, column = 0, sticky = N)
        canvas_frame = Frame(frame)
        canvas_frame.grid(row =0, column = 1)

        #Application variables
        self.char_pts = [(1,1),(7,1),(14,1),(1,7),(7,7),(14,7),(1,14),(7,14)
                         ,(14,14)]
        self.chars = []
        self.text = []
        self.tmpstr = '\nFor another,\nHit Randomize'
        
        #Control FrameWidgets
        self.lab = Label(control_frame, text=self.tmpstr)
        self.lab.pack(side = TOP)
        self.b1 = Button(control_frame, text='Randomize')
        self.b1.config(command=self.randomize)
        self.b1.pack()
        
        # The Canvas
        self.canvas = Canvas(canvas_frame, width = 800, height = 800)
        self.canvas.config(bg='white')
        self.canvas.pack()
        frame.pack()

        #Menu
        menubar = Menu(root)
        menu_1 = Menu(menubar, tearoff=0)
        menu_1.add_command(label='Quit',command=root.destroy)
        menubar.add_cascade(label='File', menu=menu_1)
        master.config(menu=menubar)
        self.randomize()

    def randomize(self):
        self.canvas.delete(ALL)
        # create characters
        self.chars = []
        for j in range(10):
            new_char = []
            b = randint(0,8)
            for i in range(randint(2,7)):
                a = b
                b = (a + randint(1,8))%9
                new_char.append((a,b,randint(1,2)))
            self.chars.append(new_char)
        # create text, in 2-7 letter long words.
        tl = 0
        self.text = []
        while tl <3000:
            wl = randint(2,7)
            for k in range(wl):
                self.text.append(randint(1,len(self.chars)-1))
            tl = tl + wl + 1
            self.text.append(0)
        # now write the text using the characters
        i = 0
        j = 0
        for char in self.text:
            if char != 0:
                for seg in self.chars[char]:
                    start = seg[0]
                    end = seg[1]
                    wt = seg[2]
                    x1 = i*15+self.char_pts[start][0]
                    y1 = j*17+self.char_pts[start][1]
                    x2 = i*15+self.char_pts[end][0]
                    y2 = j*17+self.char_pts[end][1]
                    if x1==x2 or y1==y2:
                        self.canvas.create_line(x1,y1,x2,y2,width=wt)
                    else:
                        self.canvas.create_arc(x1,y1,x2,y1,width=wt)
            if i == 50:
                j = j + 1
                i =0
            else:
                i= i + 1
        self.canvas.update()

if __name__ == '__main__':
    root = Tk()
    root.wm_title('Alien Text')
    app = App(root)
    root.mainloop()
     


Thursday, April 9, 2020

About "random" numbers

It is an interesting thing that if you were to ask some person to give you a "random" 6-digit number, they would tend to try really hard to not repeat digits.  People have a tendency to think that random number do not have repeating digits.  But an analysis of these numbers shows that, in fact, for longer numbers, it starts to be more likely that there are repeated digits than not.  Below is a table of the number of digits and the likelihood that there are repeating digits.
(argh, excuse me but there is not a control to create a table!!)
Number of digits vs. likelihood of repeated digits:

  1. None
  2. 9.99%
  3. 28%
  4. 50%
  5. 70%
  6. 85%
  7. 94%
  8. 98%
  9. 99%
  10. nearly 100%
So the psychology going on here is that people have a sense in 2 digit and maybe 3 digit numbers that repeated digits in a number are rarer than unique.  However, in reality, when you get to 5 and 6 digit numbers, most numbers have repeated digits.
The math of permutations and combinations is explained here .
The python math module functions are explained here.  The ones that are relevant are the factorial, comb and perm functions.  I wrote some code to re-create these.  I found out that after 10 levels of recursion, Python gives you an error message.  So I had to use the math module version of the factorial function.

# This defines the permutation function
# and the combination function

from math import factorial

def perm(n,r):
    return factorial(n)/factorial(n-r)

def comb(n,r):
    return factorial(n)/factorial(n-r)/factorial(r)

for i in range(2,11):
    print(i,1-perm(10,i)/10**i)

Also, if you don't believe the math, I also wrote a piece of code that does a brute force calculation.  It does take some time to work its way through all the different combinations.  And it will not give the exact same results since it is not considering the numbers that start with 0.
 
# This program prints out how many of the numbers in a range
# have repeating digits

n = 2
flag = True
while flag:
    test = 10**(n-1)
    count = 0
    print(test)
    while test < 10**n:
        a = str(test)
        dupe = False
        for i in range(10):
            if a.count(str(i))>1:
                dupe = True
        if dupe:
            count = count + 1
        test = test +1
    ratio = count / 10**n
    print(n,count, ratio)
    ask = input("continue? (n to end) >")
    if ask == 'n':
        flag = False
    n = n+1
So it's an interesting bit of psychology so I hope you enjoy it.             

Thursday, December 26, 2019

Social Security Administration Baby Name Database

Introduction

At this link, you can access the SSA's so-called Baby Name data broken down both nationally and by state.  Each file is a zip file.  When you unzip the State file, it gives you a text file named for each 2-letter state code.  These files use a comma separated values format and each line includes, the state (which seems to me to be redundant), the sex, the year, the name and number of births.  The national data files are broken down by year.  In each year's file, again formatted in CSV, it includes the name, the sex and the number of births.  I downloaded these files and decided I wanted to make a program that would show a graph for a given name, for a given locale over the years.  In the program, I used Python's CSV module to read the file data into lists of lists.  One thing is that CSV defaults every field to strings so you have to convert things to floats.

The UI

So the UI has been made with Tkinter.  It consists of an Entry for the name, a couple of radio selectors for sex, a OptionMenu for the state, a button to generate the graph and a bunch of labels.
I used an Entry for the name information.  For the sex, so as to reduce user entry errors, I used radio buttons.  Again, to ensure data validity in calls to subroutines, I used an OptionMenu object (usually called a dropdown).

The Action

Basically, everything starts after the user hits the Graph button.  This then calls the calc function.  The calc function figures out whether the user selected a national or state level request.  If it is national, then the code calls the national function, if it is state level then it calls the bystate function.
Both the national and bystate functions go and read the appropriate files to build two lists which have corresponding x and y values to be graphed.  These both then call the graph function which uses PyPlot API calls to draw and display the graph in a separate window.  Typical results are below and of course, there's no reason why I would have picked the name Martin for the examples.


The Code

## Code to process downloaded national name data - Using GUI

from tkinter import *
from tkinter import ttk
import matplotlib.pyplot as plt
import csv

filelocation = ##INSERT STRING OF WHERE YOUR FILES ARE

## This function reads the information from the national files.
## Data is Name, M/F, number of births
def national(the_name,sex):
    xdata=[]
    ydata=[]
    for year in range(1880,2019):
        file = open(filelocation+"yob"+str(year)+'.txt', newline='')
        r = csv.reader(file)
        for row in r:
            if row[0]==the_name and row[1]==sex:
                xdata.append(float(year))
                ydata.append(float(row[2]))
        file.close()
    graph(xdata,ydata,the_name,sex,"USA")


## This function reads the information from the State files.
## Data is State,Sex (M/F), Year,Name, Number of births
def bystate(the_name,sex,state):
    xdata=[]
    ydata=[]
    file=open(filelocation+state+'.txt',newline='')
    r = csv.reader(file)
    for row in r:
        if row[3]==the_name and sex==row[1]:
            xdata.append(float(row[2]))
            ydata.append(float(row[4]))
    file.close()
    graph(xdata,ydata,the_name,sex,state)
            
## This part creates the graph using Matplotlib (plt)
def graph(xdata,ydata,the_name,sex,state):
    fig,ax = plt.subplots()
    line1, = ax.plot(xdata,ydata,label=the_name)
    ax.legend(loc='upper left')
    ax.set_title('Births per year in '+state+': '+the_name+' ('+sex+')')
    plt.ylabel('Number of births')
    plt.xlabel('Year')
    plt.show()

def calc():
    mf = ['M','F']
    if sel_state.get()=='USA':
        national(getn.get(),mf[sel_sex.get()])
    else:
        bystate(getn.get(),mf[sel_sex.get()],sel_state.get())
## Below is the data for the state/national selection dropdown
states=['USA','AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA',
        'HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME',
        'MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM',
        'NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX',
        'UT','VA','VT','WA','WI','WV','WY']
##Below is the set up for the GUI window
root = Tk()
content = ttk.Frame(root)
frame = ttk.Frame(content)
sel_state = StringVar()
sel_sex = IntVar()
lblinstr = ttk.Label(content, text="Enter Name and location")
getn= ttk.Entry(content, text="Name")
male= ttk.Radiobutton(content, text='Male', variable=sel_sex, value=0)
female=ttk.Radiobutton(content, text='Female',variable=sel_sex,value=1)
ok = ttk.Button(content, text="Graph", command=calc)
rlbl = ttk.Label(content, text="Enter Name")
slbl = ttk.Label(content, text="Enter Sex (M/F)")
stlbl = ttk.Label(content, text="Select USA or state")
statedd = ttk.OptionMenu(content, sel_state, *states)
##Below, we put the GUI together
content.grid(column = 0, row = 0)
frame.grid(column=0, row=0)
lblinstr.grid(column=0, row=0)
rlbl.grid(column=0, row=1)
getn.grid(column=1, row=1, sticky=N)
slbl.grid(column=0, row=2)
male.grid(column=1, row=2, sticky=N)
female.grid(column=2, row=2)
statedd.grid(column=1, row=3)
ok.grid(column=0, row=4)
## And run!!
root.mainloop()

Sunday, December 22, 2019

Creating graphs of Federal Reserve GDP data

After having read some interesting speculations about the future of work, AI, and robotics, I ran across a free resource in the form of the St. Louis Federal Reserve bank FRED system.  You do have to sign up with a valid email address, but there is no charge and the government is, well, supposed to spam or sell your email address.
After I had signed up, I configured to download some data.  This was the total annualized quarterly GDP as well as the goods and services components, see the picture below which is a screen shot of data from the FRED system.
The download was a text file, it had a header row and was delimited by tabs.  This is not ideal, but it is something that the csv Python standard library can handle.  The idea was to use matplotlib to graph the data.  I could have tried to create the graph on a Tkinter canvas using the draw functions.  But I am getting ahead of myself...
First we need to read the data and create Lists for the x and y values of the two different lines.  I will run through the code I wrote.  First we need to open the text file:
file = open(filelocation, newline='')
r = csv.reader(file,delimiter='\t')
Then I will initialize variables:
period=[]
goods=[]
services=[]
header=True
The variable header is there so that we can skip over the header row during the loop which reads the data and creates the iterables that will be used to graph the data.  The Pyplot api expects NumPy arrays for the data to be graphed but Lists and most other iterables work.
for row in r:
    if header:
        header=False
    else:
        date=row[0]
        serv=float(row[1])
        good=float(row[2])
        total=serv+good
        dateval=float(date[0:4])+float(date[5:7])/12
        period.append(dateval)
        goods.append(good/total*100)
        services.append(serv/total*100)

Now at the start I had imported matplotlib.pyplot as plt, and the next part of the code creates the graph and shows it using rather simple matplotlib calls.  I did look up a lot of stuff on this in the examples and tutorials, but it is not very clear so I struggled a bit to get it to work right.
fig,ax = plt.subplots()
line1, = ax.plot(period,goods,label='Goods')
line2, = ax.plot(period,services,label='Services')
ax.legend(loc='upper left')
ax.set_title('Change in GDP composition over time')
plt.ylabel('% of GDP')
plt.xlabel('Year')
plt.show()
Here's the final product:

What does this mean?

In this graph we see a long-terms trend wherein the American economy has been more and more reliant on services instead of goods.  We don't make things any more, or maybe a better way to put it, we make things with such high efficiency and productivity that to feed, house, cloth, provide transport and other utilities, some one third of us can provide for everyone.  So we have to find something for the other 2/3rds to do.  That turns out to be providing any and all kinds of services to the general population.  This trend has been around for a while and it probably extends to the left back to the turn of the century although likely it flattens out.  During that time, many have treated it like the weather: something to complain about, but nothing can be done about it.  Even the current politicians are not going to be able to reverse this trend no matter how hard they try.  About the only possible strategy would be to do everything to limit or even reverse the growth in population.  However, this would have other significant adverse consequences, putting the economy into a depression.
One take-away is that if you are starting a new business, concentrate on services rather than goods.  If you have a goods-related business, then consider adding some kind of service provision to complement what you make.  Growth in Services is much more likely, if not easier.