Can I store a DataDistribution in a database?

5

I have a DataDistribution

data = KernelMixtureDistribution[RandomVariate[NormalDistribution[], 10^5]] 

that takes a long time to calculate because of database operations. I don't need to constantly recalculate the distribution, so I'd like to store it in a database, and recalculate it at intervals.

The documentation for SQLBinary refers to ExportString, which seems focused on graphics and doesn't have a generic format. Nor does StringTake work on a DataDistribution.

So, how can I store a DataDistribution in an SQL database?

EDIT:

Thank you again Andy Ross!

Here's my solution in the end. creating a string version of a data distribution.

inform = kern // InputForm;
ToString[inform]

Then I save it into an SQL database with SQLInsert.

Next, to extract it back to usable form:

ToExpression[ ToString[sqlkern] , InputForm]

user4860

Posted 2012-12-06T03:06:07.353

Reputation:

Answers

4

The internals of the DataDistribution are necessary for reconstructing it. I don't know much about database connectivity but Export does the right thing.

Export["temp.txt", KernelMixtureDistribution[Range[10]]];

Since I chose a .txt file Import comes back with a string.

Import["temp.txt"] // Head

(*String*)

In order to compute with it needs to be an expression.

dist = ToExpression[Import["temp.txt"]];

Mean[dist]

(* 11/2 *)

I chose the .txt file to show this potential issue since you may need to do something similar when bringing it back from a database.

Andy Ross

Posted 2012-12-06T03:06:07.353

Reputation: 18 640

I guess I should have asked this also, how do I reconstruct the input form? Just read it back into a variable? – None – 2012-12-06T03:44:36.847

Hrm. //InputForm seems to do the right thing in making a nice text format version of the Distribution. I can save that easily to a database. But I still can't figure out how to reconstruct a working distribution from that. – None – 2012-12-06T04:01:56.693

I got it. You need to do ToString within the ToExpression function.

inform = KernelMixtureDistribution[Flatten[sampleData], MaxMixtureKernels -> All]; // InputForm;

ToExpression[ ToString[inform] , InputForm]

Works. – None – 2012-12-06T04:44:24.880