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

2023-11-15

#%% 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • java获取文件夹下所有图片_【windows技巧】快速获取文件夹内所有文件名称列表...

    转自 百度经验 https jingyan baidu com article 3aed632e3917c870108091d1 html 1 打开一个文件夹 gt 2 新建一个TXT文档 将名字命名为 文档列表 gt 3 在TXT文档里输
  • c++什么时候生成默认拷贝构造函数

    需要默认拷贝构造函数原因 如果不提供默认拷贝构造函数 编译器会按照位拷贝进行拷贝 位拷贝指的是按字节进行拷贝 有些时候位拷贝出现的不是我们预期的行为 会取消一些特性 以下是需要默认拷贝构造函数的必要条件 1 类成员也是一个类 该成员类有拷贝
  • 如何实现无线网卡上外网+有线上内网=同时上网

    网上那些花里胡哨的 一顿操作然并卵 已补充成功操作详情 请翻到最后面查看 前面内容请忽视 至少我现在根本实现不了 但是会比拔插网线换来换去方便些 记录下 需要的自取 实例 一 开始 1 管理员打开cmd命令 2 route print 查看
  • chatgpt赋能python:怎么让Python执行完不关闭的SEO

    怎么让Python执行完不关闭的SEO 作为一名有十年Python编程经验的工程师 我深知Python在SEO技术中的重要性 然而 很多人可能不知道如何让Python执行完任务后不关闭 这将会影响我们的SEO效果 因此 在这篇文章中 我将向
  • xlwt:ValueError: column index (256) not an int in range(256)

    xlwt最大列只支持255列 超过范围会报错 可以考虑用xlsxwriter
  • 安卓已死?毕业一年萌新的Android大厂面经,年薪超过80万!

    前言 最近我一直在面试高级工程师 不管初级 高级 程序员 我想面试前 大家刷题一定是是少不了吧 我也一样 我在网上找了很多面试题来看 最近又赶上跳槽的高峰期 好多粉丝 都问我要有没有最新面试题 索性 我就把我看过的和我面试中的真题 及答案都
  • layer.msg 的time 时间停留问题

    layer msg 同上 icon 1 time 2000 2秒关闭 如果不配置 默认是3秒 function do something time 属性为弹框停留时间 单位为毫秒 tipsMore 属性为是否同时显示多个弹框 true为显示
  • 顺序表与数组

    顺序表是在计算机内存中以数组的形式保存的线性表 顺序表是指用一组地址连续的存储单元依次存储数据元素的线性结构 线性表采用顺序存储的方式存储就称之为顺序表 顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中 线性表采用指针链接
  • Python 如何将字符串转为字典

    在工作中遇到一个小问题 需要将一个 python 的字符串转为字典 比如字符串 user info name john gender male age 28 我们想把它转为下面的字典 user dict name john gender m
  • Kubespray-offline v2.21.0-1 下载 Kubespray v2.22.1 离线部署 kubernetes v1.25.6

    文章目录 1 目标 2 预备条件 3 vcenter 创建虚拟机 4 系统初始化 4 1 配置网卡 4 2 配置主机名 4 3 内核参数 5 打快照 6 安装 git 7 配置科学 8 安装 docker 9 下载介质 9 1 下载安装 d
  • Linux文件编程常用函数详解——exit()和_exit()函数

    两个函数的区别
  • MongoDB未授权访问漏洞复现及加固

    说明 仅供技术学习交流 请勿用于非法行为 否则后果自负 0x01 漏洞简述 MongoDB是一个介于关系数据库和非关系数据库之间的产品 是非关系数据库当中功能最丰富 最像关系数据库的 它支持的数据结构非常松散 是类似json的bson格式
  • K8s Pod 控制器(一)

    K8s workload architecture 一 RC RS 控制器 控制Pod 使Pod拥有自愈 多副本 扩缩容的能力 RC的定义包括如下几个部分 1 Pod期待的副本数 replicas 2 用于筛选目标Pod的Label Sel
  • vscode常用快捷键使用

    Ctrl K S 全部保存 Save All Ctrl S 保存 Save ctrl f 搜索 alt 方向键右 跳转到定义 F12 转到定义 Go to Definition alt 方向键左 返回跳转 F1 或 Ctrl Shift P
  • 【华为OD机试真题 python】最大平分数组【2022 Q4

    题目描述 最大平分数组 给定一个数组nums 可以将元素分为若干个组 使得每组和相等 求出满足条件的所有分组中 最大的平分组个数 输入描述 第一行输入 m 接着输入m个数 表示此数组 数据范围 1 lt M lt 50 1 lt nums
  • 漫画:什么是中台?

    没有中台的时代 在传统IT企业 项目的物理结构是什么样的呢 无论项目内部的如何复杂 都可分为 前台 和 后台 这两部分 什么是前台 首先 这里所说的 前台 和 前端 并不是一回事 所谓前台即包括各种和用户直接交互的界面 比如web页面 手机
  • CH1-Android基础入门

    文章目录 目标 一 资源的管理与使用 1 1 图片资源 1 2 主题和样式资源 1 3 布局资源 1 4 字符串资源 1 5 颜色资源 定义颜色值 1 6 尺寸资源 Android支持的尺寸单位 二 程序调试 2 1 单元测试 2 2 注意
  • hive中解决中文乱码

    一 个人初始开发环境的基本情况以及Hive元数据库说明 hive的元数据库改成了mysql 安装完mysql之后也没有进行其它别的设置 hive site xml中设置元数据库对应的配置为 jdbc mysql crxy99 3306 hi
  • Vue使用高德地图搜索功能

    下载依赖 yarn add amap amap jsapi loader 2 初始化高德地图 设置key和秘钥
  • Python numpy练习,纯英文ipynb作业26题,100%正确答案(付费)

    md Assigment 2 Instructions This problem set should be done individually Answer each question in the designated space be