Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
cantaro86
GitHub Repository: cantaro86/Financial-Models-Numerical-Methods
Path: blob/master/src/FMNM/cost_utils.py
1700 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Mon Jun 10 09:56:25 2019
5
6
@author: cantaro86
7
"""
8
9
import numpy as np
10
11
12
def no_opt(x, y, cost_b, cost_s):
13
cost = np.zeros((len(x), len(y)))
14
15
for i in range(len(y)):
16
if y[i] <= 0:
17
cost[:, i] = (1 + cost_b) * y[i] * np.exp(x)
18
else:
19
cost[:, i] = (1 - cost_s) * y[i] * np.exp(x)
20
21
return cost
22
23
24
def writer(x, y, cost_b, cost_s, K):
25
cost = np.zeros((len(x), len(y)))
26
27
for i in range(len(x)):
28
for j in range(len(y)):
29
if y[j] < 0 and (1 + cost_b) * np.exp(x[i]) <= K:
30
cost[i][j] = (1 + cost_b) * y[j] * np.exp(x[i])
31
32
elif y[j] >= 0 and (1 + cost_b) * np.exp(x[i]) <= K:
33
cost[i][j] = (1 - cost_s) * y[j] * np.exp(x[i])
34
35
elif y[j] - 1 >= 0 and (1 + cost_b) * np.exp(x[i]) > K:
36
cost[i][j] = ((1 - cost_s) * (y[j] - 1) * np.exp(x[i])) + K
37
38
elif y[j] - 1 < 0 and (1 + cost_b) * np.exp(x[i]) > K:
39
cost[i][j] = ((1 + cost_b) * (y[j] - 1) * np.exp(x[i])) + K
40
41
return cost
42
43
44
def buyer(x, y, cost_b, cost_s, K):
45
cost = np.zeros((len(x), len(y)))
46
47
for i in range(len(x)):
48
for j in range(len(y)):
49
if y[j] < 0 and (1 + cost_b) * np.exp(x[i]) <= K:
50
cost[i][j] = (1 + cost_b) * y[j] * np.exp(x[i])
51
52
elif y[j] >= 0 and (1 + cost_b) * np.exp(x[i]) <= K:
53
cost[i][j] = (1 - cost_s) * y[j] * np.exp(x[i])
54
55
elif y[j] + 1 >= 0 and (1 + cost_b) * np.exp(x[i]) > K:
56
cost[i][j] = ((1 - cost_s) * (y[j] + 1) * np.exp(x[i])) - K
57
58
elif y[j] + 1 < 0 and (1 + cost_b) * np.exp(x[i]) > K:
59
cost[i][j] = ((1 + cost_b) * (y[j] + 1) * np.exp(x[i])) - K
60
61
return cost
62
63