5
$\begingroup$

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]
$\endgroup$
4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I guess I should have asked this also, how do I reconstruct the input form? Just read it back into a variable? $\endgroup$ – user4860 Dec 6 '12 at 3:44
  • $\begingroup$ 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. $\endgroup$ – user4860 Dec 6 '12 at 4:01
  • $\begingroup$ I got it. You need to do ToString within the ToExpression function. inform = KernelMixtureDistribution[Flatten[sampleData], MaxMixtureKernels -> All]; // InputForm; ToExpression[ ToString[inform] , InputForm] Works. $\endgroup$ – user4860 Dec 6 '12 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy