Skip to main content
7 of 8
added 30 characters in body
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

How do you pattern match a DataDistribution

I have a function,

f[dist_, samp_]:=somework[dist, samp]

that I want to return Null or zero if passed a null distribution. I can't find a way to test for a difference between Null and Data distribution: none of ===, =!=, SameQ and == can identify them.

How can I tell if I'm being passed a Null value to handle it correctly?

f[dist_, samp_]:=something[dist, samp] /; TrueQ[dist==DataDistribution]

Seems to be close to what I want.

EDIT: Edited for some clarity.

Here's my function. If probKern is a Null, I want to return a Null, otherwise, I want to calculate the PDF for the sample, with the distribution. I can't quite get a handle on matching the types to make this work though.

calcPDF[probKern_, samp_] := 
 Null /; TrueQ[probKern != DataDistribution]

calcPDF[probKern_Symbol, samp_] := 
 PDF[probKern, samp] /; TrueQ[probKern == DataDistribution]

edit: Okay, I'm still having trouble with this. Here's what I have:

calcPDF[probKern_DataDistribution, samp_] := PDF[probKern, samp]  
calcPDF[Except[_DataDistribution, probkern_?DistributionParameterQ], samp_] := -1 
calcPDF[_] := $Failed

Given a data distribution and a value it works fine.

In[38]:= calcPDF[data, 3]

Out[38]= 0.007500611755

Given two numbers, it produces the symbolic value, rather than the -1 I was expect. (The -1 is just a placeholder)

In[39]:= calcPDF[3, 3]

Out[39]= calcPDF[3, 3]
user4860