2301520 Fundmentals of AMCS
Data Manipulation & Statistics

Section 1 : Data Manipulation

1.1 Data Import/Export

Directory[]

thisDir=ToFileName[
("FileName"/.NotebookInformation[EvaluationNotebook[]])
[[1]]](* Get the path to directory where this Notebook resides. *)

SetDirectory[thisDir];

Directory[]

data1=Import["linearfitdata.xls","XLS"]

data2=Import["WeibullData.txt","Table"]

data3=Import["anovadata.txt","Table"]

TableForm[data1]

TableForm[data2]

TableForm[data3]

Export["data3.xls", data3, "XLS"]

Export["data2-1.txt", data2, "List"]

!!data2-1.txt

OutputForm[TableForm[data2, TableSpacing-> 0]] >> data2-2.txt

!!data2-2.txt

OutputForm[TableForm[data3, TableSpacing-> 0]] >>> data2-2.txt

!!data2-2.txt

Export["formula.gif", Integrate[1/(x^2-1),x]]

picture1=Import["http://www.chula.ac.th/wp-content/uploads/2014/03/logo-th@2x.png"]

Export["chulalogo.gif",picture1]

plotsample=Plot[Evaluate[Table[BesselJ[n,x],{n,4}]],{x,0,10},Filling->Axis]

Export["plotsample.gif",plotsample]

Export["plotsample.pdf",plotsample]

Export["plotsample.svg",plotsample]

1.2 Handling Arrays of Data

data4=Table[RandomInteger[{4,6}],{i,1,10}]

Length[data4]

Dimensions[data4]

Dimensions[data3]

Total[data4]

Count[data4,5]

Tally[data4]

BinCounts[data4,{{0,5,7}}]

Range[12]

Partition[Range[12],3]

Partition[Range[12],3,1]

data4

First[data4]

Take[data4, 5]

Drop[data4, -5]

Append[data4, 7]

Prepend[data4, 7]

Insert[data4, 7, 5]

data4

AppendTo[data4, 7]

data4

1.3 Data Visualization

data2

ListPlot[data2[[1]]]

ListPlot[data2[[1]], Joined-> True]

data5=Table[{x,Sin[x]},{x,0,2  Pi,0.5}]

plot1=ListPlot[data5,  Filling->Axis]

plot2=Plot[Sin[x],{x,0, 2 Pi}, PlotStyle-> Red]

Show[plot1, plot2]

data2WithNoise=Table[data2[[1]][[i]]+RandomReal[{-2,2}], {i, 1, Length[data2[[1]]]}]

ListPlot[{data2[[1]],data2WithNoise}, Joined-> True]

1.4 Data Transforms and Smoothing

data6=Table[x+(4+RandomReal[]) Sin[x]+(1+RandomReal[]) Sin[5 x],{x,0,9.9,0.1}]

plotdata=ListPlot[data6,PlotStyle->{PointSize[0.013],RGBColor[1,0,0]}]

data7 = Rescale[data6, {Min[data6], Max[data6]}]

ListPlot[data7,PlotStyle->{PointSize[0.013],RGBColor[0,1,0]}]

MovingAverage[data6,3]

ListPlot[MovingAverage[data6,3]]

1.5 Curve Fitting

1.5.1 Fit

data1=Table[{x,(3+0.5Random[])(x^2)+2x-3+2Random[]},{x,-1,2,0.1}]

plotdata1=ListPlot[data1]

fit1=Fit[data1,{1,x,(x^2)},x]

plotfit1=Plot[fit1,{x,-1,2}, PlotStyle-> Red]

Show[plotdata1,plotfit1]

1.5.2 FindFit

model2=a+b x + c (x^2)

fit2=FindFit[data1,model2,{a,b,c},x]

plotfit2=Plot[model2 /. fit2,{x,-1,2}, PlotStyle->Magenta]

Show[plotdata1,plotfit2]

data2=Table[{x,3Sin[4 x + 3+Random[]] + Random[]},{x,-π,π,0.05}]

plotdata2=ListPlot[data2]

model3=a Sin[b x + c]+d

fit3=FindFit[data2,model3,{a,b,c,d},x]

plotfit3=Plot[model3/.fit3,{x,-π,π},PlotStyle->RGBColor[0,0,1]]

Show[plotdata2,plotfit3]

fit3b=FindFit[data2,model3,{a,{b,4.1},c,d},x]

plotfit3b=Plot[model3/.fit3b,{x,-π,π},PlotStyle->Pink]

Show[plotdata2,plotfit3b]

1.6 Interpolation and Splines

1.6.1 Interpolation

data1=Table[{x,(E^(-(x^2)))},{x,-2,2,0.4}]

plotdata1=ListPlot[data1,PlotStyle->{PointSize[0.015],RGBColor[1,0,0]}]

f1=Interpolation[data1]

plotf1=Plot[f1[x],{x,-2,2},PlotStyle->RGBColor[0,0,1]]

Show[plotdata1,plotf1]

f11=Interpolation[data1, InterpolationOrder->1]

plotf2=Plot[f11[x],{x,-2,2},PlotStyle->RGBColor[0,0,1]]

Show[plotdata1,plotf2]

1.6.2 Spline

Needs["Splines`"]

spline1=Spline[data1,Cubic]

Show[Graphics[spline1]]

Show[plotdata1,Graphics[spline1]]

Section 2 : Statistics

2.1 Descriptive Statistics

data1=RandomInteger[{10,90},200]

Mean[data1]

Variance[data1]

Quantile[data1,{1/4,3/4}]

(* <<"Histograms`"; *)

Histogram[data1]

data2=RandomInteger[{10,90},200]

data3=Table[{data1[[i]], data2[[i]]}, {i, 1, Length[data1]}]

Histogram3D[data3]

2.2 Discrete Distributions

bnmdist1=BinomialDistribution[30,1/4]

PDF[bnmdist1,10] //N

CDF[bnmdist1,10]//N

ExpectedValue[x,BinomialDistribution[n,p],x]

ex =Assuming[p<1,Simplify[%]]

ExpectedValue[(x^2),BinomialDistribution[n,p],x]

exsq=Assuming[p<1,Simplify[%]]

ExpectedValue[(x^2),bnmdist1,x]

Mean[BinomialDistribution[n,p]]

Variance[BinomialDistribution[n,p]]

(* Var(X)=E[(X^2)]-(((E[X]))^2) *)
Simplify[exsq-ex^2]

RandomInteger[bnmdist1,10]

2.3 Continuous Distributions

?NormalDistribution

normal1=NormalDistribution[50,10]

PDF[normal1,x]

Plot[%,{x,20,80}]

(∫_(-∞)^x)PDF[normal1,t]\[DifferentialD]t

CDF[normal1,x]

Plot[%,{x,20,80}]

Mean[normal1]

Variance[normal1]

?Quantile

Quantile[normal1,0.5]

Quantile[normal1,0.99]

CDF[normal1,%]

stdnormal=NormalDistribution[0,1]

CDF[stdnormal,1]-CDF[stdnormal,-1]

%//N

ExpectedValue[(x^4),stdnormal,x]

(∫_(-∞)^∞)(x^4)PDF[stdnormal,x]\[DifferentialD]x

RandomReal[stdnormal,100]

Section 3 : Statistical Charts and Graphs

<<"BarCharts`";<<"Histograms`";<<"PieCharts`"

p=Table[Prime[n],{n,10}]

BarChart[p]

BarChart[{1,-3,4,5,2,3},{3,6,4,3}]

StackedBarChart[{1,-3,4,5,2,3},{3,6,4,3}]

PercentileBarChart[{1,-3,4,5,2,3},{3,6,4,3}]

PieChart[p]

PieChart[p,PieExploded->All ]

PieChart[p,PieExploded->{3,8} ]

<<"Graphics`";

TextListPlot[p, Axes-> True, AxesOrigin-> {0,0}]

LabeledListPlot[p, AxesOrigin-> {0,0}]

expdata=Table[{x,0.92 (10^(0.94 x))+RandomReal[{-1.,1.}]},{x,0.2,1.2,0.1}]

erexpdata=(Append[#1,RandomReal[{0,2.}]]&)/@expdata

<<"ErrorBarPlots`";

ErrorListPlot[erexpdata]

data

Histogram[data]