Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01%20python/03.05%20%EB%82%9C%EC%88%98%20%EB%B0%9C%EC%83%9D%EA%B3%BC%20%EC%B9%B4%EC%9A%B4%ED%8C%85 #61

Open
utterances-bot opened this issue May 28, 2022 · 7 comments

Comments

@utterances-bot
Copy link

3.5 난수 발생과 카운팅 — 데이터 사이언스 스쿨

https://datascienceschool.net/01%20python/03.05%20%EB%82%9C%EC%88%98%20%EB%B0%9C%EC%83%9D%EA%B3%BC%20%EC%B9%B4%EC%9A%B4%ED%8C%85.html

Copy link

coin=np.array([1,0])
ex1=np.random.choice(coin,10,replace=True,p=[0.5,0.5])
print(ex1)
dice=np.array([1,2,3,4,5,6])
ex2=np.random.choice(dice,100,replace=True,p=[1/6,1/6,1/6,1/6,1/6,1/6])
print(ex2)

np.random.seed(0)
a=np.random.randn(250)
percentage=a/100
price=[10000+percentage[0]*10000]
for i in range(1,len(percentage)):
Dprice=price[i-1]+price[i-1]*percentage[i]
price.append(Dprice)
print(price)

Copy link

lysnjn commented Aug 3, 2022

3.5.1
1)
coin = np.random.choice(2, 10, p=[0.5, 0.5])
coin
2)
dice = np.random.randint(1, 7, 100)
dice.mean()

3.5.2
n = 10000
x = np.random.randn(250)
for i in x:
n = n * (1 + (i / 100))
print("{:,}원".format(round(n)))

Copy link

Apollo-Oh commented Sep 30, 2022

from time import time
import numpy as np

print(f"연습1.1) 동전을 10번 던져 앞면(숫자 1)과 뒷면(숫자 0)이 나오는 가상 실험을 파이썬으로 작성한다.\n
연습1.2)주사위를 100번 던져서 나오는 숫자의 평균을 구하라.")

def coinRun(cnt):
randseed = (int)((str)(time()).split('.')[1])
print(f"randseed={randseed}")
np.random.seed(randseed)
x = np.random.choice(np.array(['앞', '뒤']), 10, replace=True)
print(f"coinRun X---{x}")
return x

def diceRun(cnt):
randseed = (int)((str)(time()).split('.')[1])
print(f"randseed={randseed}")
np.random.seed(randseed)
p = np.array([0])
print(p, type(p), len(p))
for i in range((int)(cnt)):
x = np.random.choice(np.array([1, 2, 3, 4, 5, 6]), 1, replace=True)
print(f"diceRun---{x}")
p = np.hstack([p, x]) if len(p) != 1 else x
print(p)
a = np.mean(p)
return a

print(f"1.1 my answer: ==>{coinRun(10)}")
print(f"1.2 my answer: ==>{diceRun(100)}")

print(f"연습2) 가격이 10,000원인 주식이 있다.\n
이 주식의 일간 수익률(%)은 기댓값이 0%이고 표준편차가 1%인 표준 정규 분포를 따른다고 하자.\n
250일 동안의 주가를 무작위로 생성하라")

def makestockval(initialcost, days, p):
todayStockprice = initialcost
addStockprice = 0

for i in range(days):
    randseed = (int)((str)(time()).split('.')[1])
    print(f"randseed={randseed}")
    np.random.seed(randseed)
    rate = np.random.randn(100)
    print(rate)
    print(f"(int)(randseed % 100)=={(int)(randseed % 100)}")
    print(f"---rate==>{rate[(int)(randseed % 100)]}")
    addStockprice = todayStockprice * (rate[(int)(randseed % 100)] / 100)
    print(f"addStockprice==>{addStockprice}")
    todayStockprice = (int)(todayStockprice + addStockprice)
    x = np.array([todayStockprice])
    print(f"i={i}, x={x}")
    p[i] = todayStockprice
    print(p)
return p

p = np.zeros(250)
print(f"2 my answer: ==> {makestockval(10000, 250, p)}")

Copy link

profit_rand = np.random.randn(250)*0.01
price = 10000

for i in profit_rand:
price += price*i

price

Copy link

to302 commented Dec 28, 2022

3.5.2 번 문제 아래 답이 맞는지 확신이 안서서 남깁니다.

import numpy as np
(1 + np.random.randn(250)/100) * 10000

Copy link

sdkldc commented Feb 21, 2024

#3.5.1
np.random.randint(0,2,10)
np.random.randint(1,7,100).mean()
#3.5.2
주식 = 10000
수익률 = np.random.randn(250)
for i in range(250):
주식 = 주식 * (100+수익률[i])/100
print(주식)

Copy link

fckapp commented May 6, 2024

연습문제

1. 동전을 10번 던져 앞면(숫자 1)과 뒷면(숫자 0)이 나오는 가상 실험을 파이썬으로 작성한다.

np.random.choice(2, 10, p=[0.5, 0.5])

2.주사위를 100번 던져서 나오는 숫자의 평균을 구하라.

random_num = np.random.randint(1, high=7, size=100)
np.mean(random_num)
or
np.sum(random_num) / 100

# 3. 가격이 10,000원인 주식이 있다.

# 이 주식의 일간 수익률(%)은 기댓값이 0%이고 표준편차가 1%인 표준 정규 분포를 따른다고 하자.

# 250일 동안의 주가를 무작위로 생성하라

price = 10000
days = np.random.randn(250) * 0.01
for i in days:
total = price * (1 + i)
print("{:,}원".format(round(total)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants