Python numpy练习,纯英文ipynb作业26题,100%正确答案(付费)

#%% md

Assigment 2

Instructions: This problem set should be done individually

Answer each question in the designated space below

After you are done. save and upload in blackboard.

Please check that you are submitting the correct file. One way to avoid mistakes is to save it with a different name.

#%% md

Write your name and My email

Please write names below

#%% md

Perusall

click on the “New Facts” link in assigments. This will direct you to Perusall where you will have instructions of what to do in a box that will pop up in the bottom left corner of your screen.

#%% md

Exercises

#%% md

1.Import the numpy package under the name np

#%%

import numpy as np

#%% md

2.Create a 3d numpy array using the following list. Label it x_3d

[[[1, 2, 3], [4, 5, 6]], [[10, 20, 30], [40, 50, 60]]]

#%%

your code here

print(x_3d)
type(x_3d)

#%% md

3.Use index to select the number 5, 10, and 60 respectively by indexing appropriately.

Building an understanding of indexing means working through this
type of operation several times – without skipping steps!

#%%

select 5 and print

select 10 and print

select 60 and print

#%% md

4. Exercise

Look at the 2-dimensional array x_2d below

Does the inner-most index correspond to rows or columns? What does the
outer-most index correspond to?

Write your thoughts.

#%%

x_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x_2d)

#%% md

Your answer:

#%% md

#%% md

5. What would you do to extract the array [[5, 6], [50, 60]] from x_3d?

#%%

#%% md

6. Create a zero vector of size 10

#%%

#%% md

7. Create a zero vector of size 10 but the fifth value which is 1

#%%

#%% md

8. Do you recall what multiplication by an integer did for lists?

How does mulitplying an array is different? Please create a list and an array and demonstrate the difference. Then explain below

#%%

#code here

#%% md

Explain here:

#%% md

9. Exercise

Let’s revisit a bond pricing example we saw in Control flow.

Recall that the equation for pricing a bond with coupon payment $ C $,
face value $ M $, yield to maturity $ i $, and periods to maturity
$ N $ is

KaTeX parse error: No such environment: align* at position 8: \begin{̲a̲l̲i̲g̲n̲*̲}̲ P &= \left…

In the code cell below, we have defined variables for i, M and C.

You have two tasks:

  1. Define a numpy array N that contains all maturities between 1 and 10 (hint look at the np.arange function).
  2. Define a numpy array CF that contains all cash flows from year 1 to year 10 of the bond.
  3. Using the equation above, determine the discounted value for each cash flow and comupte the bond price.

#%%

i = 0.03
M = 100
C = 5

year to maturity =10

Define N array here

Define CF array here

price bonds here

#%% md

10.Exercise

Consider the polynomial expression

p ( x , a ) = a 0 + a 1 x + a 2 x 2 + ⋯ a N x N = ∑ n = 0 N a n x n (1) p(x,a) = a_0 + a_1 x + a_2 x^2 + \cdots a_N x^N = \sum_{n=0}^N a_n x^n \tag{1} p(x,a)=a0+a1x+a2x2+aNxN=n=0Nanxn(1)

Now write a new function that does the same job, but uses NumPy arrays and array operations for its computations, rather than any form of Python loop.

(Such functionality is already implemented as np.poly1d, but for the sake of the exercise don’t use this class)

Demonstrate it works using a vector of a=[0.2,0.3,0.1,0.5,0.7] and x=0.3

#%%

your code here

#%% md

11.Create a vector array and Reverse it (first element becomes last)

#%%

#%% md

12.Create a 3x3 matrix with values ranging from 0 to 8

#%%

#%% md

13.Create a 3x3 identity matrix

#%%

#%% md

14.Create a 10x10 array with random values and find the minimum and maximum values

#%%

#%% md

15.Create and normalize a 5x5 random matrix so that all its entries add up to 1

#%%

#%% md

16.Create a vector of size 10 with values ranging from 0 to 1, both exclusive (exclude 0 and 1)

#%%

#%% md

17.How to find the closest value (to a given scalar) in a vector?

#%%

#%% md

18.Build a 4 by 4 matrix with any numbers of your choosing, then Subtract the mean of each row of a matrix

#%%

#%% md

19.Sort the matrix you build above according to column 3

#%%

#%% md

20.Build a function that finds the nearest value in a array to a given value. Demonstrate that your function works for a particular array and number of your choosing.

#%%

#%% md

21.Plot the function

f ( x ) = cos ⁡ ( π θ x ) exp ⁡ ( − x ) f(x) = \cos(\pi \theta x) \exp(-x) f(x)=cos(πθx)exp(x)

over the interval $ [0, 5] $ for each $ \theta $ in np.linspace(0, 2, 10).

Place all the curves in the same figure.

The output should look like this

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qb32txoN-1623246786230)(https://python-programming.quantecon.org/_static/lecture_specific/matplotlib/matplotlib_ex1.png)]

#%%

your code here

#%% md

22.Alice is a stock broker who owns two types of assets: A and B. She owns 100 units of asset A and 50 units of asset B.

The current interest rate is 5%.
Each of the A assets have a remaining duration of 6 years and pay
\$1500 each year, while each of the B assets have a remaining duration
of 4 years and pay \$500 each year.

Alice would like to retire if she
can sell her assets for more than \$500,000. Use vector addition, scalar
multiplication, and dot products to determine whether she can retire.

Hint: Use the interest rate to compute the value of each asset, then compute the overall value of Alice portfolio

#%%

your code here

#%% md

23.Which of the following operations will work and which will create errors because of size issues?

#%% md

Simply type next to each of operations “works” or “doesn’t work” without running the code.

x1 @ x2
x2 @ x1
x2 @ x3
x3 @ x2
x1 @ x3
x4 @ y1
x4 @ y2
y1 @ x4
y2 @ x4

#%%

x1 = np.reshape(np.arange(6), (3, 2))
x2 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
x3 = np.array([[2, 5, 2], [1, 2, 1]])
x4 = np.ones((2, 3))

y1 = np.array([1, 2, 3])
y2 = np.array([0.5, 0.5])

#%% md

Test out your intuitions in the code cell below

#%%

test your intuitions here

#%% md

24.Wikipedia and other credible statistics sources tell us that the mean and variance of the Uniform(0, 1) distribution are (1/2, 1/12) respectively.

How could we check whether the numpy random numbers are consistent with these values
values? Describe below

#%% md

#%% md

25.Now demonstrate that this is indeed true by implementing the code you described above

#%%

your code here

#%% md

26.Exercise

Assume you have been given the opportunity to choose between one of three financial assets:

You will be given the asset for free, allowed to hold it indefinitely, and keep all the payoffs.

Also assume the assets’ payoffs are distributed as follows:

  1. Normal with $ \mu = 10, \sigma = 5 $
  2. Gamma with $ k = 5.3, \theta = 2 $
  3. Gamma with $ k = 5, \theta = 2 $

Use scipy.stats to answer the following questions:

  • Which asset has the highest average returns?
  • Which asset has the highest median returns?
  • Which asset has the lowest coefficient of variation (standard deviation divided by mean)?
  • Which asset would you choose? Why? (Hint: There is not a single right answer here. Be creative
    and express your preferences. You can plot the density functions to have a broader sense of these distributions)

#%%

your code here

#%% md

Assigment 2

Instructions: This problem set should be done individually

Answer each question in the designated space below

After you are done. save and upload in blackboard.

Please check that you are submitting the correct file. One way to avoid mistakes is to save it with a different name.

#%% md

Write your name and simon email

Please write names below

#%% md

Perusall

click on the “New Facts” link in assigments. This will direct you to Perusall where you will have instructions of what to do in a box that will pop up in the bottom left corner of your screen.

#%% md

Exercises

#%% md

1.Import the numpy package under the name np

#%%

import numpy as np

#%% md

2.Create a 3d numpy array using the following list. Label it x_3d

[[[1, 2, 3], [4, 5, 6]], [[10, 20, 30], [40, 50, 60]]]

#%%

your code here

print(x_3d)
type(x_3d)

#%% md

3.Use index to select the number 5, 10, and 60 respectively by indexing appropriately.

Building an understanding of indexing means working through this
type of operation several times – without skipping steps!

#%%

select 5 and print

select 10 and print

select 60 and print

#%% md

4. Exercise

Look at the 2-dimensional array x_2d below

Does the inner-most index correspond to rows or columns? What does the
outer-most index correspond to?

Write your thoughts.

#%%

x_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x_2d)

#%% md

Your answer:

#%% md

#%% md

5. What would you do to extract the array [[5, 6], [50, 60]] from x_3d?

#%%

#%% md

6. Create a zero vector of size 10

#%%

#%% md

7. Create a zero vector of size 10 but the fifth value which is 1

#%%

#%% md

8. Do you recall what multiplication by an integer did for lists?

How does mulitplying an array is different? Please create a list and an array and demonstrate the difference. Then explain below

#%%

#code here

#%% md

Explain here:

#%% md

9. Exercise

Let’s revisit a bond pricing example we saw in Control flow.

Recall that the equation for pricing a bond with coupon payment $ C $,
face value $ M $, yield to maturity $ i $, and periods to maturity
$ N $ is

KaTeX parse error: No such environment: align* at position 8: \begin{̲a̲l̲i̲g̲n̲*̲}̲ P &= \left…

In the code cell below, we have defined variables for i, M and C.

You have two tasks:

  1. Define a numpy array N that contains all maturities between 1 and 10 (hint look at the np.arange function).
  2. Define a numpy array CF that contains all cash flows from year 1 to year 10 of the bond.
  3. Using the equation above, determine the discounted value for each cash flow and comupte the bond price.

#%%

i = 0.03
M = 100
C = 5

year to maturity =10

Define N array here

Define CF array here

price bonds here

#%% md

10.Exercise

Consider the polynomial expression

p ( x , a ) = a 0 + a 1 x + a 2 x 2 + ⋯ a N x N = ∑ n = 0 N a n x n (1) p(x,a) = a_0 + a_1 x + a_2 x^2 + \cdots a_N x^N = \sum_{n=0}^N a_n x^n \tag{1} p(x,a)=a0+a1x+a2x2+aNxN=n=0Nanxn(1)

Now write a new function that does the same job, but uses NumPy arrays and array operations for its computations, rather than any form of Python loop.

(Such functionality is already implemented as np.poly1d, but for the sake of the exercise don’t use this class)

Demonstrate it works using a vector of a=[0.2,0.3,0.1,0.5,0.7] and x=0.3

#%%

your code here

#%% md

11.Create a vector array and Reverse it (first element becomes last)

#%%

#%% md

12.Create a 3x3 matrix with values ranging from 0 to 8

#%%

#%% md

13.Create a 3x3 identity matrix

#%%

#%% md

14.Create a 10x10 array with random values and find the minimum and maximum values

#%%

#%% md

15.Create and normalize a 5x5 random matrix so that all its entries add up to 1

#%%

#%% md

16.Create a vector of size 10 with values ranging from 0 to 1, both exclusive (exclude 0 and 1)

#%%

#%% md

17.How to find the closest value (to a given scalar) in a vector?

#%%

#%% md

18.Build a 4 by 4 matrix with any numbers of your choosing, then Subtract the mean of each row of a matrix

#%%

#%% md

19.Sort the matrix you build above according to column 3

#%%

#%% md

20.Build a function that finds the nearest value in a array to a given value. Demonstrate that your function works for a particular array and number of your choosing.

#%%

#%% md

21.Plot the function

f ( x ) = cos ⁡ ( π θ x ) exp ⁡ ( − x ) f(x) = \cos(\pi \theta x) \exp(-x) f(x)=cos(πθx)exp(x)

over the interval $ [0, 5] $ for each $ \theta $ in np.linspace(0, 2, 10).

Place all the curves in the same figure.

The output should look like this

https://python-programming.quantecon.org/_static/lecture_specific/matplotlib/matplotlib_ex1.png

#%%

your code here

#%% md

22.Alice is a stock broker who owns two types of assets: A and B. She owns 100 units of asset A and 50 units of asset B.

The current interest rate is 5%.
Each of the A assets have a remaining duration of 6 years and pay
\$1500 each year, while each of the B assets have a remaining duration
of 4 years and pay \$500 each year.

Alice would like to retire if she
can sell her assets for more than \$500,000. Use vector addition, scalar
multiplication, and dot products to determine whether she can retire.

Hint: Use the interest rate to compute the value of each asset, then compute the overall value of Alice portfolio

#%%

your code here

#%% md

23.Which of the following operations will work and which will create errors because of size issues?

#%% md

Simply type next to each of operations “works” or “doesn’t work” without running the code.

x1 @ x2
x2 @ x1
x2 @ x3
x3 @ x2
x1 @ x3
x4 @ y1
x4 @ y2
y1 @ x4
y2 @ x4

#%%

x1 = np.reshape(np.arange(6), (3, 2))
x2 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
x3 = np.array([[2, 5, 2], [1, 2, 1]])
x4 = np.ones((2, 3))

y1 = np.array([1, 2, 3])
y2 = np.array([0.5, 0.5])

#%% md

Test out your intuitions in the code cell below

#%%

test your intuitions here

#%% md

24.Wikipedia and other credible statistics sources tell us that the mean and variance of the Uniform(0, 1) distribution are (1/2, 1/12) respectively.

How could we check whether the numpy random numbers are consistent with these values
values? Describe below

#%% md

#%% md

25.Now demonstrate that this is indeed true by implementing the code you described above

#%%

your code here

#%% md

26.Exercise

Assume you have been given the opportunity to choose between one of three financial assets:

You will be given the asset for free, allowed to hold it indefinitely, and keep all the payoffs.

Also assume the assets’ payoffs are distributed as follows:

  1. Normal with $ \mu = 10, \sigma = 5 $
  2. Gamma with $ k = 5.3, \theta = 2 $
  3. Gamma with $ k = 5, \theta = 2 $

Use scipy.stats to answer the following questions:

  • Which asset has the highest average returns?
  • Which asset has the highest median returns?
  • Which asset has the lowest coefficient of variation (standard deviation divided by mean)?
  • Which asset would you choose? Why? (Hint: There is not a single right answer here. Be creative
    and express your preferences. You can plot the density functions to have a broader sense of these distributions)

#%%

your code here

![在这里插入图片描述](https://img-blog.csdnimg.cn/2021060921541226.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwODExNjgy,size_16,color_FFFFFF,t_70

部分示例100%准确
在这里插入图片描述
在这里插入图片描述

联系VX vx_xuxx

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是狮子搏兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值