0
$\begingroup$

EDIT-1

This is a minimal example where an error occurs after 44538 samples generated in d1 upon importing as d2. Note that if d1 is used as parameter for SmoothKernelDistribution it is still okay, so there seems to be a problem with the import.

d1 = RandomVariate[NormalDistribution[0, 1], 100000];

Export["d1TEST.txt", d1];

d2 = Flatten[Import["d1TEST.txt", "Table"]];

\[ScriptCapitalD]1 = SmoothKernelDistribution[d2];

SmoothKernelDistribution::invldd: The input data SmoothKernelDistribution[{6617.53,7952.92,<<47>>,7810.37,<<99950>>}] should be a vector or a matrix of real numbers or a valid TemporalData object.


Original post

I have a code in which I write

empiricPDF1 = SmoothKernelDistribution[RandomVariable1Histogram];
d1 = RandomVariate[empiricPDF1, 100000];
isexpan = Transpose[ALPHAHATis].Simplify[PHI]; 
simis = Table[
   isexpan /. {z1 -> d1[[k]], z2 -> d2[[k]]}, {k, 1, nsim}];
timesample1 = simis[[1 ;; All, 688]]
\[ScriptCapitalD]1 = SmoothKernelDistribution[timesample1];

This takes a long time to run since it has to generate a random sample of a big size. I want to save the vector d1 so that I don't have to generate it time and again and also have a constant sample to study. I save the data as

Export["d1_2RVConflictBC.txt", d1];

And then, instead of generating d1, I load it as

d1 = Flatten[Import["d1_2RVConflictBC.txt", "Table"]];

Unfortunately, when I arrive to

\[ScriptCapitalD]1 = SmoothKernelDistribution[timesample1];

I get an error

SmoothKernelDistribution::invldd: The input data SmoothKernelDistribution[{6617.53,7952.92,<<47>>,7810.37,<<99950>>}] should be a vector or a matrix of real numbers or a valid TemporalData object.

which I don't understand. I don't get why it happens. As can be seen in the first attached figure, d1 is the same in both versions of the code and so is the value of time sample. So, I don't understand why the error mentioned above (and in the second attached figure) appears.

enter image description here

enter image description here

$\endgroup$
6
  • $\begingroup$ empiricPDF1 is not computing. Please add the definition of RandomVariable1Histogram? $\endgroup$
    – Syed
    Commented Dec 17, 2021 at 11:57
  • $\begingroup$ @Syed I am afraid that I load it from a file as RandomVariable1Histogram = Flatten[Import["RV1txt.txt", "Table"]];, it is a 50*1 vector $\endgroup$ Commented Dec 17, 2021 at 12:01
  • $\begingroup$ isexpan = Transpose[ALPHAHATis].Simplify[PHI]; is not evaluating either. Please add enough data to your post to create a minimal working example that is copy-paste-able. Also the data you have posted as a comment can be deleted after you have added it to your post. A presumptive answer to your question is that there is no difference in loading and generating data, if executing listA==listB returns True where listA is the generated data and the listB is the loaded data. $\endgroup$
    – Syed
    Commented Dec 17, 2021 at 12:07
  • $\begingroup$ @Syed Very well, I have added the example to the question. The most annoying thing is that it works if I write d1 = RandomVariate[NormalDistribution[0, 1], 1000]; $\endgroup$ Commented Dec 17, 2021 at 12:45
  • 2
    $\begingroup$ Up to 44538 samples, the output D2 is okay and then it throws an error and I certainly don't know why. I am on v12.2.0 and this could also be version dependent. $\endgroup$
    – Syed
    Commented Dec 17, 2021 at 13:11

1 Answer 1

3
$\begingroup$

It is always prudent to use the same format in Export and Import. Consider:

Export["d1TEST.txt", d1]

and

Export["d1TEST.txt", d1,"Table"]

do not export the same format.

For example:

d1={6.064691740996638*^-6, 1.}
Export["test1.txt",d1]

writes a file with:

6.064691740996638*^-6
1.

On the other hand:

Export["test2.txt",d1,"Table"]

writes:

6.064691740996638e-6
1.

Note the difference in the exponent. On import with the command:

Import["d1TEST.txt", "Table"]

6.064691740996638*^-6 is imported as a string, what creates the error.

$\endgroup$
3
  • $\begingroup$ Can you tell if OP’s fix is to merely just remove the “Table” part of their call to Import? $\endgroup$ Commented Dec 17, 2021 at 22:35
  • 1
    $\begingroup$ No, I would use "Table" both on Export and Import. $\endgroup$ Commented Dec 18, 2021 at 9:12
  • $\begingroup$ Actually, I most probably wouldn't use a text format for that purpose at all :-) But whatever format is used, it certainly is a good idea to read the same format that has been written... $\endgroup$ Commented Dec 18, 2021 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.