Hallo again,
Yes that was about the point I arrived at and I didn't like the *(mu + s)
construct (having a history of algol and pascal). This morning (European
time) I decided to do an other search and not stop reading on the second
page. than I found out (
http://questiontrack.com/how-to-substitute-symbol-for-matrix-using-sympy-and-numpy-1093001.html)
that lambdify has an option 'numpy' and that solves my issue. Could have
found that in the manual (5.32.6 Lambdify) although not very explicit.
I would also have liked to use mu = symarray('mu', n) but I don't see how
to get the attributes 'positive' etc implemented; the distribution needs
that for the sigma. So I leave it to this for the moment.
The resulting beautified version is attached.
Cheers, Janwillem
On Saturday, 31 August 2013 05:04:16 UTC+2, Matthew wrote:
>
> The first input to lambdify should be either a SymPy expression or a list
> of SymPy expressions. I don't know enough about lambdify to know if we
> support list of lists of sympy expressions.
>
> In general you won't be able to use lambdify with arbitrary n because the
> target expression (the second argument to lambdify) needs to already be
> evaluated.
>
> Building off of my previous example
>
> In [6]: result = simplify(E(prod(Xs)))
>
> In [7]: f = lambdify(mus+sigmas, result)
>
> In [8]: f(1,2,3,4,5,10,11,12,13,14)
> Out[8]: 120
>
> Or, if you need your current interface
>
> In [9]: numeric_mus = [1,2,3,4,5]
>
> In [10]: numeric_sigmas = [10, 11, 12, 13, 14]
>
> In [11]: f(*(numeric_mus + numeric_sigmas))
> Out[11]: 120
>
>
>
> On Fri, Aug 30, 2013 at 3:09 PM, Janwillem van Dijk
> <[email protected]<javascript:>
> > wrote:
>
>> T <https://groups.google.com/forum/#!topic/sympy/gRgNYef3oyc>hanks
>> Matthew, Is the following snippet of use? Yes and No.
>> The lines 5 and 6 are a useful tip; I missed the prod method, that is
>> nice. And yes for two N-distributions explicit solutions are to be found in
>> the litt and SymPy can find them as it can for longer products and also for
>> the uniform distribution which is also of use to me. However, I want both
>> the algebraic expression and numerical results. In the past lambdify has
>> helped me often and here it works also.
>>
>> My problem is that when I have PNs as the product of n distributions an
>> mu and sigma symbolic arrays of n means and stdevs then after:
>> MeanPns = E(PNs)
>> meanPNs = lambdify([mu, sigma], MeanPNs)
>> I cannot calculate the mean of the product of two (or more) Ns with
>> m=[1.0, 1.0] and s= [0.05, 0.15] by calling
>> average = meanPNs(m, s)
>> This gives an error saying that meanPNs expects 4 arguments an only 2 are
>> given.
>> So you need the intermediate step
>> ms = m + s
>> and than
>> average = meanPNs(*ms)
>> whereby meanPS gets the 4 Or rather 2n) arguments it wants.
>> This works but I do not consider it an elegant solution so I wondered
>> whether there is a more elegant solution.
>>
>> By the way, the purpose of it is to show in a course on uncertainty in
>> measurement that the Law of Propagation of Uncertainties (LPU) under
>> estimates the standard uncertainty and that the resulting distribution is
>> skewed in violation to the common assumption that the Central Limit Theorem
>> (CLT) would be valid. Showing this both through equations an numbers.
>>
>> Thanks again for paying attention, cheers, Janwillem
>>
>>
>> On Friday, 30 August 2013 06:00:21 UTC+2, Matthew wrote:
>>
>>> Is the following snippet of use?
>>>
>>> In [1]: from sympy.stats import *
>>>
>>> In [2]: n = 5
>>>
>>> In [3]: mus = [Symbol('mu'+str(i), bounded=True) for i in range(n)]
>>>
>>> In [4]: sigmas = [Symbol('sigma'+str(i), positive=True) for i in
>>> range(n)]
>>>
>>> In [5]: Xs = [Normal('X'+str(i), mu, sigma) for i, (mu, sigma) in
>>> enumerate(zip(mus, sigmas))]
>>>
>>> In [6]: E(prod(Xs))
>>> << Big Scary Thing >>
>>>
>>> In [7]: simplify(E(prod(Xs)))
>>> Out[7]: μ₀⋅μ₁⋅μ₂⋅μ₃⋅μ₄
>>>
>>> Please note that you shouldn't have to switch down to numerics for this
>>> problem. There is a clean analytic form.
>>>
>>>
>>>
>>> On Thu, Aug 29, 2013 at 2:01 PM, Janwillem van Dijk <[email protected]
>>> > wrote:
>>>
>>>> I have problems lambdifying a symbolic function that needs array
>>>> arguments.
>>>> The problem I want to solve is to find the statistics of the product of
>>>> Normal distributions. Attached is an example that works and even yields
>>>> the
>>>> correct answers. However, I think that the part where the symbolic
>>>> versions
>>>> are lambdified into Python callable functions can't win a beauty contest.
>>>> For example a part of the class :
>>>>
>>>> self.MeanNN = E(NN)
>>>>
>>>> self.meanNN = lambdify([mu, s], self.MeanNN)
>>>>
>>>> def meanOfNs(self, mu, s):
>>>>
>>>> mus = mu + s
>>>>
>>>> return self.meanNN(*mus)
>>>>
>>>> Than called as:
>>>>
>>>> Nn = ProductOfNormals(n)
>>>>
>>>> print('Mean: ', Nn.meanOfNs(mu, s))
>>>>
>>>> Where mu and s are lists of length n
>>>>
>>>>
>>>> In case the arguments are spelled out it looks nicer but I want the
>>>> flexibility of variable number of distributions in the product.
>>>>
>>>> e.g.:
>>>>
>>>> self.MeanNN = E(NN)
>>>>
>>>> self.meanNN = lambdify([mu1, s1, mu2, s2], self.MeanNN)
>>>>
>>>> Than:
>>>>
>>>> NN = ProductNormalNormal()
>>>>
>>>> print('Mean: ', NN.meanNN(mu0, s0, mu1, s1))
>>>>
>>>> Where all parms are scalar
>>>>
>>>>
>>>> Any tips on how to get a more elegant solution are welcome!!
>>>>
>>>> The complete example is attached. I am using SymPy 0.7.3 on Python
>>>> 2.7.4 on Linux.
>>>>
>>>> Cheers, Janwillem
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "sympy" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to sympy+un...@**googlegroups.com.
>>>> To post to this group, send email to [email protected].
>>>>
>>>> Visit this group at
>>>> http://groups.google.com/**group/sympy<http://groups.google.com/group/sympy>
>>>> .
>>>> For more options, visit
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "sympy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] <javascript:>
>> .
>> Visit this group at http://groups.google.com/group/sympy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.