user4099407
user4099407

Reputation:

How do I add an attribute name to an array?

I was wondering how to add an attribute to an array.

When I do

errors1 = pm.Uniform('errors', 0, 100, size = 7)

the name 'errors' is added.

but then when I do

 errors2 = [errors1[1], errors1[3], errors1[6]]

I have no idea how to add that name, and because I didn't add it, when I try to create a model with errors2, I get an error, saying that it doesn't have an attribute name.

Here's my full code:

import pymc as pm
from matplotlib import pyplot as plt
from pymc.Matplot import plot as mcplot
import numpy as np
from matplotlib import rc

first_res = [-27.020,3.570,8.191,9.898,9.603,9.945,10.056]
second_res = [18.752, 12.450, 11.832]

v1 = pm.Uniform('v1', -30, 15)
v2 = pm.Uniform('v2', 0, 20)

errors1 = pm.Uniform('errors', 0, 100, size = 7)
errors2 = [errors1[1], errors1[3], errors1[6]] # doesn't have an attribute name

taus1 = 1/(errors1 ** 2)
taus2 = [taus1[1], taus1[3], taus1[6]]

first_dist = pm.Normal('first_dist', mu = v1, tau = taus1, value = first_res, observed = True)
second_dist= pm.Normal('second_dist', mu = v2, tau = taus2, value = second_res, observed = True)

model=pm.Model([first_dist, second_dist, errors1, taus1, v1, v2])
mcmc=pm.MCMC(model)
mcmc.sample(20000,10000)

mcplot(mcmc.trace("errors"))

plt.figure() 

model2=pm.Model([second_dist, errors2, taus2, v2]) # since errors2 doesn't have an attribute name, I get an error
mcmc2=pm.MCMC(model2)
mcmc2.sample(20000,10000)

mcplot(mcmc2.trace('second_dist'))

Upvotes: 1

Views: 377

Answers (2)

Ramon Crehuet
Ramon Crehuet

Reputation: 4017

Just to clarify on some python concepts, the way you define errors2, it is a python list. lists do not have any name attribute. The attributes that have the elements of a list are not the same attributes of the list as a whole (as an object). In fact, arrays do not have any name attribute either, and if errors1 does have a name attribute it is because it is pymc object, a distribution.

I think you have to define errors2 in more detail. Is it a uniform distribution? What is its relation to errors1, not in python, but statistically?

Upvotes: 0

Abraham D Flaxman
Abraham D Flaxman

Reputation: 2979

PyMC2 has some magic that lets us operate on nodes like errors1 as if they are numpy arrays, but it doesn't always work as you might expect. In cases like this, you can define a deterministic node explicitly with the pm.Lambda, e.g.

errors2 = pm.Lambda('errors2', lambda errors1=errors1: [errors1[1],
                                                        errors1[3], 
                                                        errors1[6]])

Upvotes: 1

Related Questions