how can i resolve error non-numeric argument to binary operator - nls

i was trying to generate parameter estimate for a nonlinear equation using r. i guess the starting points but i got the following error message:
"error in exp^-((Ht - h5/(b1))) : non-numeric argument to binary operator"
Ht and h5 are variables in the dataset and b1, b2, b3 are parameter to be estimated using nls.
g<- formula(D5^2 ~ Dbh^2* ((Ht- h5 - b1+ (b1*exp^-((Ht- h5/(b1))))
+ b2 *(Ht-h5)*(exp^(-h5/b3)))/
(Ht-1.3 - b1 + b1*(exp^-(Ht-1.3)/b1) + b2* (Ht- 1.3)*(exp^(-1.3/b3)))))
h<- nls(g, data= odat,
start = list(b1= 6.3, b2= 0.6, b3 = 1.4))

Related

How to take all values from a single cell

I have the following data
X f1 f2 f3
1 20 20/5/2 3
2 0 10/5/0 7
3 15 20/2/1 3
4 30 80/0/9 3
I want to make SUM() of all values in f2 column but it gives me an error because of the /.
How can I take each value, separately?
Plus, how to get each relative percentage of each cell in f2? For example, the first cell of f2 would be 74,07 / 18,52 / 7,41 taken from doing (20/27 - 5/27- 2/27)
use:
=INDEX(SUM(IFERROR(SPLIT(F1:F; "/"); 0)*1))
update:
=ARRAYFORMULA(IF(C1:C="";;SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(ROUND(
IFERROR(SPLIT(C1:C; "/")/MMULT(1*IFERROR(SPLIT(C1:C; "/"));
SEQUENCE(COLUMNS(SPLIT(C1:C; "/")); 1;;)^0))*100; 2));;9^9)); " "; " / ")))

Using quadratic function with unknown constant term, How can i find these unknown constant using gradient descent?

everyone.
I am beginner of machine learning and start learning about gradient descent right now. However, I got a one big problem. Following question is like this :
given numbers [0,0],[1,1],[1,2],[2,1] and
equation will be [ f=(a2)*x^2 + (a1)*x + a0 ]
With hand-solving, i got a answer [-1,5/2,0]
but it is hard to find out the solution from making a python code with gradient descent with these given data.
In my case, I try to make a code with gradient descent method with easiest and fastest way like :
learningRate = 0.1
make **a series of number of x
initialize given 1,1,1 for a2,a1,a0
partial derivative for a2,a1,a0 (a2_p:2x, a1_p:x, a0_p:1)
gradient descent method : (ex) a2 = a2 - (learningRate)( y - [(a2)*x^2 + (a1)*x + a0] )(a2_p)
ps. Honestly, I do not know what should i put 'x' and 'y' or a2, a1, a0.
However, i got a wrong answer with different result each time.
So, I want to get a hint for correct equation or code sequence.
Thank you for reading my lowest level of question.
There are a few errors in your equations
For the function f(x) = a2*x^2+a1*x+a0, partial derivatives for a2, a1 and a0 are x^2, x and 1, respectively.
Suppose cost function is (1/2)*(y-f(x))^2
Partial derivatives of cost function with respect to ai is -(y-f(x))* partial derivative of f(x) for ai, where i belongs to [0,2]
So, the gradient descent equation is:
ai = ai + learning_rate*(y-f(x)) * partial derivative of f(x) for ai, where i belongs to [0,2]
I hope this code helps
#Training sample
sample = [(0,0),(1,1),(1,2),(2,1)]
#Our function => a2*x^2+a1*x+a0
class Function():
def __init__(self, a2, a1, a0):
self.a2 = a2
self.a1 = a1
self.a0 = a0
def eval(self, x):
return self.a2*x**2+self.a1*x+self.a0
def partial_a2(self, x):
return x**2
def partial_a1(self, x):
return x
def partial_a0(self, x):
return 1
#Initialise function
f = Function(1,1,1)
#To Calculate loss from the sample
def loss(sample, f):
return sum([(y-f.eval(x))**2 for x,y in sample])/len(sample)
epochs = 100000
lr = 0.0005
#To record the best values
best_values = (0,0,0)
for epoch in range(epochs):
min_loss = 100
for x, y in sample:
#Gradient descent
f.a2 = f.a2+lr*(y-f.eval(x))*f.partial_a2(x)
f.a1 = f.a1+lr*(y-f.eval(x))*f.partial_a1(x)
f.a0 = f.a0+lr*(y-f.eval(x))*f.partial_a0(x)
#Storing the best values
epoch_loss = loss(sample, f)
if min_loss > epoch_loss:
min_loss = epoch_loss
best_values = (f.a2, f.a1, f.a0)
print("Loss:", min_loss)
print("Best values (a2,a1,a0):", best_values)
Output:
Loss: 0.12500004789165717
Best values (a2,a1,a0): (-1.0001922562970325, 2.5003368582261487, 0.00014521557599919338)

Recurrence relation - equal roots of characteristic equation

I have the following problem:
Solve the following recurrence relation, simplifying your final answer
using 'O' notation.
f(0)=3
f(1)=12
f(n)=6f(n-1)-9f(n-2)
We know this is a homogeneous 2nd order relation so we write the characteristic equation: a^2-6a+9=0 and the solutions are a1,2=3.
The problem is when I replace these values I get:
f(n)=c1*3^n+c2*3^n
and using the 2 initial relations I have:
f(0)=c1+c2=3
f(1)=3(c1+c2)=12
which gives me that there no values such that c1 and c2 such that these 2 relation are true.
Am I doing something wrong? Is the way it should be solved different when it comes to identical roots for the characteristic equation?
You can't solve it this way, because your matrix A is not diagonalizable.
However, here is what you get if you use Jordan's normal form instead:
f(n) = 3^{n-1}(3n + 9)
The Jordan matrix and the basis (with notation from wikipedia + Octave) is:
J := [3,1;0,3]
P := [3,4;1,1]
such that PJP^{-1} = A, where
A := [6,-9;1,0]
is your recurrence matrix. Furthermore, the Jordan matrix is almost as good as a diagonal matrix for computing powers:
J^n = 3^(n-1) * [3,n;0,3].
The recurrence is then:
[f(n+1); f(n)] = A^n [12,3] = PJ^nP^-1[12,3] = (<whatever>, 3^(n-1)*(3n+9)).
Here a quick numerical check (Scala, but you can take whatever you want, Octave or I whatever you like):
scala> def f(n: Int): Int = { if (n == 0) 3 else if (n == 1) 12 else (6 * f(n-1) - 9 * f(n-2)) }
f: (n: Int)Int
scala> for (i <- 0 until 20) println(f(i))
3
12
45
162
567
1944
6561
21870
72171
236196
767637
2480058
7971615
25509168
81310473
258280326
817887699
^
scala> def explicit(n: Int): Int = (Math.pow(3, n -1) * (3 * n + 9)).toInt
explicit: (n: Int)Int
scala> for (i <- 0 until 20) println(explicit(i))
3
12
45
162
567
1944
6561
21870
72171
236196
767637
2480058
7971615
25509168
81310473
258280326
817887699

nlsLM giving error: attempt to use zero-length variable name - Black Scholes Model Fit

I am working on my modeling skills and am having a go at Black Scholes. The idea is to use the current option prices (x) to get an estimate for volatility (b).
When I run nlsLM, I get this error:
Error in assign(i, data[[i]], envir = env) :
attempt to use zero-length variable name
I cannot see where the error is, I have removed all of the variables from the formula and replaced them with their numerical values for this specific case. x and b remain as variables. I have run this without d1 and d2, in other words, placing the long formulas for each inside the respective pnorm() but no luck there either.
x is the strike price, y is the price of the GOOG call expiring 12/20/14 as of 12/12/14.
Here is the code to reproduce the error.
df <- structure(list(x = c(505, 510, 512.5, 515, 517.5, 520, 522.5,
525, 527.5, 530, 532.5), y = c(17.7, 12.5, 12, 9.2, 7.82, 6.3,
5.1, 4.1, 3.21, 2.45, 1.9)), .Names = c("x", "y"),
row.names = c(NA, -11L), class = "data.frame")
f <- function(x, b){
d1 = (1/(b*sqrt(0.021918)))*(log(518.66/x) + (0.0025+b^2/2) * 0.021918)
d2 = (1/(b*sqrt(0.021918)))*(log(518.66/x) - (0.0025+b^2/2) * 0.021918)
return (pnorm(d1)*518.66 - pnorm(d2)*x*exp(-0.0025*0.021918))
}
library(minpack.lm)
test <- nlsLM(y~f(x, b), data=df, start=list((b=0.50)))
Thank you.
Found the answer...There is a line in the nlsLM function:
for (i in names(start)) assign(i, start[[i]], envir = startEnv)
The problem was the start provided in my formula did not have a name, b. I thought by using the list command that would resolve the issue.
I have corrected it as follows:
test <- nlsLM(y~f(x, b), data=df, start=c(b=0.50))

How to convert datetime interval to hours?

I have this spreadsheet:
A B C D E
1 08/13/2013 02:10 4
2 08/13/2013 02:19 10 00:09:00 160
In D2, I have this formula : =if(B2="";"";to_date(concatenate(A2;" ";B2))-to_date(concatenate(A1;" ";B1)))
In E2 I have this formula : =if(D2="";"";(C2-C1)/D2)
But E2 outputs the wrong result, 160. I want it to be 40 (=10-4/0.15). 0.15 is the value in D2 converted to hours.
How can I do that?
Do not quite understand what you're trying to calculate, but with the information that explains, the following formula can help:
=IF(D2 = "", "", ROUND((C2-C1)/(D2*24), 0))

Resources