Advertisement
NotQuiteAmish

Untitled

Apr 7th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr  7 18:40:12 2020
  4.  
  5. @author: caleb
  6. """
  7.  
  8. ## Section 1 - Import necessary libraries
  9. import pandas as pd
  10. import matplotlib.pyplot as plt
  11. import numpy as np
  12. import statsmodels.api as sm
  13.  
  14.  
  15. ## Section 2 - Download and read data file
  16. url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vRQ6XSqSc79HlLHsOkFHlODwJ7CgHZ8Xp-TegHZuaHGItr2-HY3Oc5ofAANNWlZ6_I5LlRzY9ilil_A/pub?gid=0&single=true&output=csv'
  17. data = pd.read_csv(url)
  18. time  = data.iloc[1:, 1]
  19. rate  = data.iloc[1:, 2]
  20. total = data.iloc[1:, 4]
  21.  
  22.  
  23. ## Section 3 - Fit  a linear regression line
  24. #rate_log  = np.log(rate.astype(float))            # Transform data with log
  25. total_log = np.log(total.astype(float))
  26. const = sm.add_constant(time)                    # Fit the model y=mx+b, not y=mx
  27. model = sm.OLS(total_log[:-8], const[:-8]).fit()  # Negative numbers exclude the last few
  28.  
  29. # Plot the data
  30. plt.close('all')
  31. plt.figure(1)
  32. plt.yscale('log')
  33.  
  34. #plt.plot(time, rate, 'o', color='tab:blue')
  35.  
  36. plt.title('Radioactivity (Low initial concentration)')
  37. plt.xlabel('Time [s]')
  38. plt.ylabel('Rate [counts per 13s]')
  39.  
  40.  
  41. ## Section 4 - Extract parameters of interest
  42. slope     = model.params[1]
  43. intercept = model.params[0]
  44. uncertainty = model.conf_int(alpha=0.05)
  45.  
  46. print(slope)
  47. print(intercept)
  48. print(uncertainty)
  49.  
  50.  
  51. ## Section 5 - Create the best fit line and prediction intervals
  52. model_prediction = model.get_prediction(const[:-8]).summary_frame(alpha=.05)
  53. model_fit = np.exp(model_prediction['mean'])
  54. prediction_lo = np.exp(model_prediction['obs_ci_lower'])
  55. prediction_hi = np.exp(model_prediction['obs_ci_upper'])
  56.  
  57.  
  58. ## Section 6 - Plot the data
  59. plt.plot(time[:-8], model_fit,           color='tab:green')
  60. plt.plot(time[:-8], prediction_lo, '--', color='tab:gray')
  61. plt.plot(time[:-8], prediction_hi, '--', color='tab:gray')
  62. plt.plot(time,      total,       'o',     color='tab:blue' )
  63.  
  64. plt.xlim(0, 1050)
  65. plt.ylim(bottom = 1e0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement