#!/usr/bin/env python # coding: utf-8 # # Solution sketch for lab 1 assignment # ## Johannes Mauritzen # ## MET2010 Applied Statistics # ## NTNU Business School # In[1]: import numpy as np import matplotlib.pyplot as plt # ### Assignment 1 # # Generating arrays - random order generator. Make an NP array list that includes the names of your immediate family (you can make up the names if you want) as well as uncles and aunts and cousins. Now pretend that you are all staying at a small cabin with a single bathroom and shower and you need a fair way of randomly ordering who will get to use the bathroom/shower during the morning. So you offer to create a random ordering. Write a program that creates a random ordering of the names in the array. (Hint: you could probably use the function [np.random.choice()](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.choice.html) # ### Solution to 1 # In[2]: # I will use the basic list, but you could easily also do this with np.array family = ["Johannes", "Siri", "Bo", "Odin", "Olav"] # In[3]: np.random.choice # In[4]: np.random.choice(family,5, replace=False) # ### Assignment 2 # # Generating random values # # a. Generate 100 random values from a uniform distribution (you can choose the range yourself, 0-1, 0-10, whatever). Plot the values in a histogram # # b. Now generate a 100x100 2-d array of random values from a uniform distribution. # # c. Take the mean across each column and store this in a 1-d array. Plot a histogram of this array. Does it look like the array in a? # ### Solution to 2 # In[5]: # I use the np.random.uniform command to generate 100 random values from a uniform distribution rvals = np.random.uniform(0,10,100) plt.hist(rvals) # In[6]: # To create a 100 x 100 array, I start by generating 10.000 numbers (100x100) rvals2 = np.random.uniform(0,10,10000) # Then I change the shape of the array rvals2 = rvals2.reshape(100,100) rvals2.shape # In[7]: rvals # In[8]: rvals2_means = rvals2.mean(axis=1) plt.hist(rvals2_means) # We notice that we don't get a uniform distribution. Instead, this looks more normal. This is not accident - this is one of the most central results in statistics: that the mean (or sum) of any distribution of numbers end up converging towards a normal (gaussian) distribution! # ### Assignment 3 # # Working with a small data set # # a. Find a small data set on a website somewhere and import it as a numpy array. You can either copy and paste the data directly into Jupyter, or copy it into a txt document then read it in as we did above. (For those who really want a challenge, you could experiment with using a web-scraping package like [BeautifulSoup](https://beautiful-soup-4.readthedocs.io/en/latest/) to import data directly from a website.) # # b. Create a simple plot of the data - it doesn't need to be pretty. # ### Solution to 3 # See the example of electricity prices in the lab # In[ ]: