Skip to main content
8 of 8
added 1071 characters in body
user avatar
user avatar

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]

EDIT dec 5th: Running calcPDF by hand seems to result in correct results, but now, when it is threaded over data, the result is a single result of whatever you return in the Except case. There's two sets of distributions I'm running over the sample data, so the final result is a list of two Nulls (or -1's).

If you remove the Except case, you get closer to the expected result - two lists 205 elements long, containing PDF's for entries with matching Distributions, and PDF[Null, value] for those without.

I'm confused.

Clear[calcPDF] (*calcPDF[probKern_, samp_]:=PDF[probKern, samp]; *) calcPDF[probKern_DataDistribution, samp_] := PDF[probKern, samp]; calcPDF[Except[DataDistribution], samp] := Null;

(* Pass a list of samples, and a list of distributions's *)

applyKernel[lsamples_, kernel_] := Thread[calcPDF[kernel, lsamples]];

(* Pass a list of samples, and a list of two lists of distribution's *)

predClass[samples_, kernels_] := Map[applyKernel[samples, #] &, kernels]; predPDF = predClass[predictionSamples[[1]], kernels]

user4860