Mathematica 8: nové statististické a finanční funkce

Jan Hurt, Matematicko-fyzikální fakulta, Universita Karlova v Praze

hurt@karlin.mff.cuni.cz

Odkazy

Obecné odkazy

http://www.wolfram.com/mathematica/new-in-8/

Abecední seznam New in 8.0: Alphabetical Listing

http://reference.wolfram.com/mathematica/guide/NewIn80AlphabeticalListing.html

Detailnější odkazy

Documentation Center: guide/ProbabilityAndStatistics

Probability and Statistics Solvers and Properties

http://www.wolfram.com/mathematica/new-in-8/probability-and-statistics-solvers-and-properties/

Parametric Probability Distributions

http://www.wolfram.com/mathematica/new-in-8/parametric-probability-distributions/

Nonparametric, Derived, and Formula Distributions

http://www.wolfram.com/mathematica/new-in-8/nonparametric-derived-and-formula-distributions/

Parameter Estimation and Testing

http://www.wolfram.com/mathematica/new-in-8/parameter-estimation-and-testing/

Parametric Statistical Distributions

http://reference.wolfram.com/mathematica/guide/ParametricStatisticalDistributions.html

Statistical Visualization

http://www.wolfram.com/mathematica/new-in-8/statistical-visualization/

Financial Visualization

http://www.wolfram.com/mathematica/new-in-8/financial-visualization/

Built-in Financial Computations

http://www.wolfram.com/mathematica/new-in-8/built-in-financial-computations/

http://www.wolfram.com/solutions/industry/financial-risk-management/

http://www.wolfram.com/solutions/industry/financial-risk-management/

Financial Functions (finanční funkce)

Anuita polhůtní (annuity immediate)

Annuity

Anuita předlhůtní (annuity due)

AnnuityDue

Peněžní tok

Cashflow

Časová hodnota peněz

TimeValue

EffectiveInterest

Obligace, dluhopis

FinancialBond

Finanční deriváty

FinancialDerivative

Seznam všech možných typů kontraktů

FinancialDerivative[]

TradingChart

InteractiveTradingChart

Other Financial Topics

In Documentation Center find “financial”.

See also

Financial & Economic Data
Mathematica has immediate built-in access to current and historical financial and economic data.
FinancialData — current and historical stock, fund, index, currency, etc. data
“Price”  ▪  “CumulativeReturn”  ▪  “Volatility50Day”  ▪  “MarketCap”  ▪  “PERatio”  ▪  “EBITDA”  ▪  “DividendYield”  ▪  “SICCode”  ▪  ...
	
CountryData — socioeconomic data and time series for all countries
“GDP”  ▪  “ExportValue”  ▪  “FixedInvestment”  ▪  “ElectricityProduction”  ▪  “OilReserves”  ▪  ...
“CurrencyCode”  ▪  “CurrentUnit”  ▪  “ExchangeRate”  ▪  ...
	
DateListPlot — plot a time series
MovingAverage  ▪  ExponentialMovingAverage  ▪  LinearModelFit

See also

Finance
Mathematica has fully integrated support for many of the tools used in classical and modern finance. These capabilities include financial instrument valuation, advanced time value of money computations, and advanced financial charting with a library of technical indicators. Mathematica also provides immediate access to a large array of financial and economic data, and contains financial import and export tools for working with external data.
Time Value Computations
TimeValue — time value of money computations
EffectiveInterest — effective interest rate conversions
Annuity  ▪  AnnuityDue  ▪  Cashflow
	
Financial Instruments
FinancialDerivative — financial derivatives valuation
FinancialBond — financial bond valuation
	
Financial Visualization »
CandlestickChart — candlestick and OHLC chart of open-high-low-close prices
InteractiveTradingChart — interactive trading chart including financial indicators
TradingChart  ▪  DateListPlot  ▪  RenkoChart  ▪  ...
	
Financial Indicators »
"Open"  ▪  "High"  ▪  "Low"  ▪  "Close"  ▪  "Volume"  ▪  "TypicalPrice"  ▪  "TrueRange"  ▪  "SimpleMovingAverage"  ▪  "ExponentialMovingAverage"  ▪  "BollingerBands"  ▪  "KeltnerChannels"  ▪  "RelativeStrengthIndex"  ▪  "FastStochastic"  ▪  "SlowStochastic"  ▪  "FullStochastic"  ▪  "RateOfChange"  ▪  "UltimateOscillator"  ▪  ...
	
Financial and Economic Data »
FinancialData — current and historical stock, fund, index, currency, etc. data
CountryData — socioeconomic data and time series for all countries
DateListPlot  ▪  MovingAverage  ▪  ExponentialMovingAverage  ▪  LinearModelFit
	
Importing & Exporting External Data »
Import — import from files and URLs
Export — export data, graphics, and expressions to files
"Table"  ▪  "CSV"  ▪  "TSV"  ▪  "XLS"  ▪  "ODS"  ▪  "SXC"  ▪  "MDB"  ▪  "DBF"  ▪  "DIF"  ▪  ...

Pravděpodobnost

Zabudovaná rozdělení

distributions=?*Distribution

Probability, Nprobability, Conditional probability (podmíněná pravděpodobnost), ProbabilityDistribution

Probability[event, distribution] (Pravděpodobnost[náhodný jev, rozdělení náhodné veličiny])

𝒟 ... EscscDEsc
\[Distributed] ... EscdistEsc

𝒟01=NormalDistribution[0,1];
{Probability[x≤3,x\[Distributed]𝒟01],NProbability[x≤3,x\[Distributed]𝒟01]}

(Conditional probability) Podmíněná pravděpodobnost

Místo obvyklého P(A|B) se používá P(A\[Conditioned]B)

\[Conditioned] ... EsccondEsc

{Probability[x>4\[Conditioned]x>3,x\[Distributed]𝒟01],NProbability[x>4\[Conditioned]x>3,x\[Distributed]𝒟01]}

Druhý příklad ilustruje skutečnost, že exponenciální rozdělení nemá paměť. V teorii spolehlivosti “Starý výrobek stejně dobrý jako nový.”

{Probability[x>4\[Conditioned]x>3,x\[Distributed]ExponentialDistribution[λ]]//FullSimplify,Probability[x>w+h\[Conditioned]x>w,x\[Distributed]ExponentialDistribution[λ],Assumptions->w>0&&h>0]}

Examples of multivariate distributions (příklady mnohorozměrných rozdělení)

?DirichletDistribution

PDF[DirichletDistribution[{α1,α2,α3,α4}],{x1,x2,x3}]

Beta rozdělení je specielní případ Dirichletova rozdělení:

{PDF[DirichletDistribution[{α1,α2}],{x}]//FullSimplify,PDF[BetaDistribution[α1,α2],x]}

Složitější příklad

{Probability[x+(y^2)+(z^2)<(3/10)\[Conditioned](x==1/7∧y==1/4),{x,y,z}\[Distributed]DirichletDistribution[{1,3,4,7}]],NProbability[x+(y^2)+(z^2)<(3/10)\[Conditioned](x==1/7∧y==1/4),{x,y,z}\[Distributed]DirichletDistribution[{1,3,4,7}]]}

Empirical distribution (empirické rozdělení)

r=RandomVariate[NormalDistribution[],10000];
{Probability[x^2+3x<11,x\[Distributed]r],Probability[x^2+3x<11,x\[Distributed]NormalDistribution[]]}
N[%]

Independent random variables (nezávislé náhodné veličiny)

Probability[1<x<5&&-3<y<8,{x\[Distributed]ExponentialDistribution[1],y\[Distributed]NormalDistribution[]}]

Probability[(x^2)+(y^2)<1,{x\[Distributed]UniformDistribution[{-1,1}],y\[Distributed]UniformDistribution[{-1,1}]}]

Nonliear conditions in events

Probability[(x^2<5&& (2x+3)<5)||(x^3+x>11),x\[Distributed]ExponentialDistribution[2]]
ToRadicals[%]
N[%]

Product distribution (the join distribution of independent components)
Sdružené rozdělení nezávislých náhodných veličin

𝒟product=ProductDistribution[ExponentialDistribution[1],NormalDistribution[]];
PDF[𝒟product,{x,y}]

Probability[1<x<5&&-3<y<8,{x,y}\[Distributed]𝒟product]

Tables of distributions

ℋ=Table[NormalDistribution[μ,(Sqrt[μ+1])],{μ,Range[5]-1}];
Plot[Map[PDF[#,x]&,ℋ],{x,-3,10}]

Derived distributions (odvozená rozdělení)

TransformedDistribution (transformace náhodných veličin)

𝒟trans=TransformedDistribution[(x^2)+(y^2),{x,y}\[Distributed]ProductDistribution[{NormalDistribution[],2}]]
𝒟trans2=TransformedDistribution[(x^2)+(y^2),{x\[Distributed]NormalDistribution[],y\[Distributed]NormalDistribution[]}]
{PDF[𝒟trans,x]//Evaluate,PDF[𝒟trans2,x],PDF[ChiSquareDistribution[2],x]}

Copula distribution

Motivation: The class of multivariate distributions based on univariate generalizations is rather poor. Copulas provide fairly wide range of distributions which are flexible enough. Here we deal only with the case of two-dimensional copulas. Suppose that C((Subscript[u, 1]),(Subscript[u, 2]))  is a distribution function of the pair of random variables taking on the values from the interval [0,1] (kernel of the copula), F and G distribution functions of random variables X and Y, respectively. Then C(F(x),G(y)) is the distribution function of the copula generated by the kernel C and marginal distributions F and G. In Mathematica more than 10 kernels.

Product copula

CDF[𝒞1=CopulaDistribution["Product",{UniformDistribution[],UniformDistribution[]}],{x,y}]

Plot3D[Evaluate@PDF[𝒞1, {x,y}], {x,-0.2,1.2}, {y,-0.2,1.2},PlotRange->All]

𝒟cop1=CopulaDistribution["Product",{GeometricDistribution[.3],PoissonDistribution[4]}];

DiscretePlot3D[PDF[𝒟cop1,{x,y}],{x,0,9},{y,0,9},ExtentSize->Full]

data=RandomVariate[𝒟cop1,(10^5)];
Histogram3D[data,{{0,9,1},{0,9,1}},"PDF"]

ProbabilityDistribution (Rozdělení definovaná uživatelem pomocí hustoty nebo distribuční funkce)

Enables to define users probability distribution by a density

𝒟prob=ProbabilityDistribution[(Sqrt[2]/Pi)(1/(1+x^4)),{x,-Infinity,Infinity}];

{Mean[𝒟prob],Variance[𝒟prob]}

Discrete distribution

𝒟discr=ProbabilityDistribution[2^(-k-1),{k,0,∞,1}];
Sum[PDF[𝒟discr,k],{k,0,∞}]

Estimation (Statistické odhady)

EstimatedDistribution

data=RandomVariate[GammaDistribution[1,2],(10^2)];
edist=EstimatedDistribution[data,GammaDistribution[α,β]]
Plot[{PDF[GammaDistribution[1,2],x],PDF[edist,x]},{x,0,10}]

EstimatedDistribution[data,GammaDistribution[α,β],ParameterEstimator->"MethodOfMoments"]

data=RandomVariate[BetaDistribution[2,3],1000];EstimatedDistribution[data,BetaDistribution[α,β]]EstimatedDistribution[data,BetaDistribution[α,β],ParameterEstimator->{"MaximumLikelihood",Method->"FindMaximum"}]

See 15 very interesting applications at the documantation of EstimatedDistribution.

LogLikelihood (věrohodnost)

LogLikelihood[NormalDistribution[μ,σ],Array[x,5]]

Empirical distribution

data=RandomVariate[NormalDistribution[],50];
𝒟emp=EmpiricalDistribution[data];
{DiscretePlot[PDF[𝒟emp,x],{x,data}],DiscretePlot[CDF[𝒟emp,x],{x,-4,4,.01}]}
{Moment[𝒟emp,2],Quantile[𝒟emp,0.95]}

Kernel estimator (jádrový odhad)

data=RandomVariate[NormalDistribution[],10^3];
𝒟=SmoothKernelDistribution[data];
Plot[PDF[𝒟,x],{x,-3,3}]

Choice of bandwidth (volba šířky okénka)

𝒟1=SmoothKernelDistribution[data,.05];
𝒟2=SmoothKernelDistribution[data,1.];
Table[Plot[PDF[d,x],{x,-3,3}],{d,{𝒟1,𝒟2}}]

Existuje rozsáhlá terie o volbě šířky okénka. Z praktického hlediska je nejlepší užít Manipulate

Manipulate[Plot[Evaluate@PDF[SmoothKernelDistribution[data,bw],x],{x,-4,4},Filling->Axis,PlotLabel->{"band width",bw},Ticks->None],{bw,Table[0.025 j,{j,20}]},SaveDefinitions->True]

Choice of the kernel

Table[Plot[Evaluate@PDF[SmoothKernelDistribution[data,Automatic,i],x],{x,-4,4},Filling->Axis,PlotLabel->i,Ticks->None],{i,{"Biweight","Cosine","Epanechnikov","Gaussian","Rectangular","SemiCircle","Triangular","Triweight"}}]

Testing (Testování statistických hypotéz)

DistributionFitTest (test-of-goodness of fit, test dobré shody)

Test normality

data=RandomVariate[NormalDistribution[],2500];
DistributionFitTest[data]

ℋ=DistributionFitTest[data,Automatic,"HypothesisTestData"];
ℋ["TestDataTable",All]

Comparison of data and fitted

Show[Histogram[data,Automatic,"ProbabilityDensity"],Plot[PDF[ℋ["FittedDistribution"],x],{x,-5,5}]]

Location test (Test parametru polohy)

LocationTest

Variance test (Test rozptylu, test rovnosti rozptylů dvou populací)

VarianceTest

Various problems

Tukey Λ distribution

PPCCPlot[data_,{λ_,a_,b_}]:=Module[{len=10,qq,tlq,dq,cor},qq=(Range[len]-1/2)/len;dq=Quantile[data,qq];
cor[la_?NumberQ]:=Correlation[dq,Quantile[TukeyLambdaDistribution[la],qq]];
Plot[cor[λ],{λ,a,b}]]

pC=Interpolation[Transpose[{n,Table[Probability[x≤0.05,x\[Distributed]i],{i,ℋ}]}],InterpolationOrder->1];

Probability[x≤3,x\[Distributed]NormalDistribution[]]

Create Population Pyramids

{age,male,female}=Transpose[WolframAlpha["us population pyramid",{{"Result",1},"ComputableData"}]];

PairedBarChart[male,female,ChartStyle->{{ColorData["Pastel",1],ColorData["Pastel",0.2]},None,None},ChartLabels->{Placed[{"Men","Women"},Top],None,Placed[age,Axis,Row[#[[1]],"-"]&]},BarSpacing->{2 10^6,0,0},PlotLabel->"United States Population by Age, 2008",ImageSize->550,BaseStyle->{FontFamily->"Helvetica"}]

Tests of goodness of fit

𝒟=ProductDistribution[{StudentTDistribution[0,1, 9],2}];
sample=RandomVariate[𝒟,10^3];
edist=EstimatedDistribution[sample,MultivariateTDistribution[{{1,0},{0,1}},ν]]
DistributionFitTest[sample,edist]
DistributionFitTest[sample,edist,Automatic]
DistributionFitTest[sample,edist,All]

DistributionFitTest[sample,edist,Automatic]
DistributionFitTest[sample,edist,All]

data=RandomVariate[NormalDistribution[],2500];
DistributionFitTest[data]
ℋ=DistributionFitTest[data,Automatic,"HypothesisTestData"];
ℋ["TestDataTable",All]
Show[Histogram[data,Automatic,"ProbabilityDensity"],Plot[PDF[ℋ["FittedDistribution"],x],{x,-5,5}]]
data=RandomVariate[𝒹=ParetoDistribution[1,2],100];
DistributionFitTest[data,𝒹,{"TestDataTable","AndersonDarling"}]
ProbabilityPlot[data,𝒹]

Kernel estimators

SmoothHistogram[{RandomReal[NormalDistribution[1,0.1],100]},ColorFunction->Function[{x,y},Hue[y]],Filling->Axis,FillingStyle->Automatic]

data=RandomVariate[NormalDistribution[],1000];{Histogram[data,{0.25},"PDF"],SmoothHistogram[data,PlotStyle->Thick]}
Show[%]

SmoothKernelDistribution

As the bandwidth approaches infinity, the estimate approaches the shape of the kernel:

data={-.5,0,.5};

𝒦={"Gaussian","Epanechnikov","Biweight","Triweight","Rectangular","Triangular","Cosine","SemiCircle"};

Table[SmoothHistogram[data,{200,k},Ticks->None,PlotLabel->k],{k,𝒦}]

Convert between Formal Moments

Manipulate[TraditionalForm[f[n]]->TraditionalForm@MomentConvert[f[n],g],{{f,Moment,"source"},{Moment,CentralMoment,FactorialMoment,Cumulant}},{{g,CentralMoment,"target"},{Moment,CentralMoment,FactorialMoment,Cumulant}},{{n,3},1,20,1}]

Pictures

Two densities with the same moments

Assemble graphics from diverse sources (combining two pictures)

http://www.wolfram.com/mathematica/new-in-8/enhanced-illustration-and-drawing-tools/assemble-graphics-from-diverse-sources.html

Sinus random points

x=RandomReal[10,1000];
y=Sin[x] RandomReal[1,1000];
data=Transpose[{x,y}];
ListPlot[data]

Keyboard characters

tutorial/StructuralElementsAndKeyboardCharacters

Comments

Podmíněná pravděděpodobnost P(A|B)=((P(A,B))/(P(B))).