Modelling Population with Stochastic Model and a Random Variable





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















So I have this stochastic model: N(t+1)=(1+r^3)N(t), where r is a random variable from a Normal Population with mean= -0.1 and standard deviation=0.2.



I'm trying to make a histogram from 1000 samples of this random that will model my population, and I've tried a few things to no avail.



So far I have:



import numpy as np 
import numpy.random as npr
import matplotlib.pyplot as plt
npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 0.1)
r = npr.normal(loc=2,scale=3, size=1000)

for t in tvec[:10]:
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


but that gives me an error.




IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices




Which tells me that I'm not able to take the random numbers I'm sampling and use them in my equation.



So, I'm wondering if there's a different way I could be going about this?



Thanks in advance!










share|improve this question























  • Could you point out the line which threw that error please?

    – Arthur-1
    Nov 29 '18 at 4:03











  • @Arthur-1 it was N[t+1]=(1+r**3)+N[t]

    – dejsdukes
    Nov 29 '18 at 4:06


















2















So I have this stochastic model: N(t+1)=(1+r^3)N(t), where r is a random variable from a Normal Population with mean= -0.1 and standard deviation=0.2.



I'm trying to make a histogram from 1000 samples of this random that will model my population, and I've tried a few things to no avail.



So far I have:



import numpy as np 
import numpy.random as npr
import matplotlib.pyplot as plt
npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 0.1)
r = npr.normal(loc=2,scale=3, size=1000)

for t in tvec[:10]:
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


but that gives me an error.




IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices




Which tells me that I'm not able to take the random numbers I'm sampling and use them in my equation.



So, I'm wondering if there's a different way I could be going about this?



Thanks in advance!










share|improve this question























  • Could you point out the line which threw that error please?

    – Arthur-1
    Nov 29 '18 at 4:03











  • @Arthur-1 it was N[t+1]=(1+r**3)+N[t]

    – dejsdukes
    Nov 29 '18 at 4:06














2












2








2








So I have this stochastic model: N(t+1)=(1+r^3)N(t), where r is a random variable from a Normal Population with mean= -0.1 and standard deviation=0.2.



I'm trying to make a histogram from 1000 samples of this random that will model my population, and I've tried a few things to no avail.



So far I have:



import numpy as np 
import numpy.random as npr
import matplotlib.pyplot as plt
npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 0.1)
r = npr.normal(loc=2,scale=3, size=1000)

for t in tvec[:10]:
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


but that gives me an error.




IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices




Which tells me that I'm not able to take the random numbers I'm sampling and use them in my equation.



So, I'm wondering if there's a different way I could be going about this?



Thanks in advance!










share|improve this question














So I have this stochastic model: N(t+1)=(1+r^3)N(t), where r is a random variable from a Normal Population with mean= -0.1 and standard deviation=0.2.



I'm trying to make a histogram from 1000 samples of this random that will model my population, and I've tried a few things to no avail.



So far I have:



import numpy as np 
import numpy.random as npr
import matplotlib.pyplot as plt
npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 0.1)
r = npr.normal(loc=2,scale=3, size=1000)

for t in tvec[:10]:
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


but that gives me an error.




IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices




Which tells me that I'm not able to take the random numbers I'm sampling and use them in my equation.



So, I'm wondering if there's a different way I could be going about this?



Thanks in advance!







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '18 at 3:54









dejsdukesdejsdukes

536




536













  • Could you point out the line which threw that error please?

    – Arthur-1
    Nov 29 '18 at 4:03











  • @Arthur-1 it was N[t+1]=(1+r**3)+N[t]

    – dejsdukes
    Nov 29 '18 at 4:06



















  • Could you point out the line which threw that error please?

    – Arthur-1
    Nov 29 '18 at 4:03











  • @Arthur-1 it was N[t+1]=(1+r**3)+N[t]

    – dejsdukes
    Nov 29 '18 at 4:06

















Could you point out the line which threw that error please?

– Arthur-1
Nov 29 '18 at 4:03





Could you point out the line which threw that error please?

– Arthur-1
Nov 29 '18 at 4:03













@Arthur-1 it was N[t+1]=(1+r**3)+N[t]

– dejsdukes
Nov 29 '18 at 4:06





@Arthur-1 it was N[t+1]=(1+r**3)+N[t]

– dejsdukes
Nov 29 '18 at 4:06












1 Answer
1






active

oldest

votes


















1














The error occurs because t is a float and initially has a value of 0.0. If you try N[0.0] you can reproduce the error. You can use integers rather than floats. Also, I changed r to r_values and zipped it to t so the loop works only on single values for r and t rather than an array for r.



import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 1)
r_values = npr.normal(loc=2,scale=3, size=10)

for t,r in zip(tvec, r_values):
#print(t,r)
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


enter image description here






share|improve this answer





















  • 1





    ah thank you! I appreciate it

    – dejsdukes
    Nov 29 '18 at 5:01












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531605%2fmodelling-population-with-stochastic-model-and-a-random-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The error occurs because t is a float and initially has a value of 0.0. If you try N[0.0] you can reproduce the error. You can use integers rather than floats. Also, I changed r to r_values and zipped it to t so the loop works only on single values for r and t rather than an array for r.



import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 1)
r_values = npr.normal(loc=2,scale=3, size=10)

for t,r in zip(tvec, r_values):
#print(t,r)
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


enter image description here






share|improve this answer





















  • 1





    ah thank you! I appreciate it

    – dejsdukes
    Nov 29 '18 at 5:01
















1














The error occurs because t is a float and initially has a value of 0.0. If you try N[0.0] you can reproduce the error. You can use integers rather than floats. Also, I changed r to r_values and zipped it to t so the loop works only on single values for r and t rather than an array for r.



import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 1)
r_values = npr.normal(loc=2,scale=3, size=10)

for t,r in zip(tvec, r_values):
#print(t,r)
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


enter image description here






share|improve this answer





















  • 1





    ah thank you! I appreciate it

    – dejsdukes
    Nov 29 '18 at 5:01














1












1








1







The error occurs because t is a float and initially has a value of 0.0. If you try N[0.0] you can reproduce the error. You can use integers rather than floats. Also, I changed r to r_values and zipped it to t so the loop works only on single values for r and t rather than an array for r.



import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 1)
r_values = npr.normal(loc=2,scale=3, size=10)

for t,r in zip(tvec, r_values):
#print(t,r)
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


enter image description here






share|improve this answer















The error occurs because t is a float and initially has a value of 0.0. If you try N[0.0] you can reproduce the error. You can use integers rather than floats. Also, I changed r to r_values and zipped it to t so the loop works only on single values for r and t rather than an array for r.



import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

npr.seed(101)

N = np.zeros(11)
N[0]=100
tvec = np.arange(0, 10, 1)
r_values = npr.normal(loc=2,scale=3, size=10)

for t,r in zip(tvec, r_values):
#print(t,r)
N[t+1]=(1+r**3)+N[t]

plt.hist(N)


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 4:49

























answered Nov 29 '18 at 4:36









DodgeDodge

1,50211023




1,50211023








  • 1





    ah thank you! I appreciate it

    – dejsdukes
    Nov 29 '18 at 5:01














  • 1





    ah thank you! I appreciate it

    – dejsdukes
    Nov 29 '18 at 5:01








1




1





ah thank you! I appreciate it

– dejsdukes
Nov 29 '18 at 5:01





ah thank you! I appreciate it

– dejsdukes
Nov 29 '18 at 5:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531605%2fmodelling-population-with-stochastic-model-and-a-random-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks