In[1]:= In[2]:= RANDOM NUMBERS & HISTOGRAMS r : RandomReal In[3]:= In[4]:= In[5]:= ri : RandomInteger In[6]:= rd : RandomInteger 1, 6 In[7]:= list Table rd rd, 100 2 dice Out[7]= 7, 11, 7, 10, 7, 8, 3, 2, 3, 5, 8, 6, 7, 11, 7, 8, 9, 5, 5, 4, 9, 10, 10, 5, 4, 5, 9, 3, 10, 4, 7, 3, 10, 6, 7, 5, 6, 5, 3, 10, 4, 4, 10, 6, 8, 9, 10, 6, 10, 5, 12, 11, 4, 9, 9, 6, 8, 3, 6, 7, 9, 10, 6, 7, 7, 10, 7, 7, 7, 4, 3, 6, 3, 6, 7, 10, 10, 10, 5, 8, 3, 9, 11, 3, 8, 8, 9, 7, 8, 5, 8, 9, 7, 7, 6, 3, 7, 8, 9, 8 In[8]:= Histogram list, 11 20 15 Out[8]= 10 5 4 6 8 10 12
2 114_histogram.nb In[9]:= Histogram list, Automatic,"Probability" 0.30 0.25 0.20 Out[9]= 0.15 0.10 0.05 In[10]:= In[11]:= 4 6 8 10 12 14 data1 RandomVariate NormalDistribution 0, 1, 500 ; Histogram data1 100 80 Out[11]= 60 40 20 In[12]:= 3 2 1 0 1 2 3 now try do it yourself bin counts and histogram In[13]:= Out[13]= 500 list data1; ll Length list In[14]:= binsize.2; del 3 2; In[15]:=
114_histogram.nb 3 In[16]:= xmin Min list ; xmax Max list ;imin Floor xmin binsize del ;imax Floor xmax binsize del ; initb : Clear b, i ; Do b i 0, i, imin, imax In[18]:= In[19]:= initb; Do ix Floor list i binsize ; b ix b ix 1;, i, Length list In[20]:= Plot b Floor x binsize, x, xmin del binsize, xmax del binsize 40 30 Out[20]= 20 10 4 3 2 1 1 2 3 In[21]:= once it worked step by step, let us try a single command
4 114_histogram.nb In[22]:= mybins data_, binsize_ : Clear i, b ; list data; del 3 2; xmin Min list ; xmax Max list ; imin Floor xmin binsize del ; imax Floor xmax binsize del ; Do b i 0, i, imin, imax ; Do ix Floor list i binsize ; b ix b ix 1;, i, Length list ; In[23]:= binsize.2; mybins data1, binsize ; Plot b Floor x binsize, x, xmin del binsize, xmax del binsize 40 30 Out[23]= 20 10 4 3 2 1 1 2 3 In[24]:= below is normalized to 1, "pdf"
114_histogram.nb 5 In[25]:= binsize.2; mybins data1, binsize ; Plot b Floor x binsize Length data1 binsize, x, xmin del binsize, xmax del binsize 0.4 0.3 Out[25]= 0.2 0.1 4 3 2 1 1 2 3 In[26]:= In[27]:= fig ; now compare with "parent" distribution In[28]:= pdf Evaluate PDF NormalDistribution 0, 1, x Out[28]= In[29]:= x2 2 2Π Show fig, Plot pdf, x, 3, 3 0.4 0.3 Out[29]= 0.2 0.1 4 3 2 1 1 2 3 In[30]:= In[31]:= In[32]:=
6 114_histogram.nb In[33]:= In[34]:= Mean and SD In[35]:= Evaluate PDF NormalDistribution 4, 1, x Out[35]= 1 2 4 x 2 2Π In[36]:= In[37]:= data3 RandomVariate NormalDistribution 4, 1, 500 ; In[38]:= In[39]:= Out[39]= Mean data3 4.02718 In[40]:= StandardDeviation data3 Out[40]= 1.0081 In[41]:= Mean data3^2 Mean data3 ^2 Out[41]= 1.01424 In[42]:= Sqrt Out[42]= 1.00709 In[43]:= almost same as SD In[44]:= In[45]:= PLOTTING WITH ERROR BARS In[46]:= take a multicolumn list "data" and cut out 3 columns: m for x, n for y, k for errors;then create yp for y error, ym for y error and blist a table of vertical lines that connect them
114_histogram.nb 7 In[47]:= mybars data_, m_, n_, k_ : xx data m ; yy data n ; ss data k ; yp yy ss; ym yy ss; blist Table xx i, ym i, xx i, yp i, i, Length xx ; Graphics Line blist 1 In[48]:= Out[48]= data 1, 2, 3, 1, 4, 9,.5,.4,.6 1, 2, 3, 1, 4, 9, 0.5, 0.4, 0.6 In[49]:= data MatrixForm Out[49]//MatrixForm= 1 2 3 1 4 9 0.5 0.4 0.6 In[50]:= Transpose data MatrixForm Out[50]//MatrixForm= 1 1 0.5 2 4 0.4 3 9 0.6
8 114_histogram.nb In[51]:= mybars data, 1, 2, 3 Out[51]= In[52]:= In[53]:= Show ListPlot Transpose xx, yy, mybars data, 1, 2, 3, AxesOrigin 0, 0, PlotRange 0, 3, 0, 10 10 8 6 Out[53]= 4 2 0.5 1.0 1.5 2.0 2.5 3.0
114_histogram.nb 9 In[54]:= Show ListPlot Transpose xx, yy, Joined True, mybars data, 1, 2, 3, AxesOrigin 0, 0, PlotRange 0, 3, 0, 10 10 8 6 Out[54]= 4 2 In[55]:= OR 0.5 1.0 1.5 2.0 2.5 3.0 In[56]:= Needs "ErrorBarPlots`" In[57]:= In[58]:= ErrorListPlot Transpose yy, ss, Joined True 8 6 Out[58]= 4 2 In[59]:= "REAL EXPERIMENT" 1.5 2.0 2.5 3.0 note Timing
10 114_histogram.nb In[60]:= Timing list Table rd rd, 100 000 ; Out[60]= 0.078001, Null In[61]:= Out[61]= Commonest list lucky 7 7 In[62]:= Histogram list 15 000 Out[62]= 10 000 5000 4 6 8 10 12 In[63]:= In[64]:= binsize 1; mybins list, binsize ; Plot b Floor x binsize, x, xmin del binsize, xmax del binsize 15 000 10 000 Out[64]= 5000 4 6 8 10 12
114_histogram.nb 11 In[65]:= Plot b Floor x binsize Length list, x, xmin del binsize, xmax del binsize 0.15 0.10 Out[65]= 0.05 4 6 8 10 12 In[66]:= In[67]:= now compare with true values i 36 for i 7 and 13 i 36 for i 7 In[68]:= In[69]:= In[70]:= In[71]:= Out[71]= In[72]:= Out[72]= Table b i Length list If i 7, i 1 36, 13 i 36, i, 12 0, 193 180 000, 931 900 000, 419 300 000, 143 900 000, 161 900 000, 11 7500, 29 180 000, 29 18 000, 11 37 500, 617 900 000, 371 900 000 N 0., 0.00107222, 0.00103444, 0.00139667, 0.000158889, 0.000178889, 0.00146667, 0.000161111, 0.00161111, 0.000293333, 0.000685556, 0.000412222 In[73]:= In[74]:= READING EXTERNAL FILES, 1st from a local directoory, then from Internet In[75]:= Out[75]= list Import "ExampleData elements.xls" 1 AtomicNumber, Abbreviation, Name, AtomicWeight, 1., H, Hydrogen, 1.00793, 2., He, Helium, 4.00259, 3., Li, Lithium, 6.94141, 4., Be, Beryllium, 9.01218, 5., B, Boron, 10.8086, 6., C, Carbon, 12.0107, 7., N, Nitrogen, 14.0067, 8., O, Oxygen, 15.9961, 9., F, Fluorine, 18.9984
12 114_histogram.nb In[76]:= list TableForm Out[76]//TableForm= AtomicNumber Abbreviation Name AtomicWeight 1. H Hydrogen 1.00793 2. He Helium 4.00259 3. Li Lithium 6.94141 4. Be Beryllium 9.01218 5. B Boron 10.8086 6. C Carbon 12.0107 7. N Nitrogen 14.0067 8. O Oxygen 15.9961 9. F Fluorine 18.9984 In[77]:= list1 Drop list, 1 Out[77]= In[78]:= Out[78]= In[79]:= 1., H, Hydrogen, 1.00793, 2., He, Helium, 4.00259, 3., Li, Lithium, 6.94141, 4., Be, Beryllium, 9.01218, 5., B, Boron, 10.8086, 6., C, Carbon, 12.0107, 7., N, Nitrogen, 14.0067, 8., O, Oxygen, 15.9961, 9., F, Fluorine, 18.9984 list1 Transpose list1 1., 2., 3., 4., 5., 6., 7., 8., 9., H, He, Li, Be, B, C, N, O, F, Hydrogen, Helium, Lithium, Beryllium, Boron, Carbon, Nitrogen, Oxygen, Fluorine, 1.00793, 4.00259, 6.94141, 9.01218, 10.8086, 12.0107, 14.0067, 15.9961, 18.9984 list1 Drop list1, 2, 3 Out[79]= In[80]:= Out[80]= 1., 2., 3., 4., 5., 6., 7., 8., 9., 1.00793, 4.00259, 6.94141, 9.01218, 10.8086, 12.0107, 14.0067, 15.9961, 18.9984 list1 Transpose list1 1., 1.00793, 2., 4.00259, 3., 6.94141, 4., 9.01218, 5., 10.8086, 6., 12.0107, 7., 14.0067, 8., 15.9961, 9., 18.9984
114_histogram.nb 13 In[81]:= ListPlot list1, AxesLabel list 1, 1, list 1, 4, LabelStyle Medium AtomicWeight 15 Out[81]= 10 5 2 4 6 8 AtomicNumber In[82]:= Out[82]= In[83]:= Out[83]= Import "http: web.njit.edu vitaly 114 114_mockdata.xlsx" 1 x, y, sig, 1., 1., 0.5, 1.5, 2.25, 0.4, 2., 4., 0.6, 2.5, 6.25, 0.5, 3., 9., 0.5 list1 Drop, 1 1., 1., 0.5, 1.5, 2.25, 0.4, 2., 4., 0.6, 2.5, 6.25, 0.5, 3., 9., 0.5 In[84]:= In[85]:= Out[85]= list1 Transpose list1 1., 1.5, 2., 2.5, 3., 1., 2.25, 4., 6.25, 9., 0.5, 0.4, 0.6, 0.5, 0.5 In[86]:= Out[86]= list2 Drop list1, 1 1., 1.5, 2., 2.5, 3., 1., 2.25, 4., 6.25, 9. In[87]:= Out[87]= list2 Transpose list2 1., 1., 1.5, 2.25, 2., 4., 2.5, 6.25, 3., 9. In[88]:= Show Plot x^2, x, 0, 3, ListPlot list2 8 6 Out[88]= 4 2 0.5 1.0 1.5 2.0 2.5 3.0 In[89]:= HW: try to add errorbars
14 114_histogram.nb In[90]:= Out[90]= Import "http: web.njit.edu vitaly 114 114_mockdata.txt", "Table" x, y, sig, 1, 1, 0.5, 1.5, 2.25, 0.4, 2, 4, 0.6, 2.5, 6.25, 0.5, 3, 9, 0.5 "Table", no 1 ; keeps integers In[91]:= Note: needs In[92]:= In[93]:=